Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function main() {
try {
// Inicia o servidor
const Server = server();
// Inicializa a API da Twitch e do Giphy
const { channels, token, username } = config.twitch;
const { token: giphy_token, rating } = config.giphy;
const { chat } = new twitch({ token, username });
const gif = giphy(giphy_token);
// Conecta na Twitch e aos entra nos chats configurados
await chat.connect();
await Promise.all(channels.map(ch => chat.join(ch)));
// Escutar todas as mensagem privadas
chat.on('PRIVMSG', async payload => {
const {
tags: { color },
username,
message,
channel,
} = payload;
// Caso o usuário não tem uma cor definida, ele irá gerar uma cor
async initClient (type: 'bot' | 'broadcaster') {
clearTimeout(this.timeouts[`initClient.${type}`]);
const [token, username, channel] = await Promise.all([
oauth[type + 'AccessToken'],
oauth[type + 'Username'],
oauth.generalChannel,
]);
try {
if (token === '' || username === '' || channel === '') {
throw Error(`${type} - token, username or channel expected`);
}
const log = isDebugEnabled('tmi.client') ? { level: 'debug' } : { level: 'silent' };
this.client[type] = new TwitchJs({
token,
username,
log,
onAuthenticationFailure: () => oauth.refreshAccessToken(type).then(token => token),
});
this.loadListeners(type);
await (this.client[type] as TwitchJs).chat.connect();
await this.join(type, channel);
} catch (e) {
if (type === 'broadcaster' && !this.broadcasterWarning) {
error('Broadcaster oauth is not properly set - hosts will not be loaded');
error('Broadcaster oauth is not properly set - subscribers will not be loaded');
this.broadcasterWarning = true;
}
this.timeouts[`initClient.${type}`] = setTimeout(() => this.initClient(type), 10000);
}