aboutsummaryrefslogtreecommitdiff
path: root/index.js
blob: f2066abe97315f95ede3b763cbcc302720c50318 (plain)
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
// https://discord.com/oauth2/authorize?client_id=928695013083316295&scope=bot%20applications.commands&permissions=8

const fs = require('fs');
const { REST } = require('@discordjs/rest');
const { SlashCommandBuilder } = require('@discordjs/builders');
const { Routes } = require('discord-api-types/v9');

const rest = new REST({ version: '9' }).setToken(fs.readFileSync("./token.txt").toString());
const { Client, Intents, MessageActionRow, MessageButton, MessageSelectMenu, MessageEmbed } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

const getResult = require('./result');
const getEmbed = require('./embed');

let langs = JSON.parse(fs.readFileSync("./userdata.json"));

global.l = (en, fr, id) => {
    if (langs[id] === "fr") {
        return fr;
    } else {
        return en;
    }
}

const commands = [
    new SlashCommandBuilder()
        .setName('pony')
        .setDescription("Searches for a pony")
        .addStringOption(option =>
            option.setName("query")
                .setDescription("The pony to search for")
                .setRequired(true)
        ),
    new SlashCommandBuilder()
        .setName('lang')
        .setDescription("Changes the bot's language")
        .addStringOption(option =>
            option.setName('locale')
                .setDescription('The selected language')
                .setRequired(true)
                .addChoice('Français', 'fr')
                .addChoice('English', 'en')
        )
];

client.on('ready', async () => {
    console.log(`Logged in as ${client.user.tag}!`);
    for (let guild of client.guilds.cache.map(g => g.id)) {
        try {
            console.log('Started refreshing application (/) commands for guild ' + guild + '.');

            await rest.put(
                Routes.applicationGuildCommands(fs.readFileSync("./client.txt").toString(), guild),
                { body: commands },
            );

            console.log('Successfully reloaded application (/) commands for guild ' + guild + '.');
        } catch (error) {
            console.error(error);
        }
    }
});

client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;

    try {
        if (interaction.commandName === 'lang') {
            lang = interaction.options.getString('locale');
            if (lang === "fr") {
                langs[interaction.user.id] = "fr";
            } else {
                langs[interaction.user.id] = "en";
            }
            fs.writeFileSync("./userdata.json", JSON.stringify(langs, null, 4));
            await interaction.reply({
                ephemeral: false,
                embeds: [
                    new MessageEmbed()
                        .setColor('#28dc46')
                        .setTitle(l("Language settings", "Paramètres de langue", interaction.user.id))
                        .setDescription(l("Your preferred language is now **English**.", "Votre langue principale est maintenant le **français**.", interaction.user.id))
                ]
            });
        }

        if (interaction.commandName === 'pony') {
            query = interaction.options.getString('query');
            result = getResult(query, interaction.user.id);

            if (result.results.length > 0 && getEmbed(result.results[0], interaction.user.id) !== false) {
                await interaction.reply({
                    ephemeral: false,
                    embeds: [
                        getEmbed(result.results[0], interaction.user.id)
                    ],
                    components: [
                        new MessageActionRow()
                            .addComponents(
                                new MessageButton()
                                    .setLabel(l("Read More", "Lire plus", interaction.user.id))
                                    .setStyle("LINK")
                                    .setURL("https://mlp.fandom.com/wiki/" + encodeURI(result.results[0])),
                                new MessageButton()
                                    .setCustomId("pixel")
                                    .setLabel("Pixel Art")
                                    .setStyle("SECONDARY"),
                                new MessageButton()
                                    .setCustomId("report")
                                    .setLabel(l("Report an issue", "Signaler un problème", interaction.user.id))
                                    .setStyle("DANGER")
                            )
                    ]
                });
            } else {
                await interaction.reply({
                    ephemeral: false,
                    embeds: [
                        new MessageEmbed()
                            .setColor('#dc2828')
                            .setTitle(l("Results for", "Résultats pour", interaction.user.id) + " \"" + query + "\"")
                            .setDescription(l("No results found. Please try with other keywords.", "Aucun résultat trouvé. Essayez avec d'autres mots clés.", interaction.user.id))
                    ],
                    components: [
                    new MessageActionRow()
                        .addComponents(
                            new MessageButton()
                                .setCustomId("pixel")
                                .setLabel(l("Suggest a missing pony", "Proposer un poney manquant", interaction.user.id))
                                .setStyle("SECONDARY"),
                            new MessageButton()
                                .setCustomId("report")
                                .setLabel(l("Report an issue", "Signaler un problème", interaction.user.id))
                                .setStyle("DANGER")
                        )
                ]
                });
            }
        }
    } catch (e) {
        try {
            await interaction.reply({
                ephemeral: false,
                embeds: [
                    new MessageEmbed()
                        .setColor('#dc2828')
                        .setTitle(l("An internal exception occurred", "Une erreur interne s'est produite", interaction.user.id))
                        .setDescription("```\n" + e.stack + "\n```")
                ],
                components: [
                    new MessageActionRow()
                        .addComponents(
                            new MessageButton()
                                .setCustomId("report")
                                .setLabel(l("Send bug report", "Envoyer un rapport de bug", interaction.user.id))
                                .setStyle("DANGER")
                        )
                ]
            });
        } catch (e) {
            await interaction.reply({
                ephemeral: false,
                embeds: [
                    new MessageEmbed()
                        .setColor('#dc2828')
                        .setTitle("An internal exception occurred")
                        .setDescription("```\n" + e.stack + "\n```")
                ],
                components: [
                    new MessageActionRow()
                        .addComponents(
                            new MessageButton()
                                .setCustomId("report")
                                .setLabel("Send bug report")
                                .setStyle("DANGER")
                        )
                ]
            });
        }
    }
});

client.login(fs.readFileSync("./token.txt").toString());