Skip to content

Commit

Permalink
feat: Add MessageCreateOptions, can explicitly exclude or not bot, ex…
Browse files Browse the repository at this point in the history
…plicitly send code errors or not, set global tags.
  • Loading branch information
Ayfri committed Sep 6, 2021
1 parent ff3f837 commit 005e860
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/defaults/events/MessageCreateEvent.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {Message} from 'discord.js';
import {argError, codeError, CommandHandler, getThing, Logger, permissionsError} from '../..';
import {CommandContext, CommandErrorType, Event, EventContext, Tag} from '../../classes';
import MessageCreateOptions = CommandHandler.MessageCreateOptions;

export class MessageCreateEvent extends Event {
public static options: MessageCreateOptions = {};
override readonly name = 'messageCreate';

public override async run(ctx: EventContext<this>, message: Message) {
if (message.author.bot || message.system) return;
if (MessageCreateEvent.options.excludeBots !== false && message.author.bot) return;
if (message.system) return;

const prefix = CommandHandler.getPrefixFromMessage(message);
if (!prefix) return;
Expand All @@ -22,6 +25,18 @@ export class MessageCreateEvent extends Event {
handler: ctx.handler,
});

if (MessageCreateEvent.options.globalTags) {
const missingTags = Tag.check(commandContext, MessageCreateEvent.options.globalTags);
return argError(
commandContext,
`There are missing global tags for the message: \n\`${missingTags
.map(tag => Tag[tag])
.sort()
.join('\n')
.toUpperCase()}\``
);
}

try {
const error = await command.execute(commandContext);

Expand Down Expand Up @@ -55,7 +70,9 @@ export class MessageCreateEvent extends Event {
Logger.log(`${message.author.tag} has executed the command ${Logger.setColor('red', command.name)}.`);
}
} catch (error) {
await codeError(commandContext, error instanceof Error ? error : new Error(String(error)));
if (MessageCreateEvent.options.sendCodeError) {
await codeError(commandContext, error instanceof Error ? error : new Error(String(error)));
}
}
}
}

0 comments on commit 005e860

Please sign in to comment.