diff options
Diffstat (limited to 'index.js')
-rw-r--r-- | index.js | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/index.js b/index.js new file mode 100644 index 0000000..f2066ab --- /dev/null +++ b/index.js @@ -0,0 +1,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());
\ No newline at end of file |