Skip to content

Commit f1ca373

Browse files
committed
Template v2.8
* Blacklisted users are now **saved** in the file * Moved config file to JSON * Moved the blacklist in a separate file (`blacklist.json`) * The colors are no longer saved in the config file
1 parent e0b93ad commit f1ca373

File tree

15 files changed

+170
-128
lines changed

15 files changed

+170
-128
lines changed

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ I would've been happy if there were any template existing. However, there wasn't
1717
decided to create my own template to let <b>you</b> guys create your discord bot easily.
1818

1919
Please note that this template is not supposed to be the best template, but a good template to start learning how
20-
discord.py works and to make your own bot in a simple way.
20+
discord.py works and to make your own bot easily.
2121

22-
If you plan to use this template to make your own template or bot, you **have to** give me credits somewhere and keep
23-
the copyright notice on the files.
24-
See [the license file](https://github.com/kkrypt0nn/Python-Discord-Bot-Template/blob/master/LICENSE.md) for more
25-
information.
22+
If you plan to use this template to make your own template or bot, you **have to**:
2623

27-
## Authors
24+
- Keep the credits, and a link to this repository in all the files that contains my code
25+
- Keep the same license
2826

29-
* **[Krypton (@kkrypt0nn)](https://github.com/kkrypt0nn)**
27+
See [the license file](https://github.com/kkrypt0nn/Python-Discord-Bot-Template/blob/master/LICENSE.md) for more
28+
information, I reserve the right to take down any repository that does not meet these requirements.
3029

3130
## Support
3231

@@ -40,8 +39,8 @@ All the updates of the template are available [here](UPDATES.md).
4039

4140
## Disclaimer
4241

43-
**Nothing** is being saved during runtime in the configuration file, please take a look at the [TODO file](TODO.md) to
44-
know if this has been implemented.
42+
When using the template you confirm that you have read the [license](LICENSE.md) and comprehend that I can take down
43+
your repository if you do not meet these requirements.
4544

4645
Please do not open issues or pull requests about things that are written in the [TODO file](TODO.md), they are **
4746
already** under work for the version 3.0 of the template.
@@ -64,18 +63,19 @@ Alternatively you can do the following:
6463

6564
## How to set up
6665

67-
To set up the bot I made it as simple as possible. I now created a [config.yaml](config.yaml) file where you can put the
66+
To set up the bot I made it as simple as possible. I now created a [config.json](config.json) file where you can put the
6867
needed things to edit.
6968

7069
Here is an explanation of what everything is:
7170

72-
| Variable | What it is |
73-
| ----------------------| ----------------------------------------------------------------------|
74-
| YOUR_BOT_PREFIX_HERE | The prefix(es) of your bot |
75-
| YOUR_BOT_TOKEN_HERE | The token of your bot |
76-
| APPLICATION_ID | The application ID of your bot |
77-
| OWNERS | The user ID of all the bot owners |
78-
| BLACKLIST | The user ID of all the users who can't use the bot |
71+
| Variable | What it is |
72+
| ------------------------- | ----------------------------------------------------------------------|
73+
| YOUR_BOT_PREFIX_HERE | The prefix(es) of your bot |
74+
| YOUR_BOT_TOKEN_HERE | The token of your bot |
75+
| YOUR_APPLICATION_ID_HERE | The application ID of your bot |
76+
| OWNERS | The user ID of all the bot owners |
77+
78+
In the [blacklist](blacklist.json) file you now can add IDs (as integers) in the `ids` list.
7979

8080
## How to start
8181

TODO.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
* [ ] Add a lock command that will let administrators temporary lock a channel, useful in case of a raid
77
* [ ] Add an archive command that lets administrators archive a text channel in a text file
88
* [ ] Add an issue and pull request template for the repository
9-
* [ ] Be able to save data to JSON (blacklist, owners, etc.)
10-
* [ ] Move config to JSON
9+
* [X] Be able to save data to JSON (blacklist, owners, etc.)
10+
* [X] Move config to JSON

UPDATES.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

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

5+
### Version 2.8
6+
7+
* Blacklisted users are now **saved** in the file
8+
* Moved config file to JSON
9+
* Moved the blacklist in a separate file (`blacklist.json`)
10+
* The colors are no longer saved in the config file
11+
512
### Version 2.7
613

714
* Added a check for `commands.MissingRequiredArgument` in the error handler
@@ -12,8 +19,8 @@ Here is the list of all the updates that I made on this template.
1219
* Removed an unnecessary `self.bot.logout()` statement
1320
* Removed the `dick` command, as I want to keep this template safe for work
1421
* Renamed the names of the arguments in some commands
15-
* The bot now **tries** to send an embed in the private message of the command author for the `invite` and `server` commands, if this was not successful it will be sent in the channel
16-
22+
* The bot now **tries** to send an embed in the private message of the command author for the `invite` and `server`
23+
commands, if this was not successful it will be sent in the channel
1724

1825
### Version 2.6
1926

blacklist.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"ids": [
3+
]
4+
}

bot.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33
Description:
44
This is a template to create your own discord bot in python.
55
6-
Version: 2.7
6+
Version: 2.8
77
"""
88

9+
import json
910
import os
1011
import platform
1112
import random
1213
import sys
1314

1415
import discord
15-
import yaml
1616
from discord.ext import commands, tasks
1717
from discord.ext.commands import Bot
1818

19-
if not os.path.isfile("config.yaml"):
20-
sys.exit("'config.yaml' not found! Please add it and try again.")
19+
if not os.path.isfile("config.json"):
20+
sys.exit("'config.json' not found! Please add it and try again.")
2121
else:
22-
with open("config.yaml") as file:
23-
config = yaml.load(file, Loader=yaml.FullLoader)
22+
with open("config.json") as file:
23+
config = json.load(file)
2424

2525
"""
2626
Setup bot intents (events restrictions)
@@ -97,8 +97,9 @@ async def on_message(message):
9797
if message.author == bot.user or message.author.bot:
9898
return
9999
# Ignores if a command is being executed by a blacklisted user
100-
101-
if message.author.id in config["blacklist"]:
100+
with open("blacklist.json") as file:
101+
blacklist = json.load(file)
102+
if message.author.id in blacklist["ids"]:
102103
return
103104
await bot.process_commands(message)
104105

@@ -123,23 +124,23 @@ async def on_command_error(context, error):
123124
embed = discord.Embed(
124125
title="Hey, please slow down!",
125126
description=f"You can use this command again in {f'{round(hours)} hours' if round(hours) > 0 else ''} {f'{round(minutes)} minutes' if round(minutes) > 0 else ''} {f'{round(seconds)} seconds' if round(seconds) > 0 else ''}.",
126-
color=config["error"]
127+
color=0xE02B2B
127128
)
128129
await context.send(embed=embed)
129130
elif isinstance(error, commands.MissingPermissions):
130131
embed = discord.Embed(
131132
title="Error!",
132133
description="You are missing the permission `" + ", ".join(
133134
error.missing_perms) + "` to execute this command!",
134-
color=config["error"]
135+
color=0xE02B2B
135136
)
136137
await context.send(embed=embed)
137138
elif isinstance(error, commands.MissingRequiredArgument):
138139
embed = discord.Embed(
139140
title="Error!",
140141
description=str(error).capitalize(),
141142
# We need to capitalize because the command arguments have no capital letter in the code.
142-
color=config["error"]
143+
color=0xE02B2B
143144
)
144145
await context.send(embed=embed)
145146
raise error

cogs/fun.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77
"""
88

99
import asyncio
10+
import json
1011
import os
1112
import random
1213
import sys
1314

1415
import aiohttp
1516
import discord
16-
import yaml
1717
from discord.ext import commands
1818
from discord.ext.commands import BucketType
1919

20-
if not os.path.isfile("config.yaml"):
21-
sys.exit("'config.yaml' not found! Please add it and try again.")
20+
if not os.path.isfile("config.json"):
21+
sys.exit("'config.json' not found! Please add it and try again.")
2222
else:
23-
with open("config.yaml") as file:
24-
config = yaml.load(file, Loader=yaml.FullLoader)
23+
with open("config.json") as file:
24+
config = json.load(file)
2525

2626

2727
class Fun(commands.Cog, name="fun"):
@@ -51,13 +51,13 @@ async def dailyfact(self, context):
5151
async with session.get("https://uselessfacts.jsph.pl/random.json?language=en") as request:
5252
if request.status == 200:
5353
data = await request.json()
54-
embed = discord.Embed(description=data["text"], color=config["main_color"])
54+
embed = discord.Embed(description=data["text"], color=0xD75BF4)
5555
await context.send(embed=embed)
5656
else:
5757
embed = discord.Embed(
5858
title="Error!",
5959
description="There is something wrong with the API, please try again later",
60-
color=config["error"]
60+
color=0xE02B2B
6161
)
6262
await context.send(embed=embed)
6363
# We need to reset the cool down since the user didn't got his daily fact.
@@ -75,7 +75,7 @@ async def rock_paper_scissors(self, context):
7575
"🧻": 1,
7676
"✂": 2
7777
}
78-
embed = discord.Embed(title="Please choose", color=config["warning"])
78+
embed = discord.Embed(title="Please choose", color=0xF59E42)
7979
embed.set_author(name=context.author.display_name, icon_url=context.author.avatar_url)
8080
choose_message = await context.send(embed=embed)
8181
for emoji in reactions:
@@ -93,30 +93,30 @@ def check(reaction, user):
9393
bot_choice_emote = random.choice(list(reactions.keys()))
9494
bot_choice_index = reactions[bot_choice_emote]
9595

96-
result_embed = discord.Embed(color=config["success"])
96+
result_embed = discord.Embed(color=0x42F56C)
9797
result_embed.set_author(name=context.author.display_name, icon_url=context.author.avatar_url)
9898
await choose_message.clear_reactions()
9999

100100
if user_choice_index == bot_choice_index:
101101
result_embed.description = f"**That's a draw!**\nYou've chosen {user_choice_emote} and I've chosen {bot_choice_emote}."
102-
result_embed.colour = config["warning"]
102+
result_embed.colour = 0xF59E42
103103
elif user_choice_index == 0 and bot_choice_index == 2:
104104
result_embed.description = f"**You won!**\nYou've chosen {user_choice_emote} and I've chosen {bot_choice_emote}."
105-
result_embed.colour = config["success"]
105+
result_embed.colour = 0x42F56C
106106
elif user_choice_index == 1 and bot_choice_index == 0:
107107
result_embed.description = f"**You won!**\nYou've chosen {user_choice_emote} and I've chosen {bot_choice_emote}."
108-
result_embed.colour = config["success"]
108+
result_embed.colour = 0x42F56C
109109
elif user_choice_index == 2 and bot_choice_index == 1:
110110
result_embed.description = f"**You won!**\nYou've chosen {user_choice_emote} and I've chosen {bot_choice_emote}."
111-
result_embed.colour = config["success"]
111+
result_embed.colour = 0x42F56C
112112
else:
113113
result_embed.description = f"**I won!**\nYou've chosen {user_choice_emote} and I've chosen {bot_choice_emote}."
114-
result_embed.colour = config["error"]
114+
result_embed.colour = 0xE02B2B
115115
await choose_message.add_reaction("🇱")
116116
await choose_message.edit(embed=result_embed)
117117
except asyncio.exceptions.TimeoutError:
118118
await choose_message.clear_reactions()
119-
timeout_embed = discord.Embed(title="Too late", color=config["error"])
119+
timeout_embed = discord.Embed(title="Too late", color=0xE02B2B)
120120
timeout_embed.set_author(name=context.author.display_name, icon_url=context.author.avatar_url)
121121
await choose_message.edit(embed=timeout_embed)
122122

cogs/general.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414

1515
import aiohttp
1616
import discord
17-
import yaml
1817
from discord.ext import commands
1918

20-
if not os.path.isfile("config.yaml"):
21-
sys.exit("'config.yaml' not found! Please add it and try again.")
19+
if not os.path.isfile("config.json"):
20+
sys.exit("'config.json' not found! Please add it and try again.")
2221
else:
23-
with open("config.yaml") as file:
24-
config = yaml.load(file, Loader=yaml.FullLoader)
22+
with open("config.json") as file:
23+
config = json.load(file)
2524

2625

2726
class general(commands.Cog, name="general"):
@@ -35,7 +34,7 @@ async def info(self, context):
3534
"""
3635
embed = discord.Embed(
3736
description="Used Krypton's template",
38-
color=config["success"]
37+
color=0x42F56C
3938
)
4039
embed.set_author(
4140
name="Bot Information"
@@ -80,7 +79,7 @@ async def serverinfo(self, context):
8079
embed = discord.Embed(
8180
title="**Server Name:**",
8281
description=f"{server}",
83-
color=config["success"]
82+
color=0x42F56C
8483
)
8584
embed.set_thumbnail(
8685
url=server.icon_url
@@ -118,7 +117,7 @@ async def ping(self, context):
118117
embed = discord.Embed(
119118
title="🏓 Pong!",
120119
description=f"The bot latency is {round(self.bot.latency * 1000)}ms.",
121-
color=config["success"]
120+
color=0x42F56C
122121
)
123122
await context.send(embed=embed)
124123

@@ -129,7 +128,7 @@ async def invite(self, context):
129128
"""
130129
embed = discord.Embed(
131130
description=f"Invite me by clicking [here](https://discordapp.com/oauth2/authorize?&client_id={config['application_id']}&scope=bot&permissions=470150263).",
132-
color=config['main_color']
131+
color=0xD75BF4
133132
)
134133
try:
135134
# To know what permissions to give to your bot, please see here: https://discordapi.com/permissions.html and remember to not give Administrator permissions.
@@ -145,7 +144,7 @@ async def server(self, context):
145144
"""
146145
embed = discord.Embed(
147146
description=f"Join the support server for the bot by clicking [here](https://discord.gg/HzJ3Gfr).",
148-
color=config['main_color']
147+
color=0xD75BF4
149148
)
150149
try:
151150
await context.author.send(embed=embed)
@@ -161,7 +160,7 @@ async def poll(self, context, *, title):
161160
embed = discord.Embed(
162161
title="A new poll has been created!",
163162
description=f"{title}",
164-
color=config["success"]
163+
color=0x42F56C
165164
)
166165
embed.set_footer(
167166
text=f"Poll created by: {context.message.author} • React to vote!"
@@ -184,7 +183,7 @@ async def eight_ball(self, context, *, question):
184183
embed = discord.Embed(
185184
title="**My Answer:**",
186185
description=f"{answers[random.randint(0, len(answers))]}",
187-
color=config["success"]
186+
color=0x42F56C
188187
)
189188
embed.set_footer(
190189
text=f"The question was: {question}"
@@ -205,7 +204,7 @@ async def bitcoin(self, context):
205204
embed = discord.Embed(
206205
title=":information_source: Info",
207206
description=f"Bitcoin price is: ${response['bpi']['USD']['rate']}",
208-
color=config["success"]
207+
color=0x42F56C
209208
)
210209
await context.send(embed=embed)
211210

cogs/help.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
Version: 2.7
77
"""
88

9+
import json
910
import os
1011
import sys
1112

1213
import discord
13-
import yaml
1414
from discord.ext import commands
1515

16-
if not os.path.isfile("config.yaml"):
17-
sys.exit("'config.yaml' not found! Please add it and try again.")
16+
if not os.path.isfile("config.json"):
17+
sys.exit("'config.json' not found! Please add it and try again.")
1818
else:
19-
with open("config.yaml") as file:
20-
config = yaml.load(file, Loader=yaml.FullLoader)
19+
with open("config.json") as file:
20+
config = json.load(file)
2121

2222

2323
class Help(commands.Cog, name="help"):
@@ -32,7 +32,7 @@ async def help(self, context):
3232
prefix = config["bot_prefix"]
3333
if not isinstance(prefix, str):
3434
prefix = prefix[0]
35-
embed = discord.Embed(title="Help", description="List of available commands:", color=config["success"])
35+
embed = discord.Embed(title="Help", description="List of available commands:", color=0x42F56C)
3636
for i in self.bot.cogs:
3737
cog = self.bot.get_cog(i.lower())
3838
commands = cog.get_commands()

0 commit comments

Comments
 (0)