aboutsummaryrefslogtreecommitdiff
path: root/bot/src/commands/fetch.js
blob: 651fb06f27a3e0f9392a9eaf49fb75398e168c23 (plain) (blame)
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
import { AttachmentBuilder, SlashCommandBuilder } from 'discord.js';

export default {
    data: new SlashCommandBuilder()
        .setName('fetch')
        .setDescription('Fetches JSON data.')
        .addStringOption(option =>
            option
                .setName('url')
                .setDescription('Enter the URL you want to fetch.')
                .setRequired(true)),
    execute(interaction) {
        const url = interaction.options.getString('url');
        fetch(url)
            .then(response => response.json())
            .then(async data => {
                const dataString = JSON.stringify(data, null, 2);
                if (dataString.length === 0) {
                    return await interaction.reply('Received an empty response.');
                }

                if (dataString.length > 2000) {
                    const attachment = new AttachmentBuilder(Buffer.from(dataString, 'utf-8'), { name: 'messages.json' });
                    return await interaction.reply({ files: [attachment] });
                }

                return await interaction.reply(`\`\`\`json\n${dataString}\n\`\`\``);
            })
            .catch(async error => {
                console.error(error);
                return await interaction.reply(`An error occurred while fetching the URL data.\n\`\`\`js\n${error.stack}\`\`\``);
            });
    }
};