Skip to content

Commit 33f3112

Browse files
committed
Some code cleanup
1 parent 20b2d3d commit 33f3112

File tree

13 files changed

+31
-116
lines changed

13 files changed

+31
-116
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@ python3 bot.py
113113
or eventually
114114

115115
```
116-
python3.8 bot.py
116+
python3.x bot.py
117117
```
118+
Replace `x` with the version of Python you have installed.
118119

119120
<br>
120121

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

141142
## Built With
142143

143-
* [Python 3.9.9](https://www.python.org/)
144+
* [Python 3.9.12](https://www.python.org/)
144145

145146
## License
146147

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Version 4.2
44

5-
* [ ] Add a timeout command.
5+
* [ ] Add a timeout command
66

77
## Version 4.3
88

bot.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,42 @@
3535
3636
Default Intents:
3737
intents.bans = True
38-
intents.dm_messages = False
39-
intents.dm_reactions = False
40-
intents.dm_typing = False
38+
intents.dm_messages = True
39+
intents.dm_reactions = True
40+
intents.dm_typing = True
4141
intents.emojis = True
42+
intents.emojis_and_stickers = True
4243
intents.guild_messages = True
4344
intents.guild_reactions = True
44-
intents.guild_typing = False
45+
intents.guild_scheduled_events = True
46+
intents.guild_typing = True
4547
intents.guilds = True
4648
intents.integrations = True
4749
intents.invites = True
50+
intents.messages = True # `message_content` is required to get the content of the messages
4851
intents.reactions = True
49-
intents.typing = False
50-
intents.voice_states = False
51-
intents.webhooks = False
52+
intents.typing = True
53+
intents.voice_states = True
54+
intents.webhooks = True
5255
53-
Privileged Intents (Needs to be enabled on dev page), please use them only if you need them:
56+
Privileged Intents (Needs to be enabled on developer portal of Discord), please use them only if you need them:
5457
intents.members = True
55-
intents.messages = True
58+
intents.message_content = True
5659
intents.presences = True
5760
"""
5861

5962
intents = disnake.Intents.default()
6063

61-
bot = Bot(command_prefix=config["prefix"], intents=intents)
64+
bot = Bot(command_prefix=config["prefix"], intents=intents, help_command=None)
65+
66+
"""
67+
Create a bot variable to access the config file in cogs so that you don't need to import it every time.
68+
69+
The config is available using the following code:
70+
- bot.config # In this file
71+
- self.bot.config # In cogs
72+
"""
73+
bot.config = config
6274

6375

6476
@bot.event
@@ -83,10 +95,6 @@ async def status_task() -> None:
8395
await bot.change_presence(activity=disnake.Game(random.choice(statuses)))
8496

8597

86-
# Removes the default help command of discord.py to be able to create our custom help command.
87-
bot.remove_command("help")
88-
89-
9098
def load_commands(command_type: str) -> None:
9199
for file in os.listdir(f"./cogs/{command_type}"):
92100
if file.endswith(".py"):
@@ -204,8 +212,8 @@ async def on_command_error(context: Context, error) -> None:
204212
elif isinstance(error, commands.MissingRequiredArgument):
205213
embed = disnake.Embed(
206214
title="Error!",
207-
description=str(error).capitalize(),
208215
# We need to capitalize because the command arguments have no capital letter in the code.
216+
description=str(error).capitalize(),
209217
color=0xE02B2B
210218
)
211219
await context.send(embed=embed)

cogs/normal/fun-normal.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
Version: 4.1
77
"""
88

9-
import json
10-
import os
119
import random
12-
import sys
1310

1411
import aiohttp
1512
import disnake
@@ -18,12 +15,6 @@
1815

1916
from helpers import checks
2017

21-
if not os.path.isfile("config.json"):
22-
sys.exit("'config.json' not found! Please add it and try again.")
23-
else:
24-
with open("config.json") as file:
25-
config = json.load(file)
26-
2718

2819
class Choice(disnake.ui.View):
2920
def __init__(self):

cogs/normal/general-normal.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
Version: 4.1
77
"""
88

9-
import json
10-
import os
119
import platform
1210
import random
13-
import sys
1411

1512
import aiohttp
1613
import disnake
@@ -19,12 +16,6 @@
1916

2017
from helpers import checks
2118

22-
if not os.path.isfile("config.json"):
23-
sys.exit("'config.json' not found! Please add it and try again.")
24-
else:
25-
with open("config.json") as file:
26-
config = json.load(file)
27-
2819

2920
class General(commands.Cog, name="general-normal"):
3021
def __init__(self, bot):
@@ -59,7 +50,7 @@ async def botinfo(self, context: Context) -> None:
5950
)
6051
embed.add_field(
6152
name="Prefix:",
62-
value=f"/ (Slash Commands) or {config['prefix']} for normal commands",
53+
value=f"/ (Slash Commands) or {self.bot.config['prefix']} for normal commands",
6354
inline=False
6455
)
6556
embed.set_footer(
@@ -140,7 +131,7 @@ async def invite(self, context: Context) -> None:
140131
:param context: The context in which the command has been executed.
141132
"""
142133
embed = disnake.Embed(
143-
description=f"Invite me by clicking [here](https://discordapp.com/oauth2/authorize?&client_id={config['application_id']}&scope=bot+applications.commands&permissions={config['permissions']}).",
134+
description=f"Invite me by clicking [here](https://discordapp.com/oauth2/authorize?&client_id={self.bot.config['application_id']}&scope=bot+applications.commands&permissions={self.bot.config['permissions']}).",
144135
color=0xD75BF4
145136
)
146137
try:

cogs/normal/moderation-normal.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,12 @@
66
Version: 4.1
77
"""
88

9-
import json
10-
import os
11-
import sys
12-
139
import disnake
1410
from disnake.ext import commands
1511
from disnake.ext.commands import Context
1612

1713
from helpers import checks
1814

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

2616
class Moderation(commands.Cog, name="moderation-normal"):
2717
def __init__(self, bot):

cogs/normal/owner-normal.py

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

99
import json
10-
import os
11-
import sys
1210

1311
import disnake
1412
from disnake.ext import commands
1513
from disnake.ext.commands import Context
1614

1715
from helpers import json_manager, checks
1816

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

2618
class Owner(commands.Cog, name="owner-normal"):
2719
def __init__(self, bot):

cogs/normal/template-normal.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,11 @@
66
Version: 4.1
77
"""
88

9-
import json
10-
import os
11-
import sys
12-
139
from disnake.ext import commands
1410
from disnake.ext.commands import Context
1511

1612
from helpers import checks
1713

18-
# Only if you want to use variables that are in the config.json file.
19-
if not os.path.isfile("config.json"):
20-
sys.exit("'config.json' not found! Please add it and try again.")
21-
else:
22-
with open("config.json") as file:
23-
config = json.load(file)
24-
2514

2615
# Here we name the cog and create a new class for the cog.
2716
class Template(commands.Cog, name="template-normal"):

cogs/slash/fun-slash.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
Version: 4.1
77
"""
88

9-
import json
10-
import os
119
import random
12-
import sys
1310

1411
import aiohttp
1512
import disnake
@@ -18,12 +15,6 @@
1815

1916
from helpers import checks
2017

21-
if not os.path.isfile("config.json"):
22-
sys.exit("'config.json' not found! Please add it and try again.")
23-
else:
24-
with open("config.json") as file:
25-
config = json.load(file)
26-
2718

2819
class Choice(disnake.ui.View):
2920
def __init__(self):

cogs/slash/general-slash.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
Version: 4.1
77
"""
88

9-
import json
10-
import os
119
import platform
1210
import random
13-
import sys
1411

1512
import aiohttp
1613
import disnake
@@ -19,12 +16,6 @@
1916

2017
from helpers import checks
2118

22-
if not os.path.isfile("config.json"):
23-
sys.exit("'config.json' not found! Please add it and try again.")
24-
else:
25-
with open("config.json") as file:
26-
config = json.load(file)
27-
2819

2920
class General(commands.Cog, name="general-slash"):
3021
def __init__(self, bot):
@@ -59,7 +50,7 @@ async def botinfo(self, interaction: ApplicationCommandInteraction) -> None:
5950
)
6051
embed.add_field(
6152
name="Prefix:",
62-
value=f"/ (Slash Commands) or {config['prefix']} for normal commands",
53+
value=f"/ (Slash Commands) or {self.bot.config['prefix']} for normal commands",
6354
inline=False
6455
)
6556
embed.set_footer(
@@ -140,7 +131,7 @@ async def invite(self, interaction: ApplicationCommandInteraction) -> None:
140131
:param interaction: The application command interaction.
141132
"""
142133
embed = disnake.Embed(
143-
description=f"Invite me by clicking [here](https://discordapp.com/oauth2/authorize?&client_id={config['application_id']}&scope=bot+applications.commands&permissions={config['permissions']}).",
134+
description=f"Invite me by clicking [here](https://discordapp.com/oauth2/authorize?&client_id={self.bot.config['application_id']}&scope=bot+applications.commands&permissions={self.bot.config['permissions']}).",
144135
color=0xD75BF4
145136
)
146137
try:

0 commit comments

Comments
 (0)