Skip to content

Commit

Permalink
feat: logger module
Browse files Browse the repository at this point in the history
  • Loading branch information
lleyton committed Nov 10, 2024
1 parent 24258de commit 5365c1d
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ PRIMARY_GUILD_ID=
ANNOUNCEMENTS_CHANNEL_ID=
UPDATES_CHANNEL_ID=
GENERAL_CHANNEL_ID=
LOGGING_CHANNEL_ID=
MEMBER_ROLE_ID=
SUPPORT_FORUM_ID=
HEALTH_PORT=8080
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ import './modules/guildMemberAdd';
import './modules/funAI';
import './modules/cutefishInfo';
import './modules/support';
import './modules/logger';
136 changes: 136 additions & 0 deletions src/modules/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import client from '../client';
import { EmbedBuilder, Events, time, TimestampStyles } from 'discord.js';
import { getLoggingChannel, userURL } from '../util';

// Right now we log the following for moderation purposes:
// - MessageDelete
// - MessageUpdate
// - GuildMemberAdd
// - GuildMemberRemove

client.on(Events.MessageDelete, async (message) => {
if (!message.member) return;

const loggingChannel = await getLoggingChannel();
if (!loggingChannel.isSendable()) {
throw new Error('Logging channel is not a text channel.');
}

const embed = new EmbedBuilder()
.setTitle('Message Delete')
.setAuthor({
name: message.member.displayName,
iconURL: message.member.displayAvatarURL(),
url: userURL(message.member.user.id),
})
.setColor('#ff0000')
.setDescription(message.content)
.setFooter({
text: `Message ID: ${message.id}`,
}).data;

await loggingChannel.send({
embeds: [embed],
});
});

client.on(Events.MessageUpdate, async (oldMessage, newMessage) => {
if (!oldMessage.member) return;

const loggingChannel = await getLoggingChannel();
if (!loggingChannel.isSendable()) {
throw new Error('Logging channel is not a text channel.');
}

const embed = new EmbedBuilder()
.setTitle('Message Update')
.setAuthor({
name: oldMessage.member.displayName,
iconURL: oldMessage.member.displayAvatarURL(),
url: userURL(oldMessage.member.user.id),
})
.setColor('#ffff00')
.addFields(
{
name: 'Old Content',
value: oldMessage.content ?? 'Unknown',
},
{
name: 'New Content',
value: newMessage.content ?? 'Unknown',
},
)
.setFooter({
text: `Message ID: ${oldMessage.id}`,
}).data;

await loggingChannel.send({
embeds: [embed],
});
});

client.on(Events.GuildMemberAdd, async (member) => {
const loggingChannel = await getLoggingChannel();
if (!loggingChannel.isSendable()) {
throw new Error('Logging channel is not a text channel.');
}

const embed = new EmbedBuilder()
.setTitle('Member Join')
.setAuthor({
name: member.displayName,
iconURL: member.displayAvatarURL(),
url: userURL(member.user.id),
})
.setColor('#00ff00')
.addFields(
{
name: 'Discord User Since',
value: time(member.user.createdAt, TimestampStyles.ShortDateTime),
},
{
name: 'User ID',
value: member.user.id,
},
)
.setFooter({
text: `Member ID: ${member.id}`,
}).data;

await loggingChannel.send({
embeds: [embed],
});
});

client.on(Events.GuildMemberRemove, async (member) => {
const loggingChannel = await getLoggingChannel();
if (!loggingChannel.isSendable()) {
throw new Error('Logging channel is not a text channel.');
}

const embed = new EmbedBuilder()
.setTitle('Member Leave')
.setAuthor({
name: member.displayName,
iconURL: member.displayAvatarURL(),
url: userURL(member.user.id),
})
.setColor('#ff0000')
.addFields(
{
name: 'Member Since',
value: member.joinedAt ? time(member.joinedAt, TimestampStyles.ShortDateTime) : 'Unknown',
},
{
name: 'User ID',
value: member.user.id,
},
)
.setFooter({
text: `Member ID: ${member.id}`,
}).data;

await loggingChannel.send({
embeds: [embed],
});
});
6 changes: 6 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export const getGeneralChannel = async (): Promise<Channel> =>
(await client.channels.fetch(process.env.GENERAL_CHANNEL_ID!)) ??
throwError('General channel not found');

export const getLoggingChannel = async (): Promise<Channel> =>
(await client.channels.fetch(process.env.LOGGING_CHANNEL_ID!)) ??
throwError('Logging channel not found');

export const getRedisConnection = (): ConnectionOptions => ({
host: process.env.REDIS_HOST!,
port: Number.parseInt(process.env.REDIS_PORT!, 10),
Expand All @@ -40,3 +44,5 @@ export const containsWord = (msg: Message, word: string): boolean => {
const matches = msg.content.match(new RegExp(`\\b${word}\\b`, 'i'));
return matches != null && matches.length > 0;
};

export const userURL = (id: string) => `https://discord.com/users/${id}`;

0 comments on commit 5365c1d

Please sign in to comment.