owengombas / discord.ts

🤖 Create your discord bot by using TypeScript and decorators!
https://owencalvin.github.io/discord.ts/
324 stars 40 forks source link

[Feature Request] Globally apply a guard #36

Closed ItsMeBrianD closed 3 years ago

ItsMeBrianD commented 3 years ago

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.

AndyClausen commented 3 years ago

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!');
  }
}
owengombas commented 3 years ago

Done with Slash commands ! :)

https://owencalvin.github.io/discord.ts/general/client.html#setup-and-start-your-application