Simple discord bot to do various stuff that was once handled by IRC bots but also does other, new things good too.
Written in a rather verbose way to make it easy for a beginner to read. There may be better ways to do things, but this is extremely simple to follow.
- Clone repo
- Copy example env file to .env
- Fill out .env with the stuff
- npm install
- node ./deploy-commands.js prod
- node ./bot.js prod
Default Value: BottyMcBotFace Name for the bot to use when referencing self
Default Value: false Set to true to enable debug functionality
Default Value: settings,coinflip,diceroll,leaderboard Comma separate list of commands to load. All commands are expected to be in the ./commands folder. Commands are dynamically imported so long as they register themselves in .env and the command file itself has a registerCommandModule call to generate the Discord command structures.
Default Value: ./data Path to JSON data to be loaded by commands
Default Value: ./temp Path to write temporary files
Default Value: $TEMP_PATH/reboot Path to file to write to signal a reboot to the OS
Default Value: 2048 Maximum number of log entries to keep in memory
Default Value: ./logs Folder to write logs to
Default Value: bot.log Log file to write ALL logs to
Default Value: discord_messages.log Log file to write discord messages to
Default Value: debug Logging level. See logger.ts enum LogLevel for available levels.
Default Value: python Path to python binary
Default Value: ../../scripts/reddit_reader.py Path to reddit reader python program
Default Value: Reddit app client id: https://www.reddit.com/prefs/apps
Default Value: Reddit app client secret: https://www.reddit.com/prefs/apps
Default Value: Reddit custom user agent for use in praw
Default Value: OpenAI API key to access data
Default Value: 8192 Max number of tokens to send during chat command
Default Value: 2048 Max number of message history to send during chat command
Default Value: Google maps API key. See https://developers.google.com/maps/documentation/javascript/get-api-key
Default Value: Open weather API key for the weather module. See https://openweathermap.org/appid to get yourself going.
Default Value: Discord bot token. You only need a single token if you don't want to setup a test environment for the bot (i.e. you just wanna use this with what it comes with)
Default Value: Discord app id for bot, see discord docs
Default Value: Comma separate list of guilds the bot will join. e.g. DISCORD_GUILD_ID="12345" is a single server. DISCORD_GUILD_ID="12345,67891" for two servers and so on.
Default Value: Clear slash commands on startup, recommend true for production use.
Default Value: Deploy slash commands to guilds, recommend true for production use
Default Value: false Deploy slash commands globally for bot, recommend to always be false
Super easy to run the bot in a VM or a raspberry pi or something.
Here's an example systemd configuration:
[Unit]
Description=BOOBSbot
After=multi-user.target
After=network-online.target
Wants=network-online.target
[Service]
WorkingDirectory=/home/user/dev/BottyMcBotFace
ExecStart=/usr/bin/node /home/user/dev/BottyMcBotFace/bot.js prod
StandardOutput=append:/var/log/bottymcbotface.log
StandardError=append:/var/log/bottymcbotface.log
Type=idle
Restart=always
RestartSec=15
RestartPreventExitStatus=0
TimeoutStopSec=10
[Install]
WantedBy=multi-user.target
bot.js contains all the discord bot code.
On startup, the env file is loaded and the modules are read in. These then dynamically register themselves based on what the .env file says should be loaded.
Each module is passed in the discord interaction when discord sends a matching command. Each command implements their own functionality independent of any other, though shared functionality (e.g. user settings) can be accessed via the common.js exports.