Ren-chan is a Discord bot for riichi club management, using Google Sheets for data store. Ren-chan is based on Ronhorn, as an effort to:
- isolate the club management commands from the mahjong analysis commands (e.g.,
/injustice) - eventually create an end-to-end club management bot with minimal setup
See Running the bot section on a "quick" start guide.
- manages recurring events
- creates each specified event if its next occurrence is missing
- posts a reminder some time before a recurring event's start.
- manages club memberships through the
registerslash commands- supports self-registration and registration by admin.
- enables score tracking through the
enter_scoresslash commands- supports entering scores for multiple game modes
- supports entering scores into multiple leaderboards
- validates the score entry before pushing it to the database).
First, cp config.template.env config.env.
- set up a bot account on Discord's developer portal (
New Application).- (SETTINGS → Bot) Privileged Gateway Intents:
SERVER MEMBERS INTENTANDMESSAGE CONTENT INTENT
- (SETTINGS → Bot) Privileged Gateway Intents:
- invite the bot to the respective servers. You can use the developer portal's OAuth2 URL Generator (SETTINGS → OAuth2 → URL Generator):
- Scopes: bot
- Bot Permissions:
- General Permissions: Manage Roles, View Channels, Manage Events, Create Events
- Text Permissions: Send Messages, Create Public Threads, Send Messages in Threads, Manage Messages, Manage Threads, Use External Emojis
- Current Bot Invite URL
- fill in the
Discord Stuffsection of config.env. The bot token can be obtained through (SETTINGS → Bot [→ Reset Token])
- set up a Google Cloud project. Enable Google Sheets API access, and "Create Credentials" for a service account (no need to give it access to the project). Generate a JSON key for that service account and save it as
gs_service_account.jsonin the root directory - make a suitable Google Spreadsheet (example)
- share the Spreadsheet with that service account.
- fill in the
Google Sheets Stuffsection of config.env
- fill in the
Ruleset Stuffsection of config.env -- should be pretty straightforward.
- update the EventPoster cog with the appropriate recurring events you want the bot to manage. In the future, these will be configured in a config file instead.
This repo does contain a GitHub Actions workflow that automates deployment to a server via SSH. Follow the below stops to enable this CD pipeline.
- ensure that you have the repo at
~/Ren-chan(deploy.ymlassumes this) and you have finish the previous setup steps. - generate your SSH key pair. Note that you should replace the
usernamepart with the target username on the server.ssh-keygen -t ed25519 -C "username" -f ~/.ssh/github_deploy_key
- if you can manually manage
~/.ssh/authorized_keyson your server, then append the content of~/.ssh/github_deploy_key.pubinto that file. HOWEVER, if you are not supposed to manually manage that file -- for example, you are using a Google Cloud VM -- then you should follow your cloud platform's instructions.- For Google Cloud VM, you want to add the SSH public key to the VM instance like so. Note that if you are pasting the content of your public key to Google Cloud console's SSH box, you need to ensure that it ends with the target username (hence the note in the previous step).
- ensure that your target user has passwordless
sudoaccess. Check withsudo ls /root. If password is required, changing the setting withsudo visudo, and add the following to the end of the file:username ALL=(ALL) NOPASSWD:ALL - finally, you should configure the repository with the following Actions secrets (
deploy.ymldepends on them):SERVER_IP: the external IP of your serverSERVER_SSH_KEY: the content of~/.ssh/github_deploy_keySERVER_USER: the same username as you configured earlier
- ensure you complete all steps in the setup.
- run
./deploy/deploy.sh(see repo structure for a breakdown ofdeploy.sh). - IF this is your first time deploying the bot to a server, OR IF you made a change to the I/O of any slash commands, then you need to run the
rc/syncregular command to sync the slash commands (just post this as a message in the server and ensure it's visible to the bot).rc/restartis another convenient admin command to restart the bot. For all bot admin commands, check bot.py.
bot.py: entry point of the Discord bot. Does the following:- imports
global_stuff.py, which does the following:- load all the environment variabels from
config.env
- load all the environment variabels from
- set up the non-slash Discord commands
- set up command error handlers (both slash and non-slash)
- imports
deploy/: deployment-related scripts and service units.ren-chan@.service: template for the system-wide service (it cannot be a user-level service because it would die with the user session).- The input specifier determines the user running the service.
- Restarts the bot automatically 5 seconds after it dies.
deploy.sh: deploys the bot as a system-wide service, under the current user.- pulls from remote
- installs latest depdendencies according to
Pipfile.lock - sets up the service according to
ren-chan@.serviceunder the current user.
stop_service.sh: stops the service (i.e., shuts down the bot)undeploy.sh: stops and deletes the service, uninstalls the pip environment, and removes the log files.
/ext/: Discord bot extensions (each extension is a suite of slash commands and their helper functions)EventPoster: automates posting regular events and reminders for those events.Utilities: various utilities, including recording in-person games, managing club membership, etc.