-
Notifications
You must be signed in to change notification settings - Fork 19
Role Commands
As you may have noticed, discord roles are used extensively by the CSSS discord guild, both to get access to things [via roles that begin with a Capital letter] as well as roles that users can assign to themselves [that begin with a lowercase letter] for a variety of reason.
As such, her are the following commands that users can use to interact with the lower-case roles:
-
newrole
: to create a new lower-case role -
deleterole
: to delete an empty role -
iam
: to assign oneself to a lower-case role -
iamn
: to remove oneself from a lower-case role -
whois
: to get a list of folks in a role -
roles
: to get a list of assignable [lower-case] roles -
Roles
: to get a list of non-self assignable [upper-case] roles -
purgeroles
: command executed by users with certain privileges that can delete all empty lower-case roles
To provide some context, the rule for the role-related commands has always been to use them only in the channel bot-commands-and-misc
channel to cut down on the amount of spam-like messages that can pollute the other channels [someone removing 4 roles from themselves is a lot of back and forth and can pollute a channel and is not really relevant to anyone else].
So, one of the biggest complaints in the CSSS discord guild around role management was that at the start of every semester, there would be a big increase in the number of people not following that rule and the moderators had gotten tired of continuously reminding folks about it, essentially putting the guild in a place where the rules are technically not enforced.
As such, I created a PR in 2020 that provided logic that has grown to become the following as of writing this:
When a command wants to reply to a user, the command will almost never directly respond, but will instead use send_message_to_user_or_bot_channel
What that function does it to look at the context of the response and determine if the user invoked the command from the bot-commands-and-misc
channel [in which case, the original message can simply be responded to], or if it came from another channel and the response is marked by the code as needing to be deleted, then the original message is deleted after the user receives a response via the original channel.
Or if the response is not marked for deletion then the bot will first try to DM the user the response after deleting the invoking message and if that doesn't work [for reasons like maybe the user has disabled DMs] then the message will have the user tag and is sent on bot-commands-and-misc
channel.
As you may notice, quite a number of the slash commands for the role cog have an auto-complete menu. That was done to attempt to make it less of a occurrence for users to enter the wrong input into the commands.
However, that also had a certain limitation. The main one being that the function called by the auto-complete mechanism can take only 3 seconds before a time-out essentially occurs
Which caused competing interests. when someone is calling /iam
, the preference is for the auto-complete function to interact directly with the interaction
parameter to be sure that the roles that are being used to populate the menu are the latest.
But if we have a lot of roles, that can result in the function taking more then 3 seconds as it has to iterate through all the roles [and the API calls necessary to get them] and convert them to a list of string.
So, the object was
- make sure to be working with the latest list of roles
- complete the logic in <= 3 seconds.
So to satisfy the second restriction, there is no choice but to loosen the first requirements.
Which is what lead to the creation of the these 2 class variables: lowercase_roles
and roles_with_members
And the creation for those variable also lead to the need for a way to regularly update them:
The above listeners all just set role_change_detected
which is used by the update_roles_cache
background task that is responsible for updating lowercase_roles
and roles_with_members
users also have the ability to manually update the lowercase_roles
and roles_with_members
with sync_roles
command in the case that there is a role that is not yet appearing in the auto-complete just cause the update_roles_cache
function has not run yet.