Skip to content

sqdnoises/discord.py-bot-template

Repository files navigation

discord.py bot template

a discord.py bot template that I use for my bots.
you're free to use this template if you like.

License

You should have received a copy of the LICENSE file with this source code.
This source code is licensed under the MIT License.

Setup dev environment

Note

Python 3.10+ should work with this bot. However it is recommended to use the latest Python 3.13 version.


Visual Studio Code extensions skip?

This bot was created, developed and meant to be used with the Visual Studio Code IDE. I have listed the recommended extensions for this bot to be used with Visual Studio Code below.

1. recommended Visual Studio Code extensions for this template

  • autoDocstring (by Nils Werner)
  • Better Color Picker (by Jannchie)
  • Black Formatter (by Microsoft)
  • Error lens (by Alexander)
  • GitHub Markdown Preview [Extension Pack] (by Matt Bierner)
    1. Markdown Preview GitHub Styling (by Matt Bierner)
    2. Markdown Emoji (by Matt Bierner)
    3. Markdown Checkboxes (by Matt Bierner)
    4. Markdown yaml Preamble (by Matt Bierner)
    5. Markdown Footnotes (by Matt Bierner)
    6. Markdown Preview Mermaid Support (by Matt Bierner)
  • Local History (by xyz)
  • Prisma (by Prisma)
  • Pylance (by Microsoft)
  • Python (by Microsoft)
  • Python Debugger (by Microsoft)
  • Python Environments (by Microsoft)
  • Python Indent (by Kevin Rose)
  • SQLite3 Editor (by yy0931)

2. other extensions I use

  • :emojisense: (by Matt Bierner)
  • AmazonQ (by Amazon Web Services)
  • Even Better TOML (by tamasfe)
  • Hex Editor (by Microsoft)
  • Live Share (by Microsoft)
  • Rainbow CSV (by mechatroner)
  • Remote - SSH (by Microsoft)

3. customization

  • Discord Rich Presence (by leonardssh)
  • Material Icon Theme (by Philipp Kief)
  • One Dark Pro (by binaryify) (I use this rarely)
  • Vitesse Theme (Anthony Fu) (I use this one the most)

Setting up a new project skip?

clone repo

First of all, git clone this repository:

git clone [email protected]:sqdnoises/discord.py-bot-template.git      # SSH
git clone https://github.com/sqdnoises/discord.py-bot-template.git  # HTTPS

setup your repo

Then reinitialize git .git (recommended to do this if you are starting a new project)

rm -rf .git  # Linux
git init
git branch -M main

Set the remote for a new project.

git remote add origin [email protected]:user/repo.git      # SSH
git remote add origin https://github.com/user/repo.git  # HTTPS

Make sure to replace user with your GitHub username and repo with your repository name.


Setting up & running the bot

create a .env file

Copy or rename the .env.example file to .env and configure .env appropriately.

This file contains secret environmental variables that are not meant to be shared with anyone.

You must configure the TOKEN and DATABASE_URL variable. Review other variables in the file for optional configurations.

install uv

Installation instructions here
(tl;dr: you can use pip install uv)

install python 3.13

$ uv python install 3.13
Searching for Python versions matching: Python 3.13
Installed 1 version in 3.13s
 + cpython-3.13.2-windows-x86_64-none

create a venv

$ uv venv
Using CPython 3.13.2
Creating virtual environment at: .venv
Activate with: source .venv/bin/activate

Run the shown venv activation command after the command is executed.

install requirements

$ uv pip install -r requirements.txt
Resolved 44 packages in 5.11s
Prepared 44 packages in 1m 50s
Installed 44 packages in 81ms
 + aiohappyeyeballs==2.6.1
 + aiohttp==3.11.13
...

setup the database

$ prisma db push
Environment variables loaded from .env
Prisma schema loaded from prisma\schema.prisma
Datasource "db": SQLite database "database.db" at "file:../database/database.db"

SQLite database database.db created at file:../database/database.db

Your database is now in sync with your Prisma schema. Done in 57ms

✔ Generated Prisma Client Python (v0.15.0) to .venv/lib/site-packages/prisma in 394ms

setup config

Open src/config.py in the editor of your choice and edit the config variables as needed.

Make sure you update ADMINS and LOG_CHANNEL to the appropriate values.

running

make sure your venv is activated and run:

python3 -m src  # py -m src/python -m src on Windows

running (with docker)

make sure docker is installed on your device/server and run:

docker compose up --build

or to run it detached:

docker compose up --build -d

post-run

Since the bot's profile has been set up for the first time, you need to tell Discord that your bot has slash commands.
For this you need to use !sync in a server the bot is in after running the bot for the first time.

This will synchronise all the slash commands in the code on Discord. Please restart your Discord app after this step to refetch all slash commands and then it should show the bot's slash commands when you start typing with / in a server the bot is in or anywhere if installed as an user app.


/ping command issues on a Linux host

If you host the bot on linux and use the /ping command, you will likely see the bot think forever or produce an error and see an Permission Error error in the console. Linux uses a kernel parameter that restricts who can create ping sockets.

For troubleshooting, please refer to: kyan001/ping3/TROUBLESHOOTING.md


© 2023-present SqdNoises
Licensed under the MIT License.

About

A bot template for all of my Discord bot projects. (in development)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published