Skip to content

Setup CodeGrind Bot Locally

Kevin Roman edited this page Jun 10, 2024 · 6 revisions

Getting Started

To set up a testing version of the bot, you need to follow the steps ahead:

Initial Setup

  1. Create a Discord application: Visit Discord developers applications and create an application.

  2. Add bot: Substitute <YOUR_BOT_CLIENT_ID> with your application's client id in the link below and press the link to add it to your selected server.

Make sure to also select all the 'Privileged Gateway Intents' on the Bot page.
https://discord.com/oauth2/authorize?client_id=<YOUR_BOT_CLIENT_ID>&scope=bot&permissions=8

Database and Browser Configuration

  1. Create a MongoDB database: Go to MongoDB, create a database, get the MongoDB URI (connection string), and add your IP address to the IP access list in the network access tab.

  2. Install Chrome: Chrome needs to be installed on your local machine. Identify the path to your chrome executable. For example, on Windows, it might be C:/Users/UserName/AppDataLocal/Google/Chrome/chrome.exe, whereas on Linux, it might be /usr/bin/chromium-browser. Remember to convert all backslashes (\) to forward slashes (/). This path will be used for executing the /stats command.

  3. Create a .env file: Within the project's root directory, create a .env file and add the environment variables given below. Replace <your_discord_bot_token>, <your_mongodb_uri>, and <path_to_chrome_executable> with your specific values.

    DISCORD_TOKEN=<your_discord_bot_token>
    MONGODB_URI=<your_mongodb_uri>
    TOPGG_TOKEN=null
    BROWSER_EXECUTABLE_PATH=<path_to_chrome_executable>
    LOGGING_CHANNEL_ID=<the_channel_id_to_log_to>
    DEVELOPER_DISCORD_ID=<your_discord_account_id>
    PRODUCTION=False
    

Virtual Environment Setup

  1. Python version: Ensure Python version 3.11 is installed on your computer.

  2. Setup a virtual environment: Install the Python venv module and set up a virtual environment. Here are the instructions for setting up a Python's virtual environment.

Make sure to call your virtual environment either .venv or venv when creating it, so that it is correctly ignored and not added to your commits.

To activate the virtual environment, navigate to the directory where the virtual environment is located and run the following commands:

  • For Windows:

    .\env\Scripts\activate
    
  • For Unix or macOS:

    source env/bin/activate
    
  1. Install required libraries: With the virtual environment activated, install all the necessary libraries using the following pip command:

    pip install -r requirements.txt
    

Running the Bot

  1. Run the bot: With all the setup complete, run the following command:

    python main.py
    

    The first time you run the bot, you will also need to write and send a message within a channel in the server your local bot is in that pings your bot and says "sync" (e.g. "@CodeGrind Bot sync") in order to sync and see all of your bot's commands. This only needs to be done when the bot is setup initially, or when new commands are implemented.

    After you've synced the commands, make sure to restart Discord.

    Whilst the program is running, you should now be able to execute bot commands on your server.