diff --git a/app.js b/app.js index 6e76fcf..3400a06 100644 --- a/app.js +++ b/app.js @@ -219,14 +219,13 @@ bot.command('addmsg', async ctx => { }) //Discord events -client.on('ready', () => { - console.log(`[Discord] Logged in as ${client.user.tag} !`); - client.user.setPresence({ activities: [{ name: 'la belle chaise', type : 3}] }); -}); +discordEvents.ready(client); discordEvents.newMessage(client); discordEvents.newInteraction(client); +discordEvents.guildCreate(client); + bot.launch() client.login(process.env.DISCORD); \ No newline at end of file diff --git a/register-commands-exemple.js b/commands/commands.js similarity index 83% rename from register-commands-exemple.js rename to commands/commands.js index ff79a0f..5542670 100644 --- a/register-commands-exemple.js +++ b/commands/commands.js @@ -1,4 +1,4 @@ -const {REST , Routes, ApplicationCommandOptionType} = require('discord.js'); +const {ApplicationCommandOptionType} = require('discord.js'); const commands = [ { @@ -102,21 +102,11 @@ const commands = [ name : 'github', description : 'Get the github link of the bot', }, + + { + name : 'servers', + description : 'Get the number of servers the bot is in', + }, ]; -const rest = new REST({ version: '10' }).setToken(process.env.DISCORD); - -(async () => { - try { - console.log('Started refreshing application (/) commands.'); - - await rest.put( - Routes.applicationGuildCommands('', ''), - { body: commands }, - ); - - console.log('Successfully reloaded application (/) commands.'); - } catch (error) { - console.error(error); - } -})(); \ No newline at end of file +module.exports = { commands }; \ No newline at end of file diff --git a/events/discordEvents.js b/events/discordEvents.js index 2f29233..6293374 100644 --- a/events/discordEvents.js +++ b/events/discordEvents.js @@ -4,6 +4,8 @@ const { addToLogs } = require('../libs/botTools'); const { generateImage, answerQuestion, sendConv } = require('../libs/openAi'); const { incrementQuota, addConv, delConv, getConvs, addMessage, getMessages, isNewUser } = require('../libs/mysql'); +const { commands } = require('../commands/commands'); + async function gptrequest(interaction) { await interaction.deferReply(); @@ -323,6 +325,14 @@ async function github(interation) { console.log('[Discord] Github requested by ' + interaction.member.user.username); } + +async function servers(client) { + console.log("Serveurs:"); + client.guilds.cache.forEach((guild) => { + console.log(" - " + guild); + }) +} + module.exports = { newMessage: (client) => { client.on('messageCreate', async msg => { @@ -384,6 +394,49 @@ module.exports = { else if (interaction.commandName === 'github') { github(interaction); } + + else if (interaction.commandName === 'servers') { + servers(client); + } }); - } + }, + + ready: (client) => { + client.on('ready', () => { + console.log(`[Discord] Logged in as ${client.user.tag} !`); + client.user.setPresence({ activities: [{ name: 'la belle chaise', type: 3 }] }); + + const rest = new discord.REST({ version: '10' }).setToken(process.env.DISCORD); + + client.guilds.cache.forEach(async (guild) => { + try { + await rest.put( + discord.Routes.applicationGuildCommands('1059559067846189067', guild.id), + { body: commands }, + ); + + console.log('[Discord] Successfully reloaded application (/) commands for ' + guild.name + '.'); + } catch (error) { + console.error(error); + } + }) + }); + }, + + guildCreate: (client) => { + client.on('guildCreate', async (guild) => { + const rest = new discord.REST({ version: '10' }).setToken(process.env.DISCORD); + + try { + await rest.put( + discord.Routes.applicationGuildCommands('1059559067846189067', guild.id), + { body: commands }, + ); + + console.log('[Discord] Successfully reloaded application (/) commands for ' + guild.name + '.'); + } catch (error) { + console.error(error); + } + }); + }, } \ No newline at end of file