diff --git a/README.md b/README.md index 5a3b7df..820ce78 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,13 @@ réponses troll et fun facts - 0.3.1 : ajout de db (retention de setting) et maj des dependences - 1.0.0 : ca marche via vs code - 1.1.0 : fix sur lxc +- 2.0.0 : ajout des fun facts ## run - npm i +- node deployCommands.js - node index.js +- pm2 start deployCommands.js - pm2 start index.js - pm2 save \ No newline at end of file diff --git a/facts/funFacts.js b/facts/funFacts.js new file mode 100644 index 0000000..8807af8 --- /dev/null +++ b/facts/funFacts.js @@ -0,0 +1,5 @@ +[ + "Les loutres se tiennent la main quand elles dorment.", + "Le miel ne se périme jamais.", + "Les bananes sont radioactives (très légèrement).", +] \ No newline at end of file diff --git a/facts/randomizer.js b/facts/randomizer.js new file mode 100644 index 0000000..7a0a0b1 --- /dev/null +++ b/facts/randomizer.js @@ -0,0 +1,8 @@ +import { createRequire } from "module"; +const require = createRequire(import.meta.url); + +export function getFunFact() { + const facts = require("funfacts.json"); + const randomFact = facts[Math.floor(Math.random() * facts.length)]; + return randomFact; +} \ No newline at end of file diff --git a/index.js b/index.js index d94df79..06a2a92 100644 --- a/index.js +++ b/index.js @@ -1,26 +1,56 @@ import 'dotenv/config'; -import { Client, GatewayIntentBits, Options, Events } from 'discord.js'; +import fs from 'fs'; +import { Client, GatewayIntentBits, Options, Events, Collection } from 'discord.js'; import firstDetector from './detectors/allDetector.js'; + +// --- Configuration Client et Intents --- const client = new Client({ makeCache: Options.cacheWithLimits({ MessageManager: 5, // opti de RAM mais 0 est trop agressif ThreadManager: 0, // peut rester a 0 car peu utilisé - // commentées pour laisser par défaut, pas d'impact vu la RAM dispo // UserManager: 0, //ThreadManager: 0, + // commentées pour laisser par défaut, pas d'impact vu la RAM dispo + }), intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, - GatewayIntentBits.MessageContent, + GatewayIntentBits.MessageContent, // required pour les détecteurs ] }); -client.on('messageCreate', async (message) => { - // DEBUG : Vérifie si le message arrive bien - console.log(`[DEBUG] Message reçu de ${message.author?.tag}: ${message.content}`); +// --- Gestion des Événements --- +// 1. login réussi +client.on(Events.ClientReady, () => { + console.log(`[SUCCESS] Logged in as ${client.user?.tag}`); +}); + +// 2. gestion des commandes slash +client.on(Events.InteractionCreate, async interaction => { + // check que ça soit bien commande slash + if (!interaction.isChatInputCommand()) return; + + const command = client.commands.get(interaction.commandName); + if (!command) return; + + try { + await command.execute(interaction); + } catch (error) { + console.error('Erreur exécution commande slash:', error); + // message d'erreur (si besoin), montré qu'à l'auteur + if (interaction.replied || interaction.deferred) { + await interaction.followUp({ content: 'Erreur lors de l’exécution de la commande !', ephemeral: true }); + } else { + await interaction.reply({ content: 'Erreur lors de l’exécution de la commande !', ephemeral: true }); + } + } +}); + +// 3. Gestion des Messages (Détecteurs Passifs) +client.on('messageCreate', async (message) => { if (message.author.bot) return; try { @@ -31,8 +61,6 @@ client.on('messageCreate', async (message) => { } }); -client.login(process.env.DISCORD_TOKEN); -client.on(Events.ClientReady, () => { - console.log(`[SUCCESS] Logged in as ${client.user?.tag}`); //check co Discord -}); \ No newline at end of file +// --- connexion --- +client.login(process.env.DISCORD_TOKEN); \ No newline at end of file diff --git a/utils/commands.js b/utils/commands.js new file mode 100644 index 0000000..402f332 --- /dev/null +++ b/utils/commands.js @@ -0,0 +1,16 @@ +import { SlashCommandBuilder } from 'discord.js'; +import { getRandomFact } from '../utils/factRandomizer.js'; + +export default { + // définition de la commande pour Discord + data: new SlashCommandBuilder() + .setName('funfact') // correspondra à /funfact + .setDescription('Donne un fait aléatoire et inutile'), + + // action + async execute(interaction) { + const fact = getRandomFact(); + // interaction.reply plutôt que message.reply + await interaction.reply(`💡 **Fun fact : **\n${fact}`); + } +}; \ No newline at end of file diff --git a/utils/deployCommands.js b/utils/deployCommands.js new file mode 100644 index 0000000..11f8515 --- /dev/null +++ b/utils/deployCommands.js @@ -0,0 +1,26 @@ +import 'dotenv/config'; +import { REST, Routes } from 'discord.js'; +import fs from 'fs'; + +const commands = []; +const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); + +for (const file of commandFiles) { + const command = await import(`./commands/${file}`); + commands.push(command.default.data.toJSON()); +} + +const rest = new REST().setToken(process.env.DISCORD_TOKEN); + +(async () => { + try { + console.log('Enregistrement des commandes slash...'); + await rest.put( + Routes.applicationCommands(process.env.CLIENT_ID), + { body: commands }, + ); + console.log('Commandes enregistrées ! Tape /funfact sur Discord.'); + } catch (error) { + console.error(error); + } +})(); \ No newline at end of file