Skip to content

Commit e39b9fa

Browse files
committed
feat: Add Docker (Compose) support
1 parent d707a5d commit e39b9fa

16 files changed

+86
-63
lines changed

.dockerignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.git/
2+
.github/
3+
database/
4+
venv/
5+
.env*
6+
CODE_OF_CONDUCT.md
7+
CONTRIBUTING.md
8+
UPDATES.md

.env.example

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
TOKEN=YOUR_BOT_TOKEN_HERE
1+
TOKEN=YOUR_BOT_TOKEN_HERE
2+
PREFIX=YOUR_BOT_PREFIX_HERE
3+
INVITE_LINK=YOUR_BOT_INVITE_LINK_HERE

CONTRIBUTING.md

+11-13
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22

33
Your input is amazing! Making contributing to this project as easy and transparent as possible is one of the most important side, this includes:
44

5-
- Reporting a bug
6-
- Discussing the current state of the code
7-
- Submitting a fix
8-
- Proposing new features
9-
- Becoming a maintainer
5+
- Reporting a bug
6+
- Discussing the current state of the code
7+
- Submitting a fix
8+
- Proposing new features
9+
- Becoming a maintainer
1010

1111
## Wanted changes
1212

13-
- New features
14-
- Better documentation
15-
- Fixing of spelling and grammatical issues
13+
- New features
14+
- Better documentation
15+
- Fixing of spelling and grammatical issues
1616

1717
## Unwanted changes
1818

19-
- Whitespaces and punctuation changes
20-
- Word changes using synonyms
21-
- Entire rewrites of the project, or parts of the project - unless approved first by a maintainer
19+
- Whitespaces and punctuation changes
20+
- Word changes using synonyms
21+
- Entire rewrites of the project, or parts of the project - unless approved first by a maintainer
2222

2323
## All code changes happen through pull requests
2424

@@ -34,8 +34,6 @@ Pull requests are the best way to propose changes to the codebase. We actively w
3434

3535
This project uses [`Conventional Commits 1.0.0`](https://conventionalcommits.org/en/v1.0.0/) hence your commit messages **must** follow the same convention or your contributions will be ignored, refused or assigned to another user or maintainer.
3636

37-
It would be more than welcome to keep your contributions as a single commit rather than, for examples, 20 `"fix: Stuff"` commits in-between. You may use multiple commmits if you believe the changes made in these commmits have nothing, or close to nothing, in common - feel free to ask a maintainer on whether or not it should be a single commit or not.
38-
3937
## Create a GitHub [Issue](https://github.com/kkrypt0nn/Python-Discord-Bot-Template/issues) and **then** a pull request
4038

4139
Start contributing by first [opening a new issue](https://github.com/kkrypt0nn/Python-Discord-Bot-Template/issues/new/choose). Once that is done, you can create a pull request for the issue.

Dockerfile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM python:3.12.9-slim-bookworm
2+
3+
WORKDIR /bot
4+
COPY . /bot
5+
6+
RUN python -m pip install -r requirements.txt
7+
8+
ENTRYPOINT [ "python", "bot.py" ]

README.md

+16-20
Original file line numberDiff line numberDiff line change
@@ -73,28 +73,14 @@ Alternatively you can do the following:
7373

7474
## How to set up
7575

76-
To set up the bot it was made as simple as possible.
76+
To set up the token you will have to make use of the [`.env.example`](.env.example) file; you should rename it to `.env` and replace the `YOUR_BOT...` content with your actual values that match for your bot.
7777

78-
### `config.json` file
79-
80-
There is [`config.json`](config.json) file where you can put the
81-
needed things to edit.
82-
83-
Here is an explanation of what everything is:
84-
85-
| Variable | What it is |
86-
| ------------------------- | ---------------------------------------------- |
87-
| YOUR_BOT_PREFIX_HERE | The prefix you want to use for normal commands |
88-
| YOUR_BOT_INVITE_LINK_HERE | The link to invite the bot |
89-
90-
### `.env` file
91-
92-
To set up the token you will have to make use of the [`.env.example`](.env.example) file, you should rename it to `.env` and replace `YOUR_BOT_TOKEN_HERE` with your actual bot's token.
93-
94-
Alternatively you can simply create an environment variable named `TOKEN`.
78+
Alternatively you can simply create a system environment variable with the same names and their respective value.
9579

9680
## How to start
9781

82+
### The _"usual"_ way
83+
9884
To start the bot you simply need to launch, either your terminal (Linux, Mac & Windows), or your Command Prompt (
9985
Windows)
10086
.
@@ -111,7 +97,17 @@ After that you can start it with
11197
python bot.py
11298
```
11399

114-
> **Note** You may need to replace `python` with `py`, `python3`, `python3.11`, etc. depending on what Python versions you have installed on the machine.
100+
> **Note**: You may need to replace `python` with `py`, `python3`, `python3.11`, etc. depending on what Python versions you have installed on the machine.
101+
102+
### Docker
103+
104+
Support to start the bot in a Docker container has been added. After having [Docker](https://docker.com) installed on your machine, you can simply execute:
105+
106+
```
107+
docker compose up -d --build
108+
```
109+
110+
> **Note**: `-d` will make the container run in detached mode, so in the background.
115111
116112
## Issues or Questions
117113

@@ -129,7 +125,7 @@ the [tags on this repository](https://github.com/kkrypt0nn/Python-Discord-Bot-Te
129125

130126
## Built With
131127

132-
- [Python 3.11.5](https://www.python.org/)
128+
- [Python 3.12.9](https://www.python.org/)
133129

134130
## License
135131

UPDATES.md

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
Here is the list of all the updates that I made on this template.
44

5+
### Version 6.3.0 (05 March 2025)
6+
7+
- Added support for Docker, `docker compose up` will spin up the bot and install the requirements
8+
- Pinned `discord.py` version in the `requirements.txt` file
9+
- Removed the `config.json` file and replaced the rest with `.env` just like the token
10+
511
### Version 6.2.0 (21 July 2024)
612

713
- Added a new `feedback` command for showcasing the use of modals (popups)

bot.py

+9-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Description:
44
🐍 A simple template to start to code your own and personalized Discord bot in Python
55
6-
Version: 6.2.0
6+
Version: 6.3.0
77
"""
88

99
import json
@@ -21,11 +21,7 @@
2121

2222
from database import DatabaseManager
2323

24-
if not os.path.isfile(f"{os.path.realpath(os.path.dirname(__file__))}/config.json"):
25-
sys.exit("'config.json' not found! Please add it and try again.")
26-
else:
27-
with open(f"{os.path.realpath(os.path.dirname(__file__))}/config.json") as file:
28-
config = json.load(file)
24+
load_dotenv()
2925

3026
"""
3127
Setup bot intents (events restrictions)
@@ -125,21 +121,22 @@ def format(self, record):
125121
class DiscordBot(commands.Bot):
126122
def __init__(self) -> None:
127123
super().__init__(
128-
command_prefix=commands.when_mentioned_or(config["prefix"]),
124+
command_prefix=commands.when_mentioned_or(os.getenv("PREFIX")),
129125
intents=intents,
130126
help_command=None,
131127
)
132128
"""
133129
This creates custom bot variables so that we can access these variables in cogs more easily.
134130
135-
For example, The config is available using the following code:
136-
- self.config # In this class
137-
- bot.config # In this file
138-
- self.bot.config # In cogs
131+
For example, The logger is available using the following code:
132+
- self.logger # In this class
133+
- bot.logger # In this file
134+
- self.bot.logger # In cogs
139135
"""
140136
self.logger = logger
141-
self.config = config
142137
self.database = None
138+
self.bot_prefix = os.getenv("PREFIX")
139+
self.invite_link = os.getenv("INVITE_LINK")
143140

144141
async def init_db(self) -> None:
145142
async with aiosqlite.connect(
@@ -287,7 +284,5 @@ async def on_command_error(self, context: Context, error) -> None:
287284
raise error
288285

289286

290-
load_dotenv()
291-
292287
bot = DiscordBot()
293288
bot.run(os.getenv("TOKEN"))

cogs/fun.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Description:
44
🐍 A simple template to start to code your own and personalized Discord bot in Python
55
6-
Version: 6.2.0
6+
Version: 6.3.0
77
"""
88

99
import random

cogs/general.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Description:
44
🐍 A simple template to start to code your own and personalized Discord bot in Python
55
6-
Version: 6.2.0
6+
Version: 6.3.0
77
"""
88

99
import platform
@@ -87,7 +87,6 @@ async def grab_id(
8787
name="help", description="List all commands the bot has loaded."
8888
)
8989
async def help(self, context: Context) -> None:
90-
prefix = self.bot.config["prefix"]
9190
embed = discord.Embed(
9291
title="Help", description="List of available commands:", color=0xBEBEFE
9392
)
@@ -99,7 +98,7 @@ async def help(self, context: Context) -> None:
9998
data = []
10099
for command in commands:
101100
description = command.description.partition("\n")[0]
102-
data.append(f"{prefix}{command.name} - {description}")
101+
data.append(f"{command.name} - {description}")
103102
help_text = "\n".join(data)
104103
embed.add_field(
105104
name=i.capitalize(), value=f"```{help_text}```", inline=False
@@ -127,7 +126,7 @@ async def botinfo(self, context: Context) -> None:
127126
)
128127
embed.add_field(
129128
name="Prefix:",
130-
value=f"/ (Slash Commands) or {self.bot.config['prefix']} for normal commands",
129+
value=f"/ (Slash Commands) or {self.bot.bot_prefix} for normal commands",
131130
inline=False,
132131
)
133132
embed.set_footer(text=f"Requested by {context.author}")
@@ -192,7 +191,7 @@ async def invite(self, context: Context) -> None:
192191
:param context: The hybrid command context.
193192
"""
194193
embed = discord.Embed(
195-
description=f"Invite me by clicking [here]({self.bot.config['invite_link']}).",
194+
description=f"Invite me by clicking [here]({self.bot.invite_link}).",
196195
color=0xD75BF4,
197196
)
198197
try:

cogs/moderation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Description:
44
🐍 A simple template to start to code your own and personalized Discord bot in Python
55
6-
Version: 6.2.0
6+
Version: 6.3.0
77
"""
88

99
import os

cogs/owner.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Description:
44
🐍 A simple template to start to code your own and personalized Discord bot in Python
55
6-
Version: 6.2.0
6+
Version: 6.3.0
77
"""
88

99
import discord

cogs/template.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Description:
44
🐍 A simple template to start to code your own and personalized Discord bot in Python
55
6-
Version: 6.2.0
6+
Version: 6.3.0
77
"""
88

99
from discord.ext import commands

config.json

-4
This file was deleted.

database/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Description:
44
🐍 A simple template to start to code your own and personalized Discord bot in Python
55
6-
Version: 6.2.0
6+
Version: 6.3.0
77
"""
88

99
import aiosqlite

docker-compose.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
services:
2+
discord-bot:
3+
build: .
4+
image: python-discord-bot-template
5+
env_file:
6+
- .env
7+
volumes:
8+
- ./database:/bot/database
9+
- ./discord.log:/bot/discord.log
10+
11+
# Alternatively you can set the environment variables as such:
12+
# /!\ The token shouldn't be written here, as this file is not ignored from Git /!\
13+
# environment:
14+
# - PREFIX=YOUR_BOT_PREFIX_HERE
15+
# - INVITE_LINK=YOUR_BOT_INVITE_LINK_HERE

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
aiohttp
22
aiosqlite
3-
discord.py==2.4.0
3+
discord.py==2.5.2
44
python-dotenv

0 commit comments

Comments
 (0)