A few of my friends asked me recently if I could help them with the Discord bot, YAGPDB, that they use to help organize server roles for people that they play games with. I do love chat bots so I opted to throw some code their way.
Specifically, what they were asking for were two commands for assigning and removing a given role to a list of users. The documentation for YAGPDB isn’t the greatest and their code samples use a function that they developed, userArg
, which limits you to 5 calls per custom command execution; not ideal for bulk assignments. Below are the custom commands that I created which do not use userArg
so they aren’t restricted to this limit. I got around it by simply parsing out the user id.
Give Role
This custom command gives the specified role to all listed users. When I deployed it to YAGPDB, I called it give-role
which is why I refer to it as such in the usage instructions I wrote.
{{ if (gt (len .CmdArgs) 1) }}
{{ $role := (reReplace ">" (slice (index $.CmdArgs 0) 3) "") }}
{{ range $x := seq 1 (len .CmdArgs) }}
{{ $userId := (reReplace ">" (slice (index $.CmdArgs $x) 3) "") }}
{{ giveRoleID $userId $role }}
{{ end }}
{{ $role }} given
{{ else }}
Usage: give-role \@role \@user1 \@user2 ...
{{ end }}
Take Role
This custom command removes the specified role from all listed users. Like the give role command, I called this one take-role
when I deployed it.
{{ if (gt (len .CmdArgs) 1) }}
{{ $role := (reReplace ">" (slice (index $.CmdArgs 0) 3) "") }}
{{ range $x := seq 1 (len .CmdArgs) }}
{{ $userId := (reReplace ">" (slice (index $.CmdArgs $x) 3) "") }}
{{ takeRoleID $userId $role }}
{{ end }}
{{ $role }} taken
{{ else }}
Usage: take-role \@role \@user1 \@user2 ...
{{ end }}
Closing thoughts
I love how vibrant the Discord bot community is and how it is helping people spend more time gaming and less time doing administrating and coordinating. I was a little surprised that these commands were not already built into YAGPDB since bulk role management is a pretty common task. I hope others find these custom commands as useful as my friends do.