refactor(audits): ♻️ add auditLog helper
Added auditLogger function as an helper to reduce duplication of code
This commit is contained in:
parent
40b640f88e
commit
ae368d8ed5
7 changed files with 112 additions and 222 deletions
|
@ -1,61 +1,24 @@
|
|||
import { ChannelType, EmbedBuilder, GuildMember } from "discord.js";
|
||||
import prisma from "../../handlers/database";
|
||||
import getEmbedConfig from "../../helpers/getEmbedData";
|
||||
import logger from "../../middlewares/logger";
|
||||
import { EmbedBuilder, GuildMember } from "discord.js";
|
||||
import auditLogger from "../../helpers/auditLogger";
|
||||
|
||||
export default {
|
||||
execute: async (member: GuildMember) => {
|
||||
const { client, guild } = member;
|
||||
|
||||
const getGuild = await prisma.guild.findUnique({
|
||||
where: { id: member.guild.id },
|
||||
});
|
||||
if (!getGuild) throw new Error("Guild not found");
|
||||
|
||||
if (getGuild.auditsEnabled !== true) return;
|
||||
if (!getGuild.auditsChannelId) {
|
||||
throw new Error("Channel not found");
|
||||
}
|
||||
|
||||
const embedConfig = await getEmbedConfig(guild);
|
||||
|
||||
const channel = client.channels.cache.get(getGuild.auditsChannelId);
|
||||
|
||||
if (!channel) throw new Error("Channel not found");
|
||||
if (channel.type !== ChannelType.GuildText) {
|
||||
throw new Error("Channel must be a text channel");
|
||||
}
|
||||
const { guild } = member;
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setTimestamp(new Date())
|
||||
.setAuthor({
|
||||
name: "Member Joined",
|
||||
iconURL: client.user?.displayAvatarURL(),
|
||||
iconURL: member.displayAvatarURL(),
|
||||
})
|
||||
.setFooter({
|
||||
text: embedConfig.footerText,
|
||||
iconURL: embedConfig.footerIcon,
|
||||
});
|
||||
|
||||
await channel
|
||||
.send({
|
||||
embeds: [
|
||||
embed
|
||||
.setColor(embedConfig.successColor)
|
||||
.setDescription(`${member.user} - (${member.user.tag})`)
|
||||
.addFields([
|
||||
{
|
||||
name: "Account Age",
|
||||
value: `${member.user.createdAt}`,
|
||||
},
|
||||
]),
|
||||
],
|
||||
})
|
||||
.then(() => {
|
||||
logger.debug(`Audit log sent for event guildMemberAdd`);
|
||||
})
|
||||
.catch(() => {
|
||||
throw new Error("Audit log failed to send");
|
||||
});
|
||||
.setDescription(`${member.user} - (${member.user.tag})`)
|
||||
.addFields([
|
||||
{
|
||||
name: "Account Age",
|
||||
value: `${member.user.createdAt}`,
|
||||
},
|
||||
]);
|
||||
|
||||
await auditLogger(guild, embed);
|
||||
},
|
||||
};
|
||||
|
|
|
@ -1,59 +1,24 @@
|
|||
import { ChannelType, EmbedBuilder, GuildMember } from "discord.js";
|
||||
import prisma from "../../handlers/database";
|
||||
import getEmbedConfig from "../../helpers/getEmbedData";
|
||||
import logger from "../../middlewares/logger";
|
||||
import { EmbedBuilder, GuildMember } from "discord.js";
|
||||
import auditLogger from "../../helpers/auditLogger";
|
||||
|
||||
export default {
|
||||
execute: async (member: GuildMember) => {
|
||||
const { client, guild } = member;
|
||||
|
||||
const getGuild = await prisma.guild.findUnique({
|
||||
where: { id: member.guild.id },
|
||||
});
|
||||
if (!getGuild) throw new Error("Guild not found");
|
||||
|
||||
if (getGuild.auditsEnabled !== true) return;
|
||||
if (!getGuild.auditsChannelId) {
|
||||
throw new Error("Channel not found");
|
||||
}
|
||||
|
||||
const embedConfig = await getEmbedConfig(guild);
|
||||
|
||||
const channel = client.channels.cache.get(getGuild.auditsChannelId);
|
||||
if (channel?.type !== ChannelType.GuildText) {
|
||||
throw new Error("Channel must be a text channel");
|
||||
}
|
||||
const { guild } = member;
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setTimestamp(new Date())
|
||||
.setAuthor({
|
||||
name: "Member Left",
|
||||
iconURL: client.user?.displayAvatarURL(),
|
||||
iconURL: member.displayAvatarURL(),
|
||||
})
|
||||
.setFooter({
|
||||
text: embedConfig.footerText,
|
||||
iconURL: embedConfig.footerIcon,
|
||||
});
|
||||
|
||||
channel
|
||||
.send({
|
||||
embeds: [
|
||||
embed
|
||||
.setColor(embedConfig.errorColor)
|
||||
.setDescription(`${member.user} - (${member.user.tag})`)
|
||||
.addFields([
|
||||
{
|
||||
name: "Account Age",
|
||||
value: `${member.user.createdAt}`,
|
||||
},
|
||||
]),
|
||||
],
|
||||
})
|
||||
.then(() => {
|
||||
logger.debug(`Audit log sent for event guildMemberRemove.`);
|
||||
})
|
||||
.catch(() => {
|
||||
throw new Error("Audit log failed to send");
|
||||
});
|
||||
.setDescription(`${member.user} - (${member.user.tag})`)
|
||||
.addFields([
|
||||
{
|
||||
name: "Account Age",
|
||||
value: `${member.user.createdAt}`,
|
||||
},
|
||||
]);
|
||||
|
||||
await auditLogger(guild, embed);
|
||||
},
|
||||
};
|
||||
|
|
|
@ -35,7 +35,7 @@ export default {
|
|||
`${author.username} sent the last message therefor not allowing again.`
|
||||
);
|
||||
await message.delete();
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (content !== channelCounter.triggerWord) {
|
||||
|
@ -44,7 +44,7 @@ export default {
|
|||
);
|
||||
|
||||
await message.delete();
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
|
||||
const updateGuildCounter = await prisma.guildCounter.update({
|
||||
|
|
|
@ -1,66 +1,23 @@
|
|||
import { ChannelType, EmbedBuilder, Message } from "discord.js";
|
||||
import prisma from "../../handlers/database";
|
||||
import getEmbedConfig from "../../helpers/getEmbedData";
|
||||
import logger from "../../middlewares/logger";
|
||||
import { EmbedBuilder, Message } from "discord.js";
|
||||
import auditLogger from "../../helpers/auditLogger";
|
||||
|
||||
export default {
|
||||
execute: async (message: Message) => {
|
||||
if (message === null) return;
|
||||
const { guild } = message;
|
||||
if (!guild) throw new Error("Guild unavailable");
|
||||
|
||||
if (message.guild === null) return;
|
||||
|
||||
const { footerText, footerIcon, successColor } = await getEmbedConfig(
|
||||
message.guild
|
||||
);
|
||||
|
||||
const getGuild = await prisma.guild.findUnique({
|
||||
where: { id: message.guild.id },
|
||||
});
|
||||
if (!getGuild) throw new Error("Guild not found");
|
||||
|
||||
const { client } = message;
|
||||
|
||||
if (!getGuild) throw new Error("Guild not found");
|
||||
|
||||
if (getGuild.auditsEnabled !== true) return;
|
||||
if (!getGuild.auditsChannelId) return;
|
||||
|
||||
const channel = client.channels.cache.get(`${getGuild.auditsChannelId}`);
|
||||
|
||||
if (!channel) return;
|
||||
if (channel.type !== ChannelType.GuildText) return;
|
||||
|
||||
channel
|
||||
.send({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setColor(successColor)
|
||||
.setAuthor({
|
||||
name: message.author.username,
|
||||
iconURL: message.author.displayAvatarURL(),
|
||||
})
|
||||
.setDescription(
|
||||
`
|
||||
**Message sent by** ${message.author} **deleted in** ${message.channel}
|
||||
${message.content}
|
||||
`
|
||||
)
|
||||
.setTimestamp()
|
||||
.setFooter({
|
||||
text: footerText,
|
||||
iconURL: footerIcon,
|
||||
}),
|
||||
],
|
||||
const embed = new EmbedBuilder()
|
||||
.setAuthor({
|
||||
name: message.author.username,
|
||||
iconURL: message.author.displayAvatarURL(),
|
||||
})
|
||||
.then(() => {
|
||||
logger.info(
|
||||
`Audit log sent for event messageDelete in guild ${message?.guild?.name} (${message?.guild?.id})`
|
||||
);
|
||||
})
|
||||
.catch(() => {
|
||||
throw new Error(
|
||||
`Audit log failed to send for event messageDelete in guild ${message?.guild?.name} (${message?.guild?.id})`
|
||||
);
|
||||
});
|
||||
.setDescription(
|
||||
`
|
||||
**Message sent by** ${message.author} **deleted in** ${message.channel}
|
||||
${message.content}
|
||||
`
|
||||
);
|
||||
|
||||
await auditLogger(guild, embed);
|
||||
},
|
||||
};
|
||||
|
|
|
@ -19,20 +19,24 @@ export default async (message: Message) => {
|
|||
},
|
||||
});
|
||||
|
||||
if (!channelCounter) throw new Error("No counter found in channel.");
|
||||
if (!channelCounter) return logger.debug("No counter found in channel.");
|
||||
|
||||
const messages = await message.channel.messages.fetch({ limit: 1 });
|
||||
const lastMessage = messages.last();
|
||||
|
||||
if (!lastMessage) return;
|
||||
if (!lastMessage) return false;
|
||||
|
||||
if (content !== channelCounter.triggerWord) return;
|
||||
if (content !== channelCounter.triggerWord)
|
||||
return logger.silly("Message do not include the trigger word");
|
||||
|
||||
if (lastMessage.author.id === message.author.id) return;
|
||||
if (lastMessage.author.id === message.author.id)
|
||||
return logger.debug("Same author as last message");
|
||||
|
||||
channel?.send(`${author} said **${channelCounter.triggerWord}**.`);
|
||||
logger?.silly(`${author} said ${channelCounter.triggerWord} in ${channel}`);
|
||||
logger?.silly(
|
||||
`User: ${author?.tag} (${author?.id}) in guild: ${guild?.name} (${guild?.id}) said the counter word: ${channelCounter.triggerWord}`
|
||||
);
|
||||
return await channel.send(
|
||||
`${author} said **${channelCounter.triggerWord}**.`
|
||||
);
|
||||
};
|
||||
|
|
|
@ -1,66 +1,23 @@
|
|||
/* eslint-disable no-loops/no-loops */
|
||||
import { ChannelType, EmbedBuilder, Message } from "discord.js";
|
||||
import prisma from "../../handlers/database";
|
||||
import getEmbedConfig from "../../helpers/getEmbedData";
|
||||
import logger from "../../middlewares/logger";
|
||||
import { EmbedBuilder, Message } from "discord.js";
|
||||
import auditLogger from "../../helpers/auditLogger";
|
||||
|
||||
export default {
|
||||
execute: async (oldMessage: Message, newMessage: Message) => {
|
||||
if (oldMessage === null) return;
|
||||
if (newMessage === null) return;
|
||||
const { guild } = newMessage;
|
||||
if (!guild) throw new Error("Guild unavailable");
|
||||
|
||||
if (oldMessage.guild === null) return;
|
||||
if (newMessage.guild === null) return;
|
||||
|
||||
const { footerText, footerIcon, successColor } = await getEmbedConfig(
|
||||
newMessage.guild
|
||||
);
|
||||
|
||||
const getGuild = await prisma.guild.findUnique({
|
||||
where: { id: oldMessage.guild.id },
|
||||
});
|
||||
if (!getGuild) throw new Error("Guild not found");
|
||||
|
||||
const { client } = oldMessage;
|
||||
|
||||
if (getGuild.auditsEnabled !== true) return;
|
||||
if (!getGuild.auditsChannelId) return;
|
||||
|
||||
const channel = client.channels.cache.get(`${getGuild.auditsChannelId}`);
|
||||
|
||||
if (!channel) return;
|
||||
if (channel.type !== ChannelType.GuildText) return;
|
||||
|
||||
channel
|
||||
.send({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setColor(successColor)
|
||||
.setAuthor({
|
||||
name: newMessage.author.username,
|
||||
iconURL: newMessage.author.displayAvatarURL(),
|
||||
})
|
||||
.setDescription(
|
||||
`
|
||||
**Message edited in** ${newMessage.channel} [jump to message](https://discord.com/channels/${newMessage.guild.id}/${newMessage.channel.id}/${newMessage.id})
|
||||
`
|
||||
)
|
||||
.setTimestamp()
|
||||
.setFooter({
|
||||
text: footerText,
|
||||
iconURL: footerIcon,
|
||||
}),
|
||||
],
|
||||
const embed = new EmbedBuilder()
|
||||
.setAuthor({
|
||||
name: newMessage.author.username,
|
||||
iconURL: newMessage.author.displayAvatarURL(),
|
||||
})
|
||||
.then(() => {
|
||||
logger.info(
|
||||
`Audit log sent for event messageUpdate in guild ${newMessage?.guild?.name} (${newMessage?.guild?.id})`
|
||||
);
|
||||
})
|
||||
.catch(() => {
|
||||
throw new Error(
|
||||
`Audit log failed to send for event messageUpdate in guild ${newMessage?.guild?.name} (${newMessage?.guild?.id})`
|
||||
);
|
||||
});
|
||||
.setDescription(
|
||||
`
|
||||
**Message edited in** ${newMessage.channel} [jump to message](https://discord.com/channels/${newMessage.guild.id}/${newMessage.channel.id}/${newMessage.id})
|
||||
`
|
||||
);
|
||||
|
||||
await auditLogger(guild, embed);
|
||||
},
|
||||
};
|
||||
|
|
44
src/helpers/auditLogger/index.ts
Normal file
44
src/helpers/auditLogger/index.ts
Normal file
|
@ -0,0 +1,44 @@
|
|||
import { ChannelType, EmbedBuilder, Guild } from "discord.js";
|
||||
import prisma from "../../handlers/database";
|
||||
import getEmbedConfig from "../../helpers/getEmbedData";
|
||||
import logger from "../../middlewares/logger";
|
||||
|
||||
export default async (guild: Guild, embed: EmbedBuilder) => {
|
||||
const getGuild = await prisma.guild.findUnique({
|
||||
where: { id: guild.id },
|
||||
});
|
||||
if (!getGuild) throw new Error("Guild not found");
|
||||
|
||||
if (getGuild.auditsEnabled !== true) return;
|
||||
if (!getGuild.auditsChannelId) {
|
||||
throw new Error("Channel not found");
|
||||
}
|
||||
|
||||
const embedConfig = await getEmbedConfig(guild);
|
||||
|
||||
embed
|
||||
.setTimestamp(new Date())
|
||||
.setFooter({
|
||||
text: embedConfig.footerText,
|
||||
iconURL: embedConfig.footerIcon,
|
||||
})
|
||||
.setColor(embedConfig.successColor);
|
||||
|
||||
const channel = guild.client.channels.cache.get(getGuild.auditsChannelId);
|
||||
|
||||
if (!channel) throw new Error("Channel not found");
|
||||
if (channel.type !== ChannelType.GuildText) {
|
||||
throw new Error("Channel must be a text channel");
|
||||
}
|
||||
|
||||
await channel
|
||||
.send({
|
||||
embeds: [embed],
|
||||
})
|
||||
.then(() => {
|
||||
logger.debug(`Audit log sent for event guildMemberAdd`);
|
||||
})
|
||||
.catch(() => {
|
||||
throw new Error("Audit log failed to send");
|
||||
});
|
||||
};
|
Loading…
Add table
Reference in a new issue