-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
537 lines (459 loc) · 18 KB
/
index.js
File metadata and controls
537 lines (459 loc) · 18 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
require('dotenv').config();
const { Client, GatewayIntentBits, SlashCommandBuilder, REST, Routes, AttachmentBuilder } = require('discord.js');
const { exec } = require('child_process');
const fs = require('fs');
const { DEPLOY_STAGES, ERROR_MESSAGES, SUCCESS_MESSAGES, EMBEDS, LOG_CONTENT } = require('./messages');
const BOT_TOKEN = process.env.BOT_TOKEN;
const CLIENT_ID = process.env.CLIENT_ID;
const GUILD_ID = process.env.GUILD_ID;
const PASSWORD = process.env.PASSWORD;
const ALLOWED_USERS = process.env.ALLOWED_USERS ? process.env.ALLOWED_USERS.split(',') : [];
const SCRIPTS_FILE = process.env.SCRIPTS_FILE || 'scripts.json';
let AVAILABLE_SCRIPTS = {};
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
// [ CARREGAR SCRIPTS ]
// [ENTRADA] - Nenhuma
// [SAIDA] - Objeto com scripts carregados
// [OBJETIVO DA FUNÇÃO] - Carrega scripts do arquivo JSON
function loadScripts() {
try {
const data = fs.readFileSync(SCRIPTS_FILE, 'utf8');
return JSON.parse(data);
} catch (error) {
console.error('Erro ao carregar scripts:', error);
return {};
}
}
// [ SALVAR SCRIPTS ]
// [ENTRADA] - scripts: objeto com scripts para salvar
// [SAIDA] - boolean indicando sucesso/falha
// [OBJETIVO DA FUNÇÃO] - Salva scripts no arquivo JSON
function saveScripts(scripts) {
try {
fs.writeFileSync(SCRIPTS_FILE, JSON.stringify(scripts, null, 2));
return true;
} catch (error) {
console.error('Erro ao salvar scripts:', error);
return false;
}
}
// [ CRIAR LOG DE DEPLOY ]
// [ENTRADA] - scriptName: nome do script, result: resultado da execução, error: erro opcional
// [SAIDA] - nome do arquivo de log criado ou null
// [OBJETIVO DA FUNÇÃO] - Cria arquivo de log com resultado do deploy
function createDeployLog(scriptName, result, error = null) {
const timestamp = new Date().toISOString();
const logFileName = `deploy-log-${scriptName.replace('.sh', '')}-${Date.now()}.txt`;
const status = error ? 'ERRO' : 'SUCESSO';
const output = error ? error : result.stdout;
const logContent = LOG_CONTENT.TEMPLATE(timestamp, scriptName, status, output, result.stderr);
try {
fs.writeFileSync(logFileName, logContent);
return logFileName;
} catch (err) {
console.error('Erro ao criar log:', err);
return null;
}
}
// [ EXECUTAR SCRIPT COM ATUALIZAÇÕES ]
// [ENTRADA] - scriptPath: caminho do script, interaction: interação do Discord, scriptName: nome do script
// [SAIDA] - Promise com resultado da execução
// [OBJETIVO DA FUNÇÃO] - Executa script mostrando progresso em tempo real no Discord
async function executeScriptWithUpdates(scriptPath, interaction, scriptName) {
return new Promise(async (resolve, reject) => {
try {
await interaction.editReply({
content: DEPLOY_STAGES.STARTING(scriptName)
});
exec(`chmod +x "${scriptPath}"`, async (chmodError) => {
if (chmodError) {
console.error('Erro ao dar permissão de execução:', chmodError);
}
await interaction.editReply({
content: DEPLOY_STAGES.PERMISSIONS_OK(scriptName)
});
setTimeout(async () => {
await interaction.editReply({
content: DEPLOY_STAGES.GITHUB_DOWNLOADED(scriptName)
});
const child = exec(`bash "${scriptPath}"`, {
timeout: 300000,
maxBuffer: 1024 * 1024 * 10
});
let stdout = '';
let stderr = '';
let lastUpdate = Date.now();
child.stdout.on('data', async (data) => {
stdout += data;
if (Date.now() - lastUpdate > 1000) {
lastUpdate = Date.now();
await interaction.editReply({
content: DEPLOY_STAGES.EXECUTING(scriptName, stdout)
}).catch(() => {});
}
});
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', async (code) => {
await interaction.editReply({
content: DEPLOY_STAGES.FINALIZING(scriptName)
});
const result = { stdout, stderr };
const logFile = createDeployLog(scriptName, result, code !== 0 ? `Script falhou com código de saída: ${code}` : null);
if (code === 0) {
resolve({ result, logFile });
} else {
reject({
error: `Script falhou com código de saída: ${code}`,
stderr,
stdout,
logFile
});
}
});
child.on('error', async (error) => {
const result = { stdout, stderr };
const logFile = createDeployLog(scriptName, result, error.message);
reject({
error: error.message,
stderr,
stdout,
logFile
});
});
}, 2000);
});
} catch (error) {
reject({
error: error.message,
stderr: '',
stdout: '',
logFile: null
});
}
});
}
// [ CRIAR COMANDOS SLASH ]
// [ENTRADA] - Nenhuma
// [SAIDA] - Array com objetos de comando
// [OBJETIVO DA FUNÇÃO] - Define estrutura dos comandos slash do Discord
function createSlashCommands() {
const executeCommand = new SlashCommandBuilder()
.setName('executar-script')
.setDescription('Executa um script .sh na EC2')
.addStringOption(option =>
option.setName('script')
.setDescription('Selecione o script para executar')
.setRequired(true)
)
.addStringOption(option =>
option.setName('password')
.setDescription('Digite a senha para executar o script')
.setRequired(true)
);
const addScriptCommand = new SlashCommandBuilder()
.setName('adicionar-script')
.setDescription('Adiciona um novo script à lista de scripts disponíveis')
.addStringOption(option =>
option.setName('nome')
.setDescription('Nome do script (ex: deploy-api.sh)')
.setRequired(true)
)
.addStringOption(option =>
option.setName('caminho')
.setDescription('Caminho completo para o script (ex: ../TESTE/teste.sh)')
.setRequired(true)
)
.addStringOption(option =>
option.setName('password')
.setDescription('Digite a senha para adicionar o script')
.setRequired(true)
);
const clearMessagesCommand = new SlashCommandBuilder()
.setName('limpar-mensagens')
.setDescription('Limpa mensagens do canal atual')
.addIntegerOption(option =>
option.setName('quantidade')
.setDescription('Quantidade de mensagens para deletar (1-100)')
.setRequired(true)
.setMinValue(1)
.setMaxValue(100)
)
.addStringOption(option =>
option.setName('password')
.setDescription('Digite a senha para limpar mensagens')
.setRequired(true)
);
const listScriptsCommand = new SlashCommandBuilder()
.setName('listar-scripts')
.setDescription('Lista todos os scripts disponíveis');
return [executeCommand, addScriptCommand, clearMessagesCommand, listScriptsCommand];
}
// [ ATUALIZAR CHOICES DO COMANDO ]
// [ENTRADA] - executeCommand: comando para atualizar
// [SAIDA] - Nenhuma
// [OBJETIVO DA FUNÇÃO] - Atualiza opções do comando executar-script com scripts disponíveis
function updateExecuteCommandChoices(executeCommand) {
const scripts = loadScripts();
const choices = Object.keys(scripts).map(scriptName => ({
name: scriptName,
value: scriptName
}));
executeCommand.options[0].choices = choices.slice(0, 25);
}
// [ REGISTRAR COMANDOS ]
// [ENTRADA] - Nenhuma
// [SAIDA] - Promise da operação de registro
// [OBJETIVO DA FUNÇÃO] - Registra comandos slash no Discord
async function registerCommands() {
const commands = createSlashCommands();
updateExecuteCommandChoices(commands[0]);
const commandsJSON = commands.map(command => command.toJSON());
const rest = new REST({ version: '10' }).setToken(BOT_TOKEN);
try {
console.log('Registrando comandos slash...');
if (GUILD_ID) {
await rest.put(
Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID),
{ body: commandsJSON }
);
console.log('Comandos registrados para o servidor específico!');
} else {
await rest.put(
Routes.applicationCommands(CLIENT_ID),
{ body: commandsJSON }
);
console.log('Comandos registrados globalmente!');
}
} catch (error) {
console.error('Erro ao registrar comandos:', error);
}
}
// [ VERIFICAR PERMISSÃO ]
// [ENTRADA] - userId: ID do usuário
// [SAIDA] - boolean indicando se tem permissão
// [OBJETIVO DA FUNÇÃO] - Verifica se usuário tem permissão para usar comandos
function hasPermission(userId) {
return ALLOWED_USERS.length === 0 || ALLOWED_USERS.includes(userId);
}
// [ VERIFICAR SENHA ]
// [ENTRADA] - inputPassword: senha digitada pelo usuário
// [SAIDA] - boolean indicando se senha está correta
// [OBJETIVO DA FUNÇÃO] - Valida senha de acesso aos comandos
function isValidPassword(inputPassword) {
return inputPassword === PASSWORD;
}
// [ MANIPULAR COMANDO EXECUTAR SCRIPT ]
// [ENTRADA] - interaction: interação do Discord
// [SAIDA] - Promise da operação
// [OBJETIVO DA FUNÇÃO] - Processa comando de execução de script
async function handleExecuteScript(interaction) {
const selectedScript = interaction.options.getString('script');
const inputPassword = interaction.options.getString('password');
if (!isValidPassword(inputPassword)) {
await interaction.reply({
content: ERROR_MESSAGES.WRONG_PASSWORD,
ephemeral: true
});
return;
}
AVAILABLE_SCRIPTS = loadScripts();
if (!AVAILABLE_SCRIPTS[selectedScript]) {
await interaction.reply({
content: ERROR_MESSAGES.SCRIPT_NOT_FOUND,
ephemeral: true
});
return;
}
const scriptPath = AVAILABLE_SCRIPTS[selectedScript];
await interaction.deferReply();
try {
console.log(`Usuário ${interaction.user.tag} (${interaction.user.id}) executando script: ${selectedScript}`);
const { result, logFile } = await executeScriptWithUpdates(scriptPath, interaction, selectedScript);
const files = [];
if (logFile && fs.existsSync(logFile)) {
files.push(new AttachmentBuilder(logFile));
}
await interaction.editReply({
content: DEPLOY_STAGES.SUCCESS(selectedScript),
files: files
});
if (logFile && fs.existsSync(logFile)) {
setTimeout(() => {
try {
fs.unlinkSync(logFile);
} catch (err) {
console.error('Erro ao deletar log temporário:', err);
}
}, 5000);
}
} catch (error) {
console.error('Erro ao executar script:', error);
const files = [];
if (error.logFile && fs.existsSync(error.logFile)) {
files.push(new AttachmentBuilder(error.logFile));
}
await interaction.editReply({
content: DEPLOY_STAGES.ERROR(selectedScript, error.error),
files: files
});
if (error.logFile && fs.existsSync(error.logFile)) {
setTimeout(() => {
try {
fs.unlinkSync(error.logFile);
} catch (err) {
console.error('Erro ao deletar log temporário:', err);
}
}, 5000);
}
}
}
// [ MANIPULAR COMANDO ADICIONAR SCRIPT ]
// [ENTRADA] - interaction: interação do Discord
// [SAIDA] - Promise da operação
// [OBJETIVO DA FUNÇÃO] - Processa comando de adição de novo script
async function handleAddScript(interaction) {
const scriptName = interaction.options.getString('nome');
const scriptPath = interaction.options.getString('caminho');
const inputPassword = interaction.options.getString('password');
if (!isValidPassword(inputPassword)) {
await interaction.reply({
content: ERROR_MESSAGES.WRONG_PASSWORD,
ephemeral: true
});
return;
}
if (!scriptName.endsWith('.sh')) {
await interaction.reply({
content: ERROR_MESSAGES.INVALID_SCRIPT_NAME,
ephemeral: true
});
return;
}
const currentScripts = loadScripts();
if (currentScripts[scriptName]) {
await interaction.reply({
content: ERROR_MESSAGES.SCRIPT_EXISTS(scriptName),
ephemeral: true
});
return;
}
currentScripts[scriptName] = scriptPath;
if (saveScripts(currentScripts)) {
AVAILABLE_SCRIPTS = currentScripts;
registerCommands();
await interaction.reply({
content: SUCCESS_MESSAGES.SCRIPT_ADDED(scriptName, scriptPath),
ephemeral: true
});
console.log(`Script adicionado: ${scriptName} -> ${scriptPath}`);
} else {
await interaction.reply({
content: ERROR_MESSAGES.SAVE_ERROR,
ephemeral: true
});
}
}
// [ MANIPULAR COMANDO LIMPAR MENSAGENS ]
// [ENTRADA] - interaction: interação do Discord
// [SAIDA] - Promise da operação
// [OBJETIVO DA FUNÇÃO] - Processa comando de limpeza de mensagens
async function handleClearMessages(interaction) {
const quantidade = interaction.options.getInteger('quantidade');
const inputPassword = interaction.options.getString('password');
if (!isValidPassword(inputPassword)) {
await interaction.reply({
content: '❌ Senha incorreta.',
ephemeral: true
});
return;
}
await interaction.deferReply({ ephemeral: true });
try {
const messages = await interaction.channel.messages.fetch({
limit: quantidade
});
const deletedMessages = await interaction.channel.bulkDelete(messages, true);
await interaction.editReply({
content: `🧹 **${deletedMessages.size}** mensagens foram deletadas com sucesso!`
});
console.log(`${interaction.user.tag} deletou ${deletedMessages.size} mensagens no canal ${interaction.channel.name}`);
} catch (error) {
console.error('Erro ao limpar mensagens:', error);
await interaction.editReply({
content: '❌ Erro ao limpar mensagens. Verifique se as mensagens não são muito antigas (14+ dias) ou se o bot tem permissões adequadas.'
});
}
}
// [ MANIPULAR COMANDO LISTAR SCRIPTS ]
// [ENTRADA] - interaction: interação do Discord
// [SAIDA] - Promise da operação
// [OBJETIVO DA FUNÇÃO] - Processa comando de listagem de scripts
async function handleListScripts(interaction) {
const currentScripts = loadScripts();
const scriptList = Object.entries(currentScripts)
.map(([name, path]) => `• **${name}**: \`${path}\``)
.join('\n');
const embed = {
title: '📋 Scripts Disponíveis',
description: scriptList || 'Nenhum script encontrado.',
color: 0x00ff00,
footer: {
text: `Total: ${Object.keys(currentScripts).length} scripts`
}
};
await interaction.reply({
embeds: [embed],
ephemeral: true
});
}
client.once('ready', () => {
console.log(`Bot conectado como ${client.user.tag}!`);
AVAILABLE_SCRIPTS = loadScripts();
registerCommands();
});
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
const { commandName, user } = interaction;
if (!hasPermission(user.id)) {
await interaction.reply({
content: '❌ Você não tem permissão para executar este comando.',
ephemeral: true
});
return;
}
switch (commandName) {
case 'executar-script':
await handleExecuteScript(interaction);
break;
case 'adicionar-script':
await handleAddScript(interaction);
break;
case 'limpar-mensagens':
await handleClearMessages(interaction);
break;
case 'listar-scripts':
await handleListScripts(interaction);
break;
}
});
client.on('messageCreate', async message => {
if (message.author.bot) return;
if (message.content.startsWith('!executar-script')) {
await message.reply('ℹ️ Use o comando slash `/executar-script` para uma melhor experiência!');
}
});
client.on('error', error => {
console.error('Erro no cliente Discord:', error);
});
process.on('unhandledRejection', error => {
console.error('Erro não tratado:', error);
});
client.login(BOT_TOKEN);