-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
178 lines (159 loc) · 5.47 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
/**
* @description Importaçao de blibiotecas e preparação de ambiente
*
* @requires discord.js
* @requires tmi.js
* @requires dotenv
* @requires fs
*/
require('dotenv').config()
const channels = process.env.TWITCH_CHANNELS.split(',');
const DiscordConnect = require('discord.js');
const express = require('express')
const tmi = require('tmi.js');
const fs = require('fs');
const discord = {};
const twitch = {};
/**
* @description Startup message
*/
console.log(fs.readFileSync('startup.txt', 'utf8').toString());
/**
* @description Carregamento assicrono das API's
*/
delete new Promise(async (resolve) => {
const api = express()
// não instanciar endpoint de status
if (!process.env.PORT && !process.env.COMMON_API_PORT) {
resolve(console.log(' > api status offline'));
return;
}
// estado de funcionamento
api.get('/', (req, res) => {
res.send('ok');
});
// acesso para api
api.listen(process.env.PORT || process.env.COMMON_API_PORT, () => {
resolve(console.log(' > api status online'));
});
});
discord.promise = new Promise(async (resolve) => {
discord.client = new DiscordConnect.Client();
resolve(console.log(' > loading discord client'));
});
twitch.promise = new Promise(async (resolve) => {
twitch.client = new tmi.client({
// authentication
identity: {
username: process.env.TWITCH_BOT_USERNAME,
password: process.env.TWITCH_OAUTH_TOKEN
},
// protect channels array
channels: [...channels]
});
resolve(console.log(' > loading twitch options'));
});
/**
* @description Conexão com as APIS
*/
discord.promise.then(async () => {
await discord.client.login(process.env.DISCORD_SECRET_TOKEN);
console.log(` > discord connected`);
});
twitch.promise.then(async () => {
await twitch.client.connect();
console.log(` > twitch connected`);
});
/**
* @description Adicionar comandos no discord
*/
discord.promise.then(async () => {
console.log(' > add discord commands');
const commands = [];
/**
* Leitura de comandos disponivéis
*/
await fs.readdirSync('./commands').forEach(file => {
var command = require(`./commands/${file}`);
commands.push({
permit: typeof (command.discord_roles) != 'undefined'? command.discord_roles: [],
event: command.event
});
});
discord.client.on('message', (message) => {
// ignorar quando não for um comando
if (message.content[0] != process.env.DISCORD_COMMAND_PREFIX) {
return;
}
// preparar para executar comandos
let params = message.content.slice(1).split(' ');
let cmdtext = params.shift().toLowerCase();
// buscar pelo commando
commands.forEach((command) => {
// verificar pelas permissões
if (command.event.handlersCount(cmdtext) && command.permit.length) {
if(!message.member || !command.permit.find(tag => (message.member.roles.cache.has(tag)))) {
message.reply(`Você não tem permissão para executar esse comando!`);
return;
}
}
// efetuar commando
command.event.emit(cmdtext, params, {
is_twitch: false,
is_discord: true,
reply: (text) => message.reply(text),
send: (text) => message.channel.send(text),
channels: channels
});
});
});
});
/**
* @description Adicionar comandos na twitch
*/
twitch.promise.then(async () => {
console.log(' > add twitch commands');
const commands = [];
/**
* Leitura de comandos disponivéis
*/
await fs.readdirSync('./commands').forEach(file => {
var command = require(`./commands/${file}`);
commands.push({
permit: typeof (command.twitch_tags) != 'undefined'? command.twitch_tags: [],
event: command.event
});
});
/**
* @see raw_message É chamado quando qualquer event ocorre na twitch e possui dados brutos das mensagens
*/
await twitch.client.on("raw_message", (messageCloned, message) => {
// ignorar quando não for uma menssagem no chat ou comando
if (message.command != "PRIVMSG" || message.params[1][0] != process.env.TWITCH_COMMAND_PREFIX) {
return;
}
// preparar para executar comando
let channel = message.params[0];
let params = message.params[1].slice(1).split(' ');
let cmdtext = params.shift().toLowerCase();
let author = message.tags['display-name'];
// buscar pelo commando
commands.forEach((command) => {
// verificar pelas permissões
if (command.event.handlersCount(cmdtext) && command.permit.length) {
if(!message.tags["badges"] && !command.permit.find(tag => (message.tags["badges"].includes(tag)))) {
twitch.client.say(channel, `@${author}, Você não tem permissão para executar esse comando!`);
return;
}
}
// efetuar commando
command.event.emit(cmdtext, params, {
is_twitch: true,
is_discord: false,
reply: (text) => twitch.client.say(channel, `@${author}, ${text}`),
send: (text) => twitch.client.say(channel, text),
channels: channels
});
});
});
});