Skip to content

Commit

Permalink
feat(events): create Events.PreMessageParsed (#164)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: CoreMessageParser is not longer a message event listener
  • Loading branch information
kyranet committed Feb 7, 2021
1 parent 769d677 commit 0311838
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
18 changes: 18 additions & 0 deletions src/events/command-handler/CoreMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { PieceContext } from '@sapphire/pieces';
import type { Message } from 'discord.js';
import { Event } from '../../lib/structures/Event';
import { Events } from '../../lib/types/Events';

export class CoreEvent extends Event<Events.Message> {
public constructor(context: PieceContext) {
super(context, { event: Events.Message });
}

public run(message: Message) {
// Stop bots and webhooks from running commands.
if (message.author.bot || message.webhookID) return;

// Run the message parser.
this.context.client.emit(Events.PreMessageParsed, message);
}
}
7 changes: 2 additions & 5 deletions src/events/command-handler/CoreMessageParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ import { Message, NewsChannel, Permissions, TextChannel } from 'discord.js';
import { Event } from '../../lib/structures/Event';
import { Events } from '../../lib/types/Events';

export class CoreEvent extends Event<Events.Message> {
export class CoreEvent extends Event<Events.PreMessageParsed> {
private readonly requiredPermissions = new Permissions(['VIEW_CHANNEL', 'SEND_MESSAGES']).freeze();
public constructor(context: PieceContext) {
super(context, { event: Events.Message });
super(context, { event: Events.PreMessageParsed });
}

public async run(message: Message) {
// Stop bots and webhooks from running commands.
if (message.author.bot || message.webhookID) return;

// If the bot cannot run the command due to lack of permissions, return.
const canRun = await this.canRunInChannel(message);
if (!canRun) return;
Expand Down
2 changes: 2 additions & 0 deletions src/lib/types/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export enum Events {
PiecePostLoad = 'piecePostLoad',
MentionPrefixOnly = 'mentionPrefixOnly',
EventError = 'eventError',
PreMessageParsed = 'preMessageParsed',
PrefixedMessage = 'prefixedMessage',
UnknownCommandName = 'unknownCommandName',
UnknownCommand = 'unknownCommand',
Expand Down Expand Up @@ -118,6 +119,7 @@ declare module 'discord.js' {
[Events.PiecePostLoad]: [Store<Piece>, Piece];
[Events.MentionPrefixOnly]: [Message];
[Events.EventError]: [Error, EventErrorPayload];
[Events.PreMessageParsed]: [Message];
[Events.PrefixedMessage]: [Message, string | RegExp];
[Events.UnknownCommandName]: [Message, string | RegExp];
[Events.UnknownCommand]: [Message, string, string | RegExp];
Expand Down

0 comments on commit 0311838

Please sign in to comment.