Skip to content

Commit 3f1d65e

Browse files
authored
Migrate messageCreate off sapphire (#328)
* Migrate messageCreate off sapphire * Remove if (client.user) check
1 parent 647ebf4 commit 3f1d65e

File tree

3 files changed

+33
-37
lines changed

3 files changed

+33
-37
lines changed

src/bot.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import dotenv from 'dotenv';
22
dotenv.config();
33

4+
import { Message } from 'discord.js';
45
import { container, LogLevel, SapphireClient, SapphirePrefix } from '@sapphire/framework';
56
import '@sapphire/plugin-logger/register';
67
import * as colorette from 'colorette';
78
import { inspect } from 'util';
9+
import { initMessageCreate } from './events/messageCreate';
810
import { initReady } from './events/ready';
911

1012
// Set default inspection depth
@@ -44,6 +46,9 @@ export const startBot = async (): Promise<void> => {
4446
});
4547
client.on('error', client.logger.error);
4648
client.on('ready', initReady);
49+
client.on('messageCreate', (message: Message) => {
50+
initMessageCreate(client, container.logger, message);
51+
});
4752
client.login();
4853
} catch (e) {
4954
console.log('Bot failure');

src/listeners/messageCreate.ts renamed to src/events/messageCreate.ts

+20-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { ApplyOptions } from '@sapphire/decorators';
2-
import { container, Listener } from '@sapphire/framework';
3-
import { Message } from 'discord.js';
1+
import { ILogger } from '@sapphire/framework';
2+
import { Client, Message } from 'discord.js';
43
import { applyBonusByUserId } from '../components/coin';
54
import { vars } from '../config';
65
import { sendKickEmbed } from '../utils/embeds';
@@ -43,8 +42,7 @@ const detectSpammersAndTrolls = (message: Message): boolean => {
4342
* Punish spammers/trolls/people who got hacked
4443
* Return true if someone of this kind is detected, false otherwise
4544
*/
46-
const punishSpammersAndTrolls = async (message: Message): Promise<boolean> => {
47-
const { logger } = container;
45+
const punishSpammersAndTrolls = async (client: Client, logger: ILogger, message: Message): Promise<boolean> => {
4846
if (detectSpammersAndTrolls(message)) {
4947
// Delete the message, and if the user is still in the server, then kick them and log it
5048
await message.delete();
@@ -61,7 +59,7 @@ const punishSpammersAndTrolls = async (message: Message): Promise<boolean> => {
6159
error: (err as Error).toString()
6260
});
6361
}
64-
await sendKickEmbed(message, user, reason, isSuccessful);
62+
await sendKickEmbed(client, message, user, reason, isSuccessful);
6563
}
6664
return true;
6765
}
@@ -97,34 +95,23 @@ const convertResumePdfsIntoImages = async (message: Message): Promise<Message<bo
9795
});
9896
};
9997

100-
@ApplyOptions<Listener.Options>({
101-
event: 'messageCreate'
102-
})
103-
export class MessageCreateListener extends Listener {
104-
async run(message: Message): Promise<void> {
105-
const { client } = container;
106-
107-
if (!client.user) {
108-
return;
109-
}
110-
111-
// Ignore all bots including self but not IRC
112-
if (message.author.bot && message.author.id !== IRC_USER_ID) {
113-
return;
114-
}
98+
export const initMessageCreate = async (client: Client, logger: ILogger, message: Message): Promise<void> => {
99+
// Ignore all bots including self but not IRC
100+
if (message.author.bot && message.author.id !== IRC_USER_ID) {
101+
return;
102+
}
115103

116-
if (await punishSpammersAndTrolls(message)) {
117-
return;
118-
}
104+
if (await punishSpammersAndTrolls(client, logger, message)) {
105+
return;
106+
}
119107

120-
// If channel is in resumes, convert the message attachment to an image
121-
if (message.channelId === RESUME_CHANNEL_ID) {
122-
await convertResumePdfsIntoImages(message);
123-
}
108+
// If channel is in resumes, convert the message attachment to an image
109+
if (message.channelId === RESUME_CHANNEL_ID) {
110+
await convertResumePdfsIntoImages(message);
111+
}
124112

125-
// Ignore DMs; include announcements, thread, and regular text channels
126-
if (message.channel.type !== 'DM') {
127-
await applyBonusByUserId(message.author.id);
128-
}
113+
// Ignore DMs; include announcements, thread, and regular text channels
114+
if (message.channel.type !== 'DM') {
115+
await applyBonusByUserId(message.author.id);
129116
}
130-
}
117+
};

src/utils/embeds.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { container } from '@sapphire/framework';
2-
import { Message, MessageEmbed, TextChannel, User } from 'discord.js';
1+
import { Client, Message, MessageEmbed, TextChannel, User } from 'discord.js';
32
import { vars } from '../config';
43

54
const NOTIF_CHANNEL_ID: string = vars.NOTIF_CHANNEL_ID;
@@ -9,8 +8,13 @@ export const EMBED_COLOUR = '#0099ff';
98
/*
109
* Send kick embed
1110
*/
12-
export const sendKickEmbed = async (message: Message, user: User, reason = '', isSuccessful = true): Promise<void> => {
13-
const { client } = container;
11+
export const sendKickEmbed = async (
12+
client: Client,
13+
message: Message,
14+
user: User,
15+
reason = '',
16+
isSuccessful = true
17+
): Promise<void> => {
1418
const kickEmbed = new MessageEmbed()
1519
.setColor(EMBED_COLOUR)
1620
.addField('User', `${user.tag} (${user.id})`)

0 commit comments

Comments
 (0)