owengombas / discord.ts

🤖 Create your discord bot by using TypeScript and decorators!

Home Page:https://owencalvin.github.io/discord.ts/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature Request] Globally apply a guard

ItsMeBrianD opened this issue · comments

I am using Mikro ORM in my bot, which requires the use of a RequestContext to ensure that it's identity map updates, and doing this with a Guard was the easiest way to make it work. Unfortunately this means that every command I create will need to be decorated with that guard, and it would be helpful to be able to globally apply this guard, either through the Client create, or the @Discord decorator.

commented

To add to this, it would be nice to be able to do it on a class-level as well.
Example:
IsAdmin.ts:

export const IsAdmin: GuardFunction<'message'> = ([message], client, next) => {
  if (!message.member.roles.cache.has(process.env.ADMIN_ROLE)) {
    return;
  }
  await next();
};

bot.ts:

@Discord('!', {
  import: [Path.join(__dirname, 'admin-commands.ts')],
})
export class Bot {
  @Command()
  async hello(command: CommandMessage<never>) {
    await command.reply('hello there!');
  }
}

admin-commands.ts:

@Guard(IsAdmin)
export class AdminCommands {
  @Command()
  async ping(command: CommandMessage<never>) {
    await command.reply('pong!');
  }
}