Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate messageCreate off sapphire #328

Merged
merged 2 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import dotenv from 'dotenv';
dotenv.config();

import { Message } from 'discord.js';
import { container, LogLevel, SapphireClient, SapphirePrefix } from '@sapphire/framework';
import '@sapphire/plugin-logger/register';
import * as colorette from 'colorette';
import { inspect } from 'util';
import { initMessageCreate } from './events/messageCreate';
import { initReady } from './events/ready';

// Set default inspection depth
Expand Down Expand Up @@ -44,6 +46,9 @@ export const startBot = async (): Promise<void> => {
});
client.on('error', client.logger.error);
client.on('ready', initReady);
client.on('messageCreate', (message: Message) => {
initMessageCreate(client, container.logger, message);
});
client.login();
KevinBacabac marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
console.log('Bot failure');
Expand Down
53 changes: 20 additions & 33 deletions src/listeners/messageCreate.ts → src/events/messageCreate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ApplyOptions } from '@sapphire/decorators';
import { container, Listener } from '@sapphire/framework';
import { Message } from 'discord.js';
import { ILogger } from '@sapphire/framework';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep sapphire dependency for logger

import { Client, Message } from 'discord.js';
import { applyBonusByUserId } from '../components/coin';
import { vars } from '../config';
import { sendKickEmbed } from '../utils/embeds';
Expand Down Expand Up @@ -43,8 +42,7 @@ const detectSpammersAndTrolls = (message: Message): boolean => {
* Punish spammers/trolls/people who got hacked
* Return true if someone of this kind is detected, false otherwise
*/
const punishSpammersAndTrolls = async (message: Message): Promise<boolean> => {
const { logger } = container;
const punishSpammersAndTrolls = async (client: Client, logger: ILogger, message: Message): Promise<boolean> => {
if (detectSpammersAndTrolls(message)) {
// Delete the message, and if the user is still in the server, then kick them and log it
await message.delete();
Expand All @@ -61,7 +59,7 @@ const punishSpammersAndTrolls = async (message: Message): Promise<boolean> => {
error: (err as Error).toString()
});
}
await sendKickEmbed(message, user, reason, isSuccessful);
await sendKickEmbed(client, message, user, reason, isSuccessful);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass in the client as argument rather than using container

}
return true;
}
Expand Down Expand Up @@ -97,34 +95,23 @@ const convertResumePdfsIntoImages = async (message: Message): Promise<Message<bo
});
};

@ApplyOptions<Listener.Options>({
event: 'messageCreate'
})
export class MessageCreateListener extends Listener {
async run(message: Message): Promise<void> {
const { client } = container;

if (!client.user) {
return;
}

// Ignore all bots including self but not IRC
if (message.author.bot && message.author.id !== IRC_USER_ID) {
return;
}
export const initMessageCreate = async (client: Client, logger: ILogger, message: Message): Promise<void> => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convert class to function

// Ignore all bots including self but not IRC
if (message.author.bot && message.author.id !== IRC_USER_ID) {
return;
}

if (await punishSpammersAndTrolls(message)) {
return;
}
if (await punishSpammersAndTrolls(client, logger, message)) {
return;
}

// If channel is in resumes, convert the message attachment to an image
if (message.channelId === RESUME_CHANNEL_ID) {
await convertResumePdfsIntoImages(message);
}
// If channel is in resumes, convert the message attachment to an image
if (message.channelId === RESUME_CHANNEL_ID) {
await convertResumePdfsIntoImages(message);
}

// Ignore DMs; include announcements, thread, and regular text channels
if (message.channel.type !== 'DM') {
await applyBonusByUserId(message.author.id);
}
// Ignore DMs; include announcements, thread, and regular text channels
if (message.channel.type !== 'DM') {
await applyBonusByUserId(message.author.id);
}
}
};
12 changes: 8 additions & 4 deletions src/utils/embeds.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { container } from '@sapphire/framework';
import { Message, MessageEmbed, TextChannel, User } from 'discord.js';
import { Client, Message, MessageEmbed, TextChannel, User } from 'discord.js';
import { vars } from '../config';

const NOTIF_CHANNEL_ID: string = vars.NOTIF_CHANNEL_ID;
Expand All @@ -9,8 +8,13 @@ export const EMBED_COLOUR = '#0099ff';
/*
* Send kick embed
*/
export const sendKickEmbed = async (message: Message, user: User, reason = '', isSuccessful = true): Promise<void> => {
const { client } = container;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use client argument instead of container

export const sendKickEmbed = async (
client: Client,
message: Message,
user: User,
reason = '',
isSuccessful = true
): Promise<void> => {
const kickEmbed = new MessageEmbed()
.setColor(EMBED_COLOUR)
.addField('User', `${user.tag} (${user.id})`)
Expand Down