Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Add permission checking for commands #14

Open
disassembledd opened this issue Sep 26, 2020 · 0 comments
Open

Add permission checking for commands #14

disassembledd opened this issue Sep 26, 2020 · 0 comments

Comments

@disassembledd
Copy link

I suggest to add permission checking functionality for commands as it's something that will almost always be necessary in most bots and I find myself implementing a lot.

For example, I have recently edited the commands.go file and added a Permission field to the Command struct. With this, I added a middleware that uses a util method I made called HasPermissions which returns a bool I use to either execute the command if the user had the permission assigned on the command or not.

Example struct:

router.RegisterCmd(&dgc.Command{
	Name:        "Kick",
	Description: "Kicks the given member from the guild with the given reason.",
	Usage:       "kick <@member> <reason>",
	Example:     "kick @troll Doesn't stop trolling",
	IgnoreCase:  true,
	Permission:  discordgo.PermissionKickMembers,
	Handler:     cmd.Kick,
})

Example middleware:

router.RegisterMiddleware(func(next dgc.ExecutionHandler) dgc.ExecutionHandler {
	return func(ctx *dgc.Ctx) {
		if util.HasPermission(ctx, ctx.Command.Permission) {
			next(ctx)
		} else {
			fmt.Printf("%v attempted command %v, missing permissions", ctx.Event.Author.ID, ctx.Command.Name)
			return
		}
	}
})
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant