Skip to content

Playing around with writing a Discord bot in Ruby

License

Notifications You must be signed in to change notification settings

joshbuker/discord-bot

Repository files navigation

Ruby Discord Bot

Playing around with writing a Discord bot in Ruby.

Discord API Library: https://github.com/shardlab/discordrb

Usage

Instructions on how to run locally via dev container, or as a docker compose service.

Install Docker

Install Docker Desktop to be able to run the compose file locally, or install Docker using your existing methods.

https://docs.docker.com/desktop/

Install Nvidia Container Toolkit

To run, you'll either need to install the nvidia container toolkit, or disable the GPU reservation in the docker-compose.yml.

https://hub.docker.com/r/ollama/ollama

  1. Configure the repository
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey \
    | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list \
    | sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' \
    | sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
sudo apt-get update
  1. Install the NVIDIA Container Toolkit packages
sudo apt-get install -y nvidia-container-toolkit
  1. Configure Docker to use Nvidia driver
sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker

Create Discord Bot Token

From the Discord Developer Portal: https://discord.com/developers/applications

Create a new application for your bot. You'll want to save the token to your password manager, as it will be needed to test and run the bot.

Also, you'll want to enable all of the Privileged Gateway Intents, otherwise you will run into errors on startup due to permission mismatch.

Populate .env

cp .env.sample .env

Edit the .env file, and add the bot token from above.

Run using Dev Containers

If you have VS Code, and are familiar with dev containers, you can test the bot locally as you would normally for any other application.

If you need to stop the service and restart it, the entrypoint from terminal is:

bundle exec ruby lib/discord_bot.rb

Running using Docker Compose

If however, you're looking to use this as a service, you can run it using Docker Compose:

docker compose up -d

Follow the logs by grabbing the docker container id:

docker ps

Then tailing the logs (exit logs with ctrl+c):

docker logs -f container_id

To bring down the service and update it:

docker compose down
docker compose pull