From 835b56f31104300c84669ef37a1e0715cd4a18dc Mon Sep 17 00:00:00 2001 From: lucmsilva651 Date: Wed, 11 Sep 2024 17:50:33 -0300 Subject: [PATCH] TMA module downloader --- commands/modarchive.js | 73 +++++++++++++++++++++++++++++++++++++++++ locales/english.json | 8 +++-- locales/portuguese.json | 8 +++-- 3 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 commands/modarchive.js diff --git a/commands/modarchive.js b/commands/modarchive.js new file mode 100644 index 0000000..d092067 --- /dev/null +++ b/commands/modarchive.js @@ -0,0 +1,73 @@ +const axios = require('axios'); +const fs = require('fs'); +const path = require('path'); +const { getStrings } = require('../plugins/checklang.js'); +const { isOnSpamWatch } = require('../plugins/lib-spamwatch/spamwatch.js'); +const spamwatchMiddleware = require('../plugins/lib-spamwatch/Middleware.js')(isOnSpamWatch); + +async function downloadModule(moduleId) { + try { + const downloadUrl = `https://api.modarchive.org/downloads.php?moduleid=${moduleId}`; + const response = await axios({ + url: downloadUrl, + method: 'GET', + responseType: 'stream', + }); + + const disposition = response.headers['content-disposition']; + let fileName = moduleId; + + if (disposition && disposition.includes('filename=')) { + fileName = disposition + .split('filename=')[1] + .split(';')[0] + .replace(/['"]/g, ''); + } + + const filePath = path.resolve(__dirname, fileName); + + const writer = fs.createWriteStream(filePath); + response.data.pipe(writer); + + return new Promise((resolve, reject) => { + writer.on('finish', () => resolve({ filePath, fileName })); + writer.on('error', reject); + }); + } catch (error) { + return null; + } +} + +module.exports = (bot) => { + bot.command(['modarchive', 'tma'], spamwatchMiddleware, async (ctx) => { + const Strings = getStrings(ctx.from.language_code); + const args = ctx.message.text.split(' '); + + if (args.length !== 2) { + return ctx.reply(Strings.maInvalidModule, { + parse_mode: "Markdown", + reply_to_message_id: ctx.message.message_id + }); + } + + const moduleId = args[1]; + + const result = await downloadModule(moduleId); + + if (result) { + const { filePath, fileName } = result; + + await ctx.replyWithDocument({ + source: filePath, + caption: fileName, + }); + + fs.unlinkSync(filePath); + } else { + ctx.reply(Strings.maDownloadError, { + parse_mode: "Markdown", + reply_to_message_id: ctx.message.message_id + }); + } + }); +}; diff --git a/locales/english.json b/locales/english.json index 5bdae39..ab6d7e3 100644 --- a/locales/english.json +++ b/locales/english.json @@ -46,10 +46,12 @@ "mainCommands": "Main commands", "mainCommandsDesc": "*Main commands*\n\n- /help: Show bot's help\n- /start: Start the bot\n- /privacy: Read the bot's Privacy Policy", "usefulCommands": "Useful commands", - "usefulCommandsDesc": "*Useful commands*\n\n- /chatinfo: Send information about the group\n- /userinfo: Send information about yourself\n- /d | /device ``: Search for a device on GSMArena and show its specs.\n- /lt | /lmu | /last | /lfm ``: Show the last song from a specified Last.fm profile + number of plays.\n- /weather | /clima `city`: See weather status for a specific location.", + "usefulCommandsDesc": "*Useful commands*\n\n- /chatinfo: Send information about the group\n- /userinfo: Send information about yourself\n- /d | /device ``: Search for a device on GSMArena and show its specs.\n- /lt | /lmu | /last | /lfm ``: Show the last song from a specified Last.fm profile + number of plays.\n- /weather | /clima ``: See weather status for a specific location.\n- /modarchive | /tma ``: Download a module from The Mod Archive", "funnyCommands": "Funny commands", "funnyCommandsDesc": "*Funny commands*\n\n- /gay: Check if you are gay\n- /furry: Check if you are a furry\n- /random: Pick a random number between 0-10", "interactiveEmojis": "Interactive emojis", "interactiveEmojisDesc": "*Interactive emojis*\n\n- /dice: Roll a dice\n- /idice: Infinitely roll a colored dice\n- /slot: Try to combine the figures!\n- /ball: Try to kick the ball into the goal!\n- /bowling: Try to hit the pins!\n- /dart: Try to hit the target!", - "goBack": "Back" -} + "goBack": "Back", + "maInvalidModule": "Please provide a valid module ID from The Mod Archive.\nExample: `/modarchive 81574`", + "maDownloadError": "Error downloading the file. Check the module ID and try again." +} \ No newline at end of file diff --git a/locales/portuguese.json b/locales/portuguese.json index 7ecfe0c..8f01d19 100644 --- a/locales/portuguese.json +++ b/locales/portuguese.json @@ -46,10 +46,12 @@ "mainCommands": "Comandos principais", "mainCommandsDesc": "*Comandos principais*\n\n- /help: Exibe a ajuda do bot\n- /start: Inicia o bot\n- /privacy: Leia a política de privacidade do bot", "usefulCommands": "Comandos úteis", - "usefulCommandsDesc": "*Comandos úteis*\n\n- /chatinfo: Envia informações sobre o grupo\n- /userinfo: Envia informações sobre você\n- /d | /device ``: Pesquisa um dispositivo no GSMArena e mostra suas especificações.\n- /lt | /lmu | /last | /lfm ``: Mostra a última música de um perfil especificado no Last.fm + o número de reproduções.\n- /weather | /clima `cidade`: Veja o status do clima para uma localização específica", + "usefulCommandsDesc": "*Comandos úteis*\n\n- /chatinfo: Envia informações sobre o grupo\n- /userinfo: Envia informações sobre você\n- /d | /device ``: Pesquisa um dispositivo no GSMArena e mostra suas especificações.\n- /lt | /lmu | /last | /lfm ``: Mostra a última música de um perfil especificado no Last.fm + o número de reproduções.\n- /weather | /clima ``: Veja o status do clima para uma localização específica\n- /modarchive | /tma ``: Baixa um módulo do The Mod Archive", "funnyCommands": "Comandos engraçados", "funnyCommandsDesc": "*Comandos engraçados*\n\n- /gay: Verifique se você é gay\n- /furry: Verifique se você é furry\n- /random: Escolhe um número aleatório entre 0-10", "interactiveEmojis": "Emojis interativos", "interactiveEmojisDesc": "*Emojis interativos*\n\n- /dice: Jogue um dado\n- /idice: Role infinitamente um dado colorido\n- /slot: Tente combinar as figuras!\n- /ball: Tente chutar a bola no gol!\n- /bowling: Tente derrubar os pinos!\n- /dart: Tente acertar o alvo!", - "goBack": "Voltar" -} + "goBack": "Voltar", + "maInvalidModule": "Por favor, forneça um ID de módulo válido do The Mod Archive.\nExemplo: `/modarchive 81574`", + "maDownloadError": "Erro ao baixar o arquivo. Verifique o ID do módulo e tente novamente." +} \ No newline at end of file