From aa1811a9f17e01b3ad6eb7fa3a93692b081bfaa6 Mon Sep 17 00:00:00 2001 From: VALOU3336 Date: Sat, 17 Feb 2024 08:31:02 +0100 Subject: [PATCH] Add greet System --- commands/gestion/greet.js | 38 +++++++++++++++++++++++++++++++++++ commands/gestion/greetlist.js | 28 ++++++++++++++++++++++++++ events/gestion/greet.js | 35 ++++++++++++++++++++++++++++++++ permissions.json | 4 +++- 4 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 commands/gestion/greet.js create mode 100644 commands/gestion/greetlist.js create mode 100644 events/gestion/greet.js diff --git a/commands/gestion/greet.js b/commands/gestion/greet.js new file mode 100644 index 0000000..9ecedba --- /dev/null +++ b/commands/gestion/greet.js @@ -0,0 +1,38 @@ +const { EmbedBuilder } = require('discord.js'); +const db = require('quick.db'); +const GestionDb = new db.table('gestion'); + +module.exports = { + name: 'greet', + description: 'Ajoute ou supprime un salon des greet', + async execute(message, args) { + const botId = message.client.user.id; + const guildId = message.guild.id; + const guildGreetings = GestionDb.get(`${botId}.${guildId}.greetings`) || []; + + const channelId = args[0] ? args[0].replace(/<#|>/g, '') : message.channel.id; + + const index = guildGreetings.findIndex(greeting => greeting.channelId === channelId); + + if (index !== -1) { + guildGreetings.splice(index, 1); + GestionDb.set(`${botId}.${guildId}.greetings`, guildGreetings); + const embed = new EmbedBuilder() + .setDescription(`Le salon <#${channelId}> a été retiré de la liste des salutations.`) + .setColor('#0099ff'); + message.channel.send({ embeds: [embed] }); + } else if (guildGreetings.length < 5) { + guildGreetings.push({ channelId }); + GestionDb.set(`${botId}.${guildId}.greetings`, guildGreetings); + const embed = new EmbedBuilder() + .setDescription(`Le salon <#${channelId}> a été ajouté à la liste des salutations.`) + .setColor('#0099ff'); + message.channel.send({ embeds: [embed] }); + } else { + const embed = new EmbedBuilder() + .setDescription('Il y a déjà 5 salons dans la liste des salutations. Vous ne pouvez pas en ajouter d\'autres.') + .setColor('#ff0000'); + message.channel.send({ embeds: [embed] }); + } + }, +}; \ No newline at end of file diff --git a/commands/gestion/greetlist.js b/commands/gestion/greetlist.js new file mode 100644 index 0000000..2868aa9 --- /dev/null +++ b/commands/gestion/greetlist.js @@ -0,0 +1,28 @@ +const { EmbedBuilder } = require('discord.js'); +const db = require('quick.db'); +const GestionDb = new db.table('gestion'); + +module.exports = { + name: 'greetlist', + description: 'Affiche la liste des salons avec un salutation', + async execute(message) { + const botId = message.client.user.id; + const guildId = message.guild.id; + const guildGreetings = GestionDb.get(`${botId}.${guildId}.greetings`) || []; + + if (guildGreetings.length === 0) { + const embed = new EmbedBuilder() + .setTitle('Aucun salon avec un salutation') + .setDescription('Il n\'y a aucun salon avec un salutation pour ce serveur.') + .setColor('#ff0000'); + message.channel.send({ embeds: [embed] }); + } else { + const greetingChannels = guildGreetings.map(greeting => `<#${greeting.channelId}>`).join(', '); + const embed = new EmbedBuilder() + .setTitle(`Salons avec un greet ${guildGreetings.length}/5`) + .setDescription(`${greetingChannels}`) + .setColor('#0099ff'); + message.channel.send({ embeds: [embed] }); + } + }, +}; \ No newline at end of file diff --git a/events/gestion/greet.js b/events/gestion/greet.js new file mode 100644 index 0000000..79cce50 --- /dev/null +++ b/events/gestion/greet.js @@ -0,0 +1,35 @@ +const db = require('quick.db'); +const GestionDb = new db.table('gestion'); +const {Events} = require("discord.js") + +module.exports = { + name: Events.GuildMemberAdd , + async execute(member) { + const botId = member.client.user.id; + const guildId = member.guild.id; + const guildGreetings = GestionDb.get(`${botId}.${guildId}.greetings`) || []; + + if (guildGreetings.length > 0) { + const welcomeMessage = `Bienvenue <@${member.id}> sur le serveur ${member.guild.name} !`; + const messagePromises = []; + for (const greeting of guildGreetings) { + try { + const channel = await member.guild.channels.fetch(greeting.channelId); + if (channel && channel.type === 0) { + messagePromises.push( + channel.send(welcomeMessage) + .then(message => { + setTimeout(() => message.delete(), 1000); + }) + ); + } + } catch (error) { + } + } + try { + await Promise.all(messagePromises); + } catch (error) { + } + } + }, +}; \ No newline at end of file diff --git a/permissions.json b/permissions.json index 2cc0e63..12f9458 100644 --- a/permissions.json +++ b/permissions.json @@ -116,5 +116,7 @@ "bl": 10, "unbl": 10, "stat": 10, - "random": 0 + "random": 0, + "greet": 10, + "greetlist": 10 } \ No newline at end of file