From 3baee6b61804ff5a6aa764adfd51f1008d9eb2e1 Mon Sep 17 00:00:00 2001 From: Lucas Gabriel Date: Sat, 28 Sep 2024 19:30:13 -0300 Subject: [PATCH] Add command for updating the bot --- commands/crew.js | 51 +++++++++++++++++++++++++++++++++++------ locales/english.json | 5 +++- locales/portuguese.json | 5 +++- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/commands/crew.js b/commands/crew.js index 3727f3f..91a739a 100644 --- a/commands/crew.js +++ b/commands/crew.js @@ -17,6 +17,18 @@ function getGitCommitHash() { }); } +function updateBot() { + return new Promise((resolve, reject) => { + exec('git pull', (error, stdout, stderr) => { + if (error) { + reject(`Error: ${stderr}`); + } else { + resolve(stdout.trim()); + } + }); + }); +} + function formatUptime(uptime) { const hours = Math.floor(uptime / 3600); const minutes = Math.floor((uptime % 3600) / 60); @@ -91,12 +103,30 @@ module.exports = (bot) => { }, '', Strings.errorRetrievingCommit); }); + bot.command('updatebot', spamwatchMiddleware, async (ctx) => { + const Strings = getStrings(ctx.from.language_code); + handleAdminCommand(ctx, async () => { + try { + await ctx.reply(Strings.botUpdated, { + parse_mode: 'Markdown', + reply_to_message_id: ctx.message.message_id + }); + updateBot(); + } catch (error) { + ctx.reply(Strings.errorUpdatingBot.replace('{error}', error), { + parse_mode: 'Markdown', + reply_to_message_id: ctx.message.message_id + }); + } + }, '', Strings.errorUpdatingBot); + }); + bot.command('setbotname', spamwatchMiddleware, async (ctx) => { const Strings = getStrings(ctx.from.language_code); const botName = ctx.message.text.split(' ').slice(1).join(' '); handleAdminCommand(ctx, async () => { await ctx.telegram.setMyName(botName); - }, Strings.botNameChanged.replace('{botName}', botName), Strings.botNameErr.replace('{tgErr}', '{error}')); + }, Strings.botNameChanged.replace('{botName}', botName), Strings.botNameErr.replace('{error}', '{error}')); }); bot.command('setbotdesc', spamwatchMiddleware, async (ctx) => { @@ -104,7 +134,7 @@ module.exports = (bot) => { const botDesc = ctx.message.text.split(' ').slice(1).join(' '); handleAdminCommand(ctx, async () => { await ctx.telegram.setMyDescription(botDesc); - }, Strings.botDescChanged.replace('{botDesc}', botDesc), Strings.botDescErr.replace('{tgErr}', '{error}')); + }, Strings.botDescChanged.replace('{botDesc}', botDesc), Strings.botDescErr.replace('{error}', '{error}')); }); bot.command('botkickme', spamwatchMiddleware, async (ctx) => { @@ -119,13 +149,20 @@ module.exports = (bot) => { }); bot.command('getfile', spamwatchMiddleware, async (ctx) => { + const Strings = getStrings(ctx.from.language_code); const botFile = ctx.message.text.split(' ').slice(1).join(' '); handleAdminCommand(ctx, async () => { - try{ - await ctx.replyWithDocument({source: botFile}); - }catch (error){ - console.log('ERROR'); + try { + await ctx.replyWithDocument({ + source: botFile, + caption: botFile + }); + } catch (error) { + ctx.reply(Strings.fileError.replace('{error}', error.message), { + parse_mode: 'Markdown', + reply_to_message_id: ctx.message.message_id + }); } - }); + }, '', Strings.fileError); }); }; diff --git a/locales/english.json b/locales/english.json index 5ada41d..22c549f 100644 --- a/locales/english.json +++ b/locales/english.json @@ -62,5 +62,8 @@ "ytUploading": "*Uploading video...*", "ytUploadDesc": "*[{userName}](tg://user?id={userId}), there is your downloaded video.*", "ytDownloadErr": "*Error during YT video download:*\n\n`{err}`", - "ytUploadLimit": "*You have reached the 50MB upload limit for the bot. Try another video. We're trying our best to increase the limit.*" + "ytUploadLimit": "*You have reached the 50MB upload limit for the bot. Try another video. We're trying our best to increase the limit.*", + "fileError": "Error uploading file", + "botUpdated": "Bot updated with success.", + "errorUpdatingBot": "Error updating bot\n\n{error}" } \ No newline at end of file diff --git a/locales/portuguese.json b/locales/portuguese.json index 71a933f..7bf80ac 100644 --- a/locales/portuguese.json +++ b/locales/portuguese.json @@ -62,5 +62,8 @@ "ytUploading": "*Enviando video...*", "ytUploadDesc": "*[{userName}](tg://user?id={userId}), aqui está o seu vídeo baixado.*", "ytDownloadErr": "*Erro durante o download do vídeo do YT:*\n\n`{err}`", - "ytUploadLimit": "*Você atingiu o limite de upload de 50MB para o bot. Tente outro vídeo. Estamos tentando o máximo para aumentar o limite.*" + "ytUploadLimit": "*Você atingiu o limite de upload de 50MB para o bot. Tente outro vídeo. Estamos tentando o máximo para aumentar o limite.*", + "fileError": "Erro ao enviar o arquivo", + "botUpdated": "Bot atualizado com sucesso.", + "errorUpdatingBot": "Erro ao atualizar o bot\n\n{error}" } \ No newline at end of file