Closed ItsMeBrianD closed 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!');
}
}
Done with Slash commands ! :)
https://owencalvin.github.io/discord.ts/general/client.html#setup-and-start-your-application
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 theClient
create, or the@Discord
decorator.