From 4b27df58a491b3d6e25f806ef3bd7d0c53b161ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Baudrin?= Date: Fri, 15 Dec 2023 16:58:34 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20add=20options=20for=20logging?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/dixt-plugin-logs/src/index.ts | 289 ++++++++++++++++--------- 1 file changed, 181 insertions(+), 108 deletions(-) diff --git a/packages/dixt-plugin-logs/src/index.ts b/packages/dixt-plugin-logs/src/index.ts index fd2d184..76154d4 100644 --- a/packages/dixt-plugin-logs/src/index.ts +++ b/packages/dixt-plugin-logs/src/index.ts @@ -14,10 +14,58 @@ export type DixtPluginLogsOptions = { webhookUrl?: string; name?: string; avatarUrl?: string; + logging?: { + message?: { + delete?: boolean; + edit?: boolean; + react?: { + add?: boolean; + remove?: boolean; + }; + }; + guild?: { + join?: boolean; + leave?: boolean; + roleChange?: boolean; + }; + voice?: { + join?: boolean; + leave?: boolean; + deafen?: boolean; + }; + member?: { + nicknameChange?: boolean; + slashCommand?: boolean; + }; + }; }; -export const optionsDefaults = { +export const optionsDefaults: DixtPluginLogsOptions = { webhookUrl: process.env.DIXT_PLUGIN_LOGS_WEBHOOK_URL || "", + logging: { + message: { + delete: true, + edit: true, + react: { + add: true, + remove: true, + }, + }, + guild: { + join: true, + leave: true, + roleChange: true, + }, + voice: { + join: true, + leave: true, + deafen: true, + }, + member: { + nicknameChange: true, + slashCommand: true, + }, + }, }; const embedEmojis: { @@ -82,134 +130,159 @@ const dixtPluginLogs: DixtPlugin = ( }); // handle when a guild member delete a message - instance.client.on(Events.MessageDelete, (message) => { - if (message.author?.bot) return; - Log.warn( - `**${message.guild}** - ${message.author} deleted a message in ${message.channel}:\n${message.cleanContent}`, - ); - }); + if (optionsValue?.logging?.message?.delete) { + instance.client.on(Events.MessageDelete, (message) => { + if (message.author?.bot) return; + Log.warn( + `**${message.guild}** - ${message.author} deleted a message in ${message.channel}:\n${message.cleanContent}`, + ); + }); + } // handle when a guild member edit a message - instance.client.on(Events.MessageUpdate, (oldMessage, newMessage) => { - if (oldMessage.author?.bot) return; + if (optionsValue?.logging?.message?.edit) { + instance.client.on(Events.MessageUpdate, (oldMessage, newMessage) => { + if (oldMessage.author?.bot) return; - if ((oldMessage.channel as TextChannel)?.name.startsWith("access")) return; - Log.warn( - `**${oldMessage.guild}** - **${oldMessage.author}** edited a message in <#${oldMessage.channel.id}>:\n${oldMessage.cleanContent}\n⬇️\n${newMessage.cleanContent}`, - ); - }); + if ((oldMessage.channel as TextChannel)?.name.startsWith("access")) + return; + Log.warn( + `**${oldMessage.guild}** - **${oldMessage.author}** edited a message in <#${oldMessage.channel.id}>:\n${oldMessage.cleanContent}\n⬇️\n${newMessage.cleanContent}`, + ); + }); + } // handle when a guild member join the server - instance.client.on(Events.GuildMemberAdd, (member) => { - Log.info(`**${member.guild}** - ${member} has joined the server`); - }); + if (optionsValue?.logging?.guild?.join) { + instance.client.on(Events.GuildMemberAdd, (member) => { + Log.info(`**${member.guild}** - ${member} has joined the server`); + }); + } // handle when a guild member leave the server - instance.client.on(Events.GuildMemberRemove, (member) => { - Log.info(`**${member.guild}** - ${member} has left the server`); - }); + if (optionsValue?.logging?.guild?.leave) { + instance.client.on(Events.GuildMemberRemove, (member) => { + Log.info(`**${member.guild}** - ${member} has left the server`); + }); + } // handle when a member join a voice channel - instance.client.on(Events.VoiceStateUpdate, (oldState, newState) => { - if (oldState.channelId === newState.channelId) return; - if (oldState.channelId === null) { - Log.info( - `**${newState.guild}** - ${newState.member} has joined <#${newState.channelId}>`, - ); - } else if (newState.channelId === null) { - Log.info( - `**${oldState.guild}** - ${oldState.member} has left <#${oldState.channelId}>`, - ); - } else { - Log.info( - `**${newState.guild}** - ${newState.member} switch from <#${oldState.channelId}> to <#${newState.channelId}>`, - ); - } - }); + if (optionsValue?.logging?.voice?.join) { + instance.client.on(Events.VoiceStateUpdate, (oldState, newState) => { + if (oldState.channelId === newState.channelId) return; + if (oldState.channelId === null) { + Log.info( + `**${newState.guild}** - ${newState.member} has joined <#${newState.channelId}>`, + ); + } else if (newState.channelId === null) { + Log.info( + `**${oldState.guild}** - ${oldState.member} has left <#${oldState.channelId}>`, + ); + } else { + Log.info( + `**${newState.guild}** - ${newState.member} switch from <#${oldState.channelId}> to <#${newState.channelId}>`, + ); + } + }); + } // handle when someone change the role of someone - instance.client.on(Events.GuildMemberUpdate, (oldMember, newMember) => { - if (oldMember.roles.cache.size === newMember.roles.cache.size) return; - if (oldMember.roles.cache.size > newMember.roles.cache.size) { - const role = oldMember.roles.cache.find( - (role) => !newMember.roles.cache.has(role.id), - ); - Log.info( - `**${newMember.guild}** - ${newMember.user} lost the role ${role}`, - ); - } else { - const role = newMember.roles.cache.find( - (role) => !oldMember.roles.cache.has(role.id), - ); - Log.info( - `**${newMember.guild}** - ${newMember.user} got the role ${role}`, - ); - } - }); + if (optionsValue?.logging?.guild?.roleChange) { + instance.client.on(Events.GuildMemberUpdate, (oldMember, newMember) => { + if (oldMember.roles.cache.size === newMember.roles.cache.size) return; + if (oldMember.roles.cache.size > newMember.roles.cache.size) { + const role = oldMember.roles.cache.find( + (role) => !newMember.roles.cache.has(role.id), + ); + Log.info( + `**${newMember.guild}** - ${newMember.user} lost the role ${role}`, + ); + } else { + const role = newMember.roles.cache.find( + (role) => !oldMember.roles.cache.has(role.id), + ); + Log.info( + `**${newMember.guild}** - ${newMember.user} got the role ${role}`, + ); + } + }); + } // handle when someone change the nickname of someone - instance.client.on(Events.GuildMemberUpdate, (oldMember, newMember) => { - if (oldMember.nickname === newMember.nickname) return; - Log.info( - `**${newMember.guild}** - ${newMember.user} changed his nickname from ${ - oldMember.nickname || oldMember.user.username - } to ${newMember.nickname || newMember.user.username}`, - ); - }); + if (optionsValue?.logging?.member?.nicknameChange) { + instance.client.on(Events.GuildMemberUpdate, (oldMember, newMember) => { + if (oldMember.nickname === newMember.nickname) return; + Log.info( + `**${newMember.guild}** - ${newMember.user} changed his nickname from ${ + oldMember.nickname || oldMember.user.username + } to ${newMember.nickname || newMember.user.username}`, + ); + }); + } // handle when someone react to a message - instance.client.on(Events.MessageReactionAdd, (reaction, user) => { - if (user.bot) return; - Log.info( - `**${ - reaction.message.guild - }** - **${user}** reacted to ${`https://discord.com/channels/${reaction.message.guild?.id}/${reaction.message.channel.id}/${reaction.message.id}`} with ${ - reaction.emoji - }`, - ); - }); + if (optionsValue?.logging?.message?.react?.add) { + instance.client.on(Events.MessageReactionAdd, (reaction, user) => { + if (user.bot) return; + Log.info( + `**${ + reaction.message.guild + }** - **${user}** reacted to ${`https://discord.com/channels/${reaction.message.guild?.id}/${reaction.message.channel.id}/${reaction.message.id}`} with ${ + reaction.emoji + }`, + ); + }); + } // handle when someone remove a reaction from a message - instance.client.on(Events.MessageReactionRemove, (reaction, user) => { - if (user.bot) return; - Log.info( - `**${ - reaction.message.guild - }** - **${user}** removed his reaction to ${`https://discord.com/channels/${reaction.message.guild?.id}/${reaction.message.channel.id}/${reaction.message.id}`} with ${ - reaction.emoji - }`, - ); - }); - - // handle when someone use a slash command - instance.client.on(Events.InteractionCreate, (interaction) => { - if (interaction.isCommand()) { + if (optionsValue?.logging?.message?.react?.remove) { + instance.client.on(Events.MessageReactionRemove, (reaction, user) => { + if (user.bot) return; Log.info( - `**${interaction.guild ?? "DM"}**`, - `member **${interaction.user.username}#${interaction.user.discriminator}** excuted the command ` + - "`" + - `/${interaction.commandName} ${interaction.options.data - .map((option) => option.name + ":" + option.value) - .join(" ")}` + - "`", + `**${ + reaction.message.guild + }** - **${user}** removed his reaction to ${`https://discord.com/channels/${reaction.message.guild?.id}/${reaction.message.channel.id}/${reaction.message.id}`} with ${ + reaction.emoji + }`, ); - } - }); + }); + } + + // handle when someone use a slash command + if (optionsValue?.logging?.member?.slashCommand) { + instance.client.on(Events.InteractionCreate, (interaction) => { + if (interaction.isCommand()) { + Log.info( + `**${interaction.guild ?? "DM"}**`, + `member **${interaction.user.username}#${interaction.user.discriminator}** excuted the command ` + + "`" + + `/${interaction.commandName} ${interaction.options.data + .map((option) => option.name + ":" + option.value) + .join(" ")}` + + "`", + ); + } + }); + } // handle when someone deafen - instance.client.on(Events.VoiceStateUpdate, (oldState, newState) => { - if (oldState.deaf === newState.deaf) return; - if (newState.deaf) { - Log.info(`**${newState.guild}** - ${newState.member} has been deafened`); - dixt.events.emit("report", { - message: `${newState.member} has been deafened.`, - }); - } else { - Log.info( - `**${newState.guild}** - ${newState.member} has been undeafened`, - ); - } - }); + if (optionsValue?.logging?.voice?.deafen) { + instance.client.on(Events.VoiceStateUpdate, (oldState, newState) => { + if (oldState.deaf === newState.deaf) return; + if (newState.deaf) { + Log.info( + `**${newState.guild}** - ${newState.member} has been deafened`, + ); + dixt.events.emit("report", { + message: `${newState.member} has been deafened.`, + }); + } else { + Log.info( + `**${newState.guild}** - ${newState.member} has been undeafened`, + ); + } + }); + } return { name,