forked from moothz/legionbot-open
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_gerencia.js
342 lines (303 loc) · 11.5 KB
/
cmd_gerencia.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
const { loggerInfo, loggerWarn } = require("./logger");
const { getDbGeral, saveDbGeral, getGrupoByNome, updateDbs } = require("./db");
const configs = require("./configs");
const mime = require('mime-types');
const fsp = require('fs').promises;
const path = require('node:path');
const { reagirMsg } = require("./wrappers-bot");
/*
Os comandos de gerencia seguem o seguinte padrão
!gerenciar-CATEGORIA-ITEM
Categorias:
- grupo: Boas vindas, Lembretes,
- cmd: NSFW, Globais, CRUD de comandos
- roleta: Opções da roleta russa
- twitch: Canal da twitch do grupo
- youtube: Canais do youtube do grupo
*/
const gerenciaHandlers = {
"info": infoGrupoHandler,
"grupo": gerenciarGrupoHandler,
"cmd": gerenciarCmdHandler,
"filtros": gerenciarFiltrosHandler,
"roleta": gerenciarRoletaHandler,
"twitch": gerenciarTwitchHandler,
"youtube": gerenciarYoutubeHandler
};
function getTipoMedia(tipoMensagem,isGif){
let emojiTipoMedia, tipoMediaAdd;
if(tipoMensagem == "chat"){
emojiTipoMedia = "💬";
tipoMediaAdd = "msg";
} else
if(tipoMensagem == "image"){
emojiTipoMedia = "🎨";
tipoMediaAdd = "img";
} else
if(tipoMensagem == "video"){
emojiTipoMedia = "📺";
tipoMediaAdd = "vid";
if(isGif){
emojiTipoMedia = "🎁";
tipoMediaAdd = "gif";
}
} else
if(tipoMensagem == "audio"){
emojiTipoMedia = "🔉";
tipoMediaAdd = "audio";
} else
if(tipoMensagem == "voice"){
emojiTipoMedia = "🎤";
tipoMediaAdd = "audio";
} else
if(tipoMensagem == "pt"){
emojiTipoMedia = "🎤";
tipoMediaAdd = "audio";
}
else if(tipoMensagem == "sticker"){
emojiTipoMedia = "🖼";
tipoMediaAdd = "sticker";
}
else if(tipoMensagem == "document"){
emojiTipoMedia = "📄";
tipoMediaAdd = "document";
}
return {tipo: tipoMediaAdd, react: emojiTipoMedia};
}
function infoGrupoHandler(dados){
return new Promise(async (resolve,reject) => {
const grupo = getGrupoByNome(dados.nomeGrupo);
});
}
function gerenciarHandler(dados){
return new Promise(async (resolve,reject) => {
const grupo = getGrupoByNome(dados.nomeGrupo);
if(!grupo){
resolve([{msg: "Seu grupo ainda não foi cadastrado e não pode ser gerenciado.\n\n!cadastrar nomeDoGrupo", reply: true, react: "⚠️"}]);
} else {
const args = dados.cleanMessageText.split(" ");
const [x, categoria, item] = args.shift().split("-"); // ["!gerenciar","categoria", "item"]
loggerInfo(`[gerenciarHandler] ${categoria}.${item}`);
if(gerenciaHandlers[categoria]){
//loggerInfo(`[gerenciarHandler] ${dados.nomeGrupo} -> ${JSON.stringify(grupo)}`);
gerenciaHandlers[categoria](item, args, grupo, dados).then(resolve);
} else {
resolve([{msg: "Comando de gerência não encontrado.", reply: true, react: "🤔"}]);
}
}
});
}
function gerenciarGrupoHandler(item, args, grupo, dados){
return new Promise(async (resolve,reject) => {
loggerInfo(`[gerenciarGrupoHandler] ${item} -> ${JSON.stringify(args)}`);
});
}
function gerenciarCmdHandler(item, args, grupo, dados){
return new Promise(async (resolve,reject) => {
loggerInfo(`[gerenciarCmdHandler] ${item} -> ${JSON.stringify(args)}`);
});
}
function gerenciarFiltrosHandler(item, args, grupo, dados){
return new Promise(async (resolve,reject) => {
loggerInfo(`[gerenciarFiltrosHandler] ${item} -> ${JSON.stringify(args)}`);
});
}
function gerenciarRoletaHandler(item, args, grupo, dados){
return new Promise(async (resolve,reject) => {
loggerInfo(`[gerenciarRoletaHandler] ${item} -> ${JSON.stringify(args)}`);
});
}
function gerenciarTwitchHandler(item, args, grupo, dados){
return new Promise(async (resolve,reject) => {
loggerInfo(`[gerenciarTwitchHandler] ${item} -> ${JSON.stringify(args)}`);
if(!grupo.twitch){
// Sem canal definido
if(item == "canal"){ // mas estão tentando definir! Cria novo objeto com valores padrões
grupo.twitch = {
"canal": args[0],
"canaisZap": [grupo.numero],
"tituloLiveOn": false,
"tituloLiveOff": false,
"msgOn": true,
"imgOn": false,
"gifOn": false,
"audioOn": false,
"vidOn": false,
"imgOff": false,
"gifOff": false,
"audioOff": false,
"msgOff": false,
"vidOff": false,
"stickerOff": false,
"publico": false
}
saveDbGeral();
loggerInfo(`[gerenciarTwitchHandler]${debugHeader} Criado canal da twitch '${args[0]}'!`);
resolve([{msg: `[${grupo.nome}] O canal *${args[0]}* foi definido para este grupo!`, reply: true, react: "👍"}]);
} else {
loggerInfo(`[gerenciarTwitchHandler]${debugHeader} Tentou alterar '${item}' mas o canal ainda não foi definido.`);
resolve([{msg: `[${grupo.nome}] Este grupo ainda não possui um canal da twitch definido.`, reply: true, react: "👎"}]);
}
} else {
const debugHeader = `[${item}][${grupo.nome}@${grupo.twitch.canal}]`;
let guardarAlteracoes = false;
let valorAnterior = "Nenhum";
let novoValor = "";
let msgErro = false;
let aguardarPromise = false; // Mensagens com mídia demoram a ser processadas
if(item === "marcar"){
if(grupo.opts.marcarTodosTwitch){
valorAnterior = "Sim";
novoValor = "Não";
} else {
valorAnterior = "Não";
novoValor = "Sim";
}
item = "Marcar Todos do Grupo (Twitch)";
grupo.opts.marcarTodosTwitch = !grupo.opts.marcarTodosTwitch;
guardarAlteracoes = true;
} else
if(item === "mudartitulo"){
if(grupo.opts.mudarTituloGrupoByTwitch){
valorAnterior = "Sim";
novoValor = "Não";
} else {
valorAnterior = "Não";
novoValor = "Sim";
}
item = "Mudar Título do Grupo (Twitch)";
grupo.opts.mudarTituloGrupoByTwitch = !grupo.opts.mudarTituloGrupoByTwitch;
guardarAlteracoes = true;
} else
if(item === "canal"){
valorAnterior = grupo.twitch.canal;
novoValor = args[0];
grupo.twitch.canal = args[0];
guardarAlteracoes = true;
} else
if(item === "titulo_on"){
valorAnterior = grupo.twitch.tituloLiveOn;
grupo.twitch.tituloLiveOn = dados.msg.body.split(" ").slice(1).join(" ").trim();
if(grupo.twitch.tituloLiveOn.length < 1){
grupo.twitch.tituloLiveOn = false;
novoValor = "Nenhum";
} else {
novoValor = grupo.twitch.tituloLiveOn;
}
guardarAlteracoes = true;
} else
if(item === "titulo_off"){
valorAnterior = grupo.twitch.tituloLiveOff;
grupo.twitch.tituloLiveOff = dados.msg.body.split(" ").slice(1).join(" ").trim();
if(grupo.twitch.tituloLiveOff.length < 1){
grupo.twitch.tituloLiveOff = false;
novoValor = "Nenhum";
} else {
novoValor = grupo.twitch.tituloLiveOff;
}
guardarAlteracoes = true;
} else
if(item === "visibilidade"){
if(grupo.twitch.publico){
valorAnterior = "Visível";
novoValor = "Oculto";
} else {
valorAnterior = "Oculto";
novoValor = "Visível";
}
grupo.twitch.publico = !grupo.twitch.publico;
guardarAlteracoes = true;
} else
if(item.startsWith("media_o") || item.startsWith("midia_o")){
const tipoMomento = item.includes("off") ? "Off" : "On";
// Aqui pode ser texto, img, gif, sticker...
loggerInfo(`[gerenciarTwitchHandler]${debugHeader} hasQuotedMsg? ${dados.msg.hasQuotedMsg}`);
if(dados.msg.hasQuotedMsg){
loggerInfo(`[gerenciarTwitchHandler]${debugHeader} hasMedia? ${dados.quotedMsg.hasMedia}`);
if(dados.quotedMsg.hasMedia){
aguardarPromise = true;
// Se tem mídia, baixa
const tipoMediaTwitch = getTipoMedia(dados.quotedMsg.type.toLowerCase(), dados.quotedMsg.isGif);
loggerInfo(`[gerenciarTwitchHandler][${item}] Recebido: ${tipoMediaTwitch.tipo}, baixando...`);
reagirMsg(dados.msg, "⏳");
dados.quotedMsg.downloadMedia().then(attachmentData => {
// Baixa e salva o arquivo
const buff = Buffer.from(attachmentData.data, "base64");
const arquivoMidia = path.join(configs.rootFolder, "media",`twitch_${grupo.nome}_${grupo.twitch.canal}_${tipoMediaTwitch.tipo}.${mime.extension(attachmentData.mimetype)}`);
fsp.writeFile(arquivoMidia, buff).then((res) => {
loggerInfo(`[gerenciarTwitchHandler]${debugHeader} ${tipoMediaTwitch.tipo}: ${arquivoMidia}`);
grupo.twitch[`${tipoMediaTwitch.tipo}${tipoMomento}`] = arquivoMidia;
saveDbGeral();
resolve([{msg: `[${grupo.nome}] *${item}* recebido e definido como ${tipoMediaTwitch.tipo}${tipoMomento}!`, reply: true, react: tipoMediaTwitch.react}]);
}).catch(e => {
throw(e);
});
}).catch(e => {
loggerWarn(`[gerenciarTwitchHandler]${debugHeader} '${item}' não foi possível baixar mídia de quotedMsg.\n${e}`);
console.warn(e);
resolve([{msg: `[${grupo.nome}] Não consegui baixar este arquivo pra definir como *${item}*. Tente enviar novamente!`, reply: true, react: "👎"}]);
});
} else {
// Se não tem , é msgOn ou msgOff
grupo.twitch[`msg${tipoMomento}`] = dados.quotedMsg.body;
loggerInfo(`[gerenciarTwitchHandler]${debugHeader} msg${tipoMomento}: ${dados.quotedMsg.body}`);
resolve([{msg: `[${grupo.nome}] *${item}* recebido e definido como msg${tipoMomento}:\n\n${dados.quotedMsg.body}`, reply: true, react: "💬"}]);
guardarAlteracoes = true;
}
} else {
guardarAlteracoes = false; // Não precisa
loggerInfo(`[gerenciarTwitchHandler]${debugHeader} Não marcou mensagem com mídia.`);
msgErro = `[${grupo.nome}] Para definir uma mídia é necessário responder à mensagem desejada.`;
}
} else
if(item.startsWith("media") || item.startsWith("midia")){
msgErro = `[${grupo.nome}] Você deve informar ser é uma mídia On ou Off!\n*Uso:* Responda a mensagem do conteúdo com:\n!gerenciar-twitch-media_on\nou\n!gerenciar-twitch-media_off`;
} else
if(item.startsWith("del")){
// Deletar vem no argumento
// !gerenciar-twitch-del msgoff / !gerenciar-twitch-del vidon
const obj = args[0];
if(grupo.twitch[obj]){
grupo.twitch[obj] = false;
valorAnterior = obj;
novoValor = "- Nada -";
loggerInfo(`[gerenciarTwitchHandler]${debugHeader} Removido: ${obj}`);
item = `${obj} (Removido)`; // apenas pra ficar bonito na msg de resposta
guardarAlteracoes = true;
} else {
loggerInfo(`[gerenciarTwitchHandler]${debugHeader} Está tentando deletar algo estranho: ${obj}`);
msgErro = `[${grupo.nome}] '${obj}' não é uma propriedade válida.\n*Uso*: !gerenciar-twitch-del propriedade\n\n*Valores possíveis:*\nmsgOn, imgOn, gifOn, audioOn, vidOn, imgOff, gifOff, audioOff, msgOff, vidOff e stickerOff`;
}
}
if(!aguardarPromise){
if(guardarAlteracoes){
loggerInfo(`[gerenciarTwitchHandler]${debugHeader} Alterado '${item}': ${valorAnterior} -> ${novoValor}`);
resolve([{msg: `[${grupo.nome}] *${item}* era _'${valorAnterior}'_ e agora é _'${novoValor}'_!`, reply: true, react: "👍"}]);
saveDbGeral();
} else {
loggerInfo(`[gerenciarTwitchHandler]${debugHeader} '${item}' não existe.`);
msgErro = msgErro ? msgErro : `[${grupo.nome}] *${item}* não é uma propriedade válida a ser definida!`;
resolve([{msg: msgErro, reply: true, react: "👎"}]);
}
}
}
});
}
function gerenciarYoutubeHandler(item, args, grupo, dados){
return new Promise(async (resolve,reject) => {
loggerInfo(`[gerenciarYoutubeHandler] ${item} -> ${JSON.stringify(args)}`);
});
}
/*
// Debug
updateDbs().then(()=>{
gerenciarHandler({
nomeGrupo: "legidonlog",
cleanMessageText: "!gerenciar-twitch-media_on [on] mutiz",
msg: {
body: "!gerenciar-twitch-titulo_on [ON] MUTiZZzZ"
}
}).then(console.log);
});
*/
module.exports = { gerenciarHandler }