User Roles

Sometimes you want to filter who can use a command.

Think of the KickCommand that kicks a user, it's not good that members can kick moderators, for example.

In this command we will show you how to code the KickCommand.

The KickCommand#

path/to/your/app/src/Commands/KickCommand.ts
import {
Command,
BaseCommand,
Argument,
Mention,
Author,
UserContract,
oneOf,
} from "discapp";
@Command({
roles: oneOf("admin", "moderator"),
clientPermissions: ["KICK_MEMBERS"],
})
export class KickCommand extends BaseCommand {
@Argument()
public user: Mention;
@Author()
public author: UserContract;
public async execute() {
const userAsMember = await this.user.asMember();
if (userAsMember.kickable) {
await userAsMember.kick();
return `${this.author} kicked ${this.user}`;
}
return `Can't kick ${this.user}`;
}
}
info

Notice that in we requiring the user to have one of the roles: admin or moderator by using the oneOf utilitary method from Discapp.

Discapp also provides the allOf method for requiring all of the roles. You can use the allOf and oneOf in permissions, roles, clientPermissions and clientRoles.

Testing the KickCommand#

You can test the the KickCommand by adding another bot to your server and sending:

!kick @(the other bot)

Now the other bot must have been kicked.

danger

Certify that your bot has higher ranking that the bot you're kicking.