-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
183 lines (161 loc) · 5.34 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import tmi from 'tmi.js';
import environment from './environment.js';
import { TEXT_COMMANDS, REDES_COMMAND } from './text-commands.js';
import { creadores } from './creator-shoutouts.js';
import { htmlToText } from 'html-to-text';
// Setup client
export const setupBot = (io) => {
const opts = {
identity: {
username: environment.BOT_USERNAME,
password: environment.OAUTH_TOKEN,
},
channels: [environment.CHANNEL_NAME],
};
let spamRedesInterval = null;
const client = new tmi.client(opts);
client.on('message', onMessageHandler);
client.on('connected', onConnectedHandler);
client.connect();
const parseEmotes = (message, emotes, emoteOnly) => {
if (!emotes) return message;
const emoteEntries = Object.entries(emotes);
const onlyOneEmote =
emoteEntries.length === 1 && emoteEntries[0][1].length === 1;
const emoteSize = onlyOneEmote && emoteOnly ? '3.0' : '1.0';
// store all emote keywords
// ! you have to first scan through
// the message string and replace later
const stringReplacements = [];
// iterate of emotes to access ids and positions
emoteEntries.forEach(([id, positions]) => {
// use only the first position to find out the emote key word
const position = positions[0];
const [start, end] = position.split('-');
const stringToReplace = message.substring(
parseInt(start, 10),
parseInt(end, 10) + 1
);
stringReplacements.push({
stringToReplace: stringToReplace,
replacement: `<img src="https://static-cdn.jtvnw.net/emoticons/v1/${id}/${emoteSize}">`,
});
});
// generate HTML and replace all emote keywords with image elements
const messageHTML = stringReplacements.reduce(
(acc, { stringToReplace, replacement }) => {
// obs browser doesn't seam to know about replaceAll
return acc.split(stringToReplace).join(replacement);
},
message
);
return messageHTML;
};
const sameCommand = (inputNames, command) => {
let same = false;
inputNames.forEach((input) => {
same = same || input.toUpperCase().startsWith(command.toUpperCase());
});
return same;
};
const commandsList = () => {
const reducer = (valorAnterior, valorActual, indice) => {
if (indice === 0) {
return `${valorAnterior} !${valorActual.name}`;
} else {
return `${valorAnterior}, !${valorActual.name}`;
}
};
return TEXT_COMMANDS.reduce(reducer, 'Los comandos disponibles son:');
};
const checkListCommandAndReact = (commandInput, target) => {
if (commandInput === 'comandos') {
client.say(target, commandsList());
console.log(`* Executed !commandos command`);
return true;
} else {
return false;
}
};
const checkCommandsAndReact = (commandInput, target, chatter) => {
let command = commandInput.split(' ')[0];
if (!checkListCommandAndReact(commandInput, target)) {
let foundCommand = TEXT_COMMANDS.find((textCommand) =>
sameCommand(textCommand.names, command)
);
foundCommand =
foundCommand ||
creadores.find((creador) => sameCommand(creador.names, command));
if (foundCommand) {
if (
foundCommand.name === 'reset' &&
chatter.toUpperCase() === 'EMOPOREMILIO'
) {
io.emit('reset', {});
}
if (
foundCommand.name === 'setTimer' &&
chatter.toUpperCase() === 'EMOPOREMILIO'
) {
const numbersToSet = commandInput.split(' ')[1];
const numbersSeparated = numbersToSet.split(':');
const firstNumber = parseInt(numbersSeparated[0]);
const secondNumber = parseInt(numbersSeparated[1]);
if (numbersToSet) {
io.emit('setTimer', { hours: firstNumber, minutes: secondNumber });
}
}
client.say(target, foundCommand.message);
console.log(`* Executed !${foundCommand.name} command`);
} else {
console.log(`* Unknown command ${commandInput}`);
}
}
};
const saveUsernameHit = (username) => {
//irAMongo
//sumarle1alusuario
};
// Called every time a message comes in
function onMessageHandler(target, context, msg, self) {
//
const emotesParsedMsg = parseEmotes(
htmlToText(msg),
context.emotes ?? {},
context['emote-only']
);
io.emit('chatMessage', {
msg: emotesParsedMsg,
username: context['display-name'],
});
if (self) {
return;
} // Ignore messages from the bot
// Remove whitespace from chat message
const commandInput = msg.trim();
saveUsernameHit(context.username);
// If message is a command and the command is known, let's execute it
if (commandInput[0] === '!') {
//console.log(commandInput.substring(1));
checkCommandsAndReact(
commandInput.substring(1),
target,
context.username
);
}
}
const spamRedes = () => {
client.say(environment.CHANNEL_NAME, REDES_COMMAND.message);
};
const initSpamRedesInterval = () => {
spamRedesInterval = setInterval(() => {
spamRedes();
//saveUsersToMongo();
}, 60 * 60 * 1000);
};
// Called every time the bot connects to Twitch chat
function onConnectedHandler(addr, port) {
console.log(`* Connected to ${addr}:${port}`);
initSpamRedesInterval();
}
};