-
Notifications
You must be signed in to change notification settings - Fork 0
Middleware
Danny SMc edited this page Dec 24, 2021
·
1 revision
The discord bot service, still abides by the auth guards and middleware, so you can create a middleware, and then pull data from the user and add it to the auth object, so you can still call the auth decorators against the methods.
Below is an example decorator that pulls the roles, and additional user data.
Note the below is defined as a global middleware for all discord commands, but you can of course use different middlewares for different controllers by doing
@Auth.Use(AuthMiddleware)
instead for a specific controller.
import { Middleware, AbstractMiddleware } from '@symbux/turbo';
import { IMiddleware, Context } from '@symbux/turbo-discord';
@Middleware('discord.auth', {}, 'discord')
export default class AuthMiddleware extends AbstractMiddleware implements IMiddleware {
public async handle(context: Context): Promise<boolean> {
const member = context.getGuildMember();
const authObject = {
id: member.user.id,
nickname: member.nickname,
username: member.user.username,
discriminator: member.user.discriminator,
roles: member.roles.cache.map(role => role.name),
};
context.setAuth(authObject);
return true;
}
}
Below demonstrates using the auth decorators with the above middleware, this is available in the demo folder in the repository.
import { AbstractCommand, Command, Context, On } from '../../src/index';
import { Auth } from '@symbux/turbo';
@Command(/* ... */)
export default class UsersCommand extends AbstractCommand {
@On.Command()
@Auth.InArray('roles', 'Admin')
public async command(context: Context): Promise<void> {
// ...
}
@On.Button('yes')
@Auth.InArray('roles', 'Admin')
public async buttonYes(context: Context): Promise<void> {
// ...
}
@On.Button('no')
@Auth.InArray('roles', 'Admin')
public async buttonNo(context: Context): Promise<void> {
// ...
}
@On.SelectMenu('animal')
@Auth.InArray('roles', 'Admin')
public async selectMenu(context: Context): Promise<void> {
// ...
}
}
Home | Turbo Engine | GitHub | NPM
- Configuration
- Creating Commands
- Adding Autocomplete
- Adding Context Menu
- Middleware Setup
- Built-In Helpers
- Promise Queue
- Command Sessions
- Setting Up OAuth
- Turbo Engine Github
- Need your own plugin? Contact Me
- Have an idea? Let's Discuss
- Want to support me? Buy me a coffee