Skip to content

Commit

Permalink
πŸ”€ merge pull request #83 from jerembdn/master
Browse files Browse the repository at this point in the history
πŸ”§ add options for logging
  • Loading branch information
AntoineKM authored Dec 16, 2023
2 parents 054d570 + 4b27df5 commit c4eaf99
Showing 1 changed file with 181 additions and 108 deletions.
289 changes: 181 additions & 108 deletions packages/dixt-plugin-logs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c4eaf99

Please sign in to comment.