Society of Math Students

Reaction-Role Discord Bot

Status: Local-tested
Language: Python 3
Library: discord.py
Bot Structure - Reaction-role message-to-role mapping

Bot Structure - Reaction-role message-to-role mapping

Analysis & FEA

This bot uses a dictionary that connects each message with its corresponding course and persists it using a JSON file. An admin-level command that is used during setup sends one message per course on the role list and reacts to it with a check mark emoji, connecting its message ID to the role name. Role assignment and removal are done using raw reaction event listeners that are able to operate on messages not yet in cache because Discord reaction events do not ensure loading of message information.

Manufacturing

Written in Python 3 by using discord.py, along with a small Flask application running on a separate background thread only for the purpose of providing a pingable HTTP endpoint to host providers such as Replit in order to keep the bot alive. The bot token has been taken from an environment variable, while the list of roles is declared in an array format.

Testing

Local Test - Bot running and responding on a personal Discord server

Results

Verified Performance
01

Local Testing

Confirmed working end-to-end on a local test server: the setup command properly creates one reaction message per course, and the addition or removal of the check mark reaction grants or revokes the corresponding role instantly.

02

Current Status

This bot works as designed but is currently not deployed on any Discord servers because it isn't being hosted anywhere yet. The code is fully functional and only needs a token for the bot to work.

Lessons Learned

"The raw reaction events, as opposed to the cached reaction events, turned out to be absolutely necessary for reliability, as a reaction to a message after the bot restart will not be present in the bot's local cache. Storing the mapping between messages and roles on disk was absolutely necessary as well; otherwise, the restart would silently break every existing reaction message in the server."

Skills Learned

Python 3discord.pyDiscord APIFlaskEvent-Driven Programming