This document outlines the steps to set up and run this Discord bot on your own machine.
-
- Python Installation
- Required Python Libraries
- Discord Bot Setup
- SQLite (optional)
-
- Creating a
.env
File - Required Environment Variables
- Creating a
-
- Installing Python Libraries
-
- Creating a Discord Bot
- Adding the Bot to a Server
- Setting Permissions for the Bot
-
- SQLite Configuration (if applicable)
- Table Creation
-
- Executing the bot
-
- Common Errors and Fixes
-
- Steps to test commands in a test server
-
- Contribution Guidelines (optional)
Ensure that Python (version 3.8 or higher) is installed on your machine. You can download it from Python's official website.
You will need the following Python libraries:
discord.py
python-dotenv
You'll need a bot token from Discord to run this bot.
You need to create a .env
file in your project directory to store sensitive credentials, such as your bot token.
Example .env
file:
GUILD=123ABC
FEEDBACK_CH=456DEF
DISCORD_API_TOKEN=789EFG
Important: This .env
file should be added to your .gitignore
file to prevent exposing credentials in GitHub.
GUILD
: The Discord Server ID where the bot will operate (used for testing or production).FEEDBACK_CH
: The Discord channel where logs or feedback will be posted.DISCORD_API_TOKEN
: The token for your bot, obtained from the Discord Developer Portal.
Install the required Python libraries using the requirements.txt
file or manually with pip:
pip install -r requirements.txt
or manually, refer to requirements.txt
pip install discord.py
pip install python-dotenv
- Go to the Discord Developer Portal.
- Click on "New Application" to create a new bot.
- Give your bot a name and click "Create."
- In the left sidebar, go to "Bot" and click on "Add Bot."
- Confirm by clicking "Yes, do it!" Your bot will be created.
- Copy the bot token by clicking "Reset Token" and saving it to your
.env
file asDISCORD_API_TOKEN
. - Under "Bot Permissions," select the required permissions your bot needs:
Presence Intent
- for context_menu functionServer Members Intent
- for verifiying member is in guild (this bot can handle multiple server installs and maintains which guild player data is for)Message Content Intent
- for use of embeds.
- In the Discord Developer Portal, go to the "Installation" tab and select "Install Link."
- Under "Default Install Settings," select the required permissions your bot needs
- User Install: Scopes, applications.commands
- scopes:
- application.commands
- scopes:
- Guild Install:
- scopes:
- application.commands
- bot
- Permissions:
- Send Messages
- scopes:
- User Install: Scopes, applications.commands
- Copy the generated URL and paste it into your browser.
- Select the server where you want to add the bot, and click "Authorize."
- Once the bot is added to your server, go to your Discord server settings.
- Go to "Roles" and find your bot's role.
- Assign the required permissions for the bot, making sure it has permissions to the following in the channels you see fit for testing.
- send messages
- see reacts
- Save the settings.
-
This bot uses an SQLite database by default to store player and guild information.
-
Ensure that your project has SQLite installed by running:
pip install sqlite3
-
The bot will automatically create and manage the SQLite database (
candy_game.db
) if it does not already exist in the dir whichbot.py
was called.
-
The bot will automatically create the required tables in the SQLite database. Review db init file
-
The following tables are created:
- players: Stores player-specific data like candy count, successful/failed steals, and active status.
- guild_settings: Stores guild-specific settings such as event and admin channels.
- lottery_pool: Stores the amount of candy available for the lottery event.
- role_access: Stores which roles have access to restricted bot commands.
-
Ensure that the bot has write access to the directory where the SQLite database is stored.
-
Before running the bot, ensure all required libraries are installed:
pip install -r requirements.txt
-
Ensure that your
.env
file is correctly set up with the required environment variables:GUILD
: Discord Server ID used for testingFEEDBACK_CH
: Discord channel for additional logs (optional)DISCORD_API_TOKEN
: Your bot's API token
-
Execute the bot by running the following command in your terminal or command prompt:
python bot.py
-
If the bot starts successfully, you should see a confirmation message in the terminal indicating that the bot is connected and ready to interact with the Discord server. Please take caution of syncing to a guild and be mindful to only sync when you have updated a command ;).
Example output on start:
WARNING - discord.client : PyNaCl is not installed, voice will NOT be supported Initializing database... Loading spooky messages... Loading group commands... Syncing tree... INFO - bot : Attempting to sync commands for guild ID: ##### INFO - bot : Successfully synced 4 commands for guild ID: ###### INFO - bot : Command synced: zmod - Moderator Commands INFO - bot : - Option: set (AppCommandOptionType.subcommand_group) - Set commands INFO - bot : - Option: get (AppCommandOptionType.subcommand_group) - get commands INFO - bot : - Option: remove (AppCommandOptionType.subcommand_group) - Remove commands INFO - bot : - Option: update (AppCommandOptionType.subcommand_group) - Update commands INFO - bot : - Option: reset (AppCommandOptionType.subcommand_group) - Reset commands INFO - bot : Command synced: trick - Trick a player into giving you candy! INFO - bot : - Option: member (AppCommandOptionType.user) - … INFO - bot : Command synced: Join Game - INFO - bot : Command synced: Trick Player -
-
In the Discord server, you can now use the bot’s commands as specified.
-
Missing API Token Error:
- Error: "DISCORD_API_TOKEN not set"
- Fix: Ensure that the
.env
file includes the correctDISCORD_API_TOKEN
value and that it's loaded properly. Check the setup insettings.py
.
-
Invalid Discord API Token:
- Error: "401 Unauthorized"
- Fix: Verify that your bot’s API token is correctly set and hasn’t been regenerated. If the token was regenerated in the Discord Developer Portal, make sure to update it in the
.env
file.
-
Bot not responding to commands:
- Error: No interaction from the bot when commands are used.
- Fix: Ensure the bot has the required permissions in your Discord server. Make sure the bot is added with the necessary scope (
bot
andapplications.commands
) and the necessary intents are enabled in the Discord Developer Portal.
-
Database-related errors:
- Error: "SQLite database not found" or "Table not found."
- Fix: Verify that the SQLite database is correctly configured and the required tables have been created. You can check the database setup section for help.
-
Create a Test Server:
- On Discord, create a new server to use as a test environment. You can add your bot to this server without affecting any live servers.
-
Add the Bot to the Test Server:
- Use the OAuth2 URL generated in the Discord Developer Portal to invite the bot to your test server. Make sure to include the necessary permissions and intents.
-
Use the Commands:
- Start interacting with the bot by running its commands. You can test commands like
/help
or/join
to verify everything is functioning as expected.
- Start interacting with the bot by running its commands. You can test commands like
-
Check Logs:
- If you set up the
FEEDBACK_CH
environment variable for logging and it was been set in code, check the designated channel for log outputs. Alternatively, you can check the terminal for detailed debug logs. - look at
/logs
dir for output - refer to terminal ouput
- If you set up the
-
Fork the Repository:
- Start by forking the repository on GitHub. This will allow you to make changes to your version without affecting the main repository.
-
Clone the Repository:
- Clone your forked repository locally using:
git clone https://github.com/your-username/repository-name.git
- Clone your forked repository locally using:
-
Create a New Branch:
- Before making changes, create a new branch to organize your contributions:
git checkout -b feature-branch-name
- Before making changes, create a new branch to organize your contributions:
-
Write Code:
- Implement the new feature, bug fix, or improvement in your new branch. Make sure to follow the coding standards set by the project.
-
Test Your Changes:
- Test your changes locally to ensure they work as expected. Be sure to test against a test Discord server, as outlined in the Testing section.
-
Submit a Pull Request:
- Push your changes to your forked repository:
git push origin feature-branch-name
- Then, submit a pull request (PR) to the main repository.
- Push your changes to your forked repository:
-
Review and Feedback:
- The repository maintainers will review your PR. They may provide feedback or request changes. Make sure to address any comments before your contribution is merged.
-
Contributions Welcome:
- All contributions, including bug reports, feature requests, and code improvements, are welcome. Make sure to adhere to the code of conduct and contribution guidelines.