What is discord Role?
A role is a group created by server admins. The role has permissions, a hierarchy, and a name. What's essential in this case is that players can refer to multiple players at once ("ping" them) by referring to their role like this @RoleName.
Context
I'm developing a bot that issues temporary roles to users. Users take Roles so that others can ping them. A Role is given for the duration user's desires. The bot is being written in Go.
The whole app primarily consists of handlers of both Mongo events and commands that users type to the bot.
Each time user takes a role by typing a command to the bot, the Discord id of that user is stored inside the Mongo database with expiration time. Once a time stored within the document is reached, the document is deleted by Mongo (TTL index is used for this). Then via handling Mongo events, the bot handles deletion by taking away roles from users. (Also, the user may remove his role preemptively. In this case, the role is being taken away and the corresponding record from the database.)
Question
When a user decides to remove his role, the Mongo deletion event handler is fired despite the user's record already being deleted inside the user command handler. So, I thought it was possible to control the bot exclusively inside Mongo's event handlers. Instead of giving and removing roles inside handlers corresponding to commands users gave, I can do so inside Mongo events. For example, to take away a role from the user, I need to remove his record from the database inside the command handler. Then role will be taken away automatically inside the corresponding Mongo event handler.
My experience does not let me foresee how this would affect future bot development, so I'm asking you for advice on this. Would this be a good idea? If not, what would be an excellent way to deal with Mongo handlers being fired in such cases as one above?
I suck at writing, so don't be shy to ask for clarifications.
CodePudding user response:
If the database is the authority on which users have which roles, your bot application serves as the bridge to keep Discord up to date with that authority.
Suppose you process role updates only on database events. In that case, this means your bridge application must be running and properly connected to both services without any error occurring at the time of every expiration. Otherwise, the state will fall out of sync. For correctness, it's best to design with the assumption that any or all of the three services could go offline at any time.
To keep your experience responsive and consistent, you should update the roles quickly and in as many places as is necessary to ensure eventual consistency. This may result in redundant updates, so check if the update is already applied and skip it if necessary.
Here's an example of update cases:
- On bot application startup: Update all users
- On database event: Update user
- On user command: Update user (optional, but may provide faster user feedback if DB events are delayed)
- On user joining server: Update user
- On periodic/background task: Update all users (optional, in case other updates fail or roles are modified by external sources)