Skip to content

Commit 7e739f0

Browse files
committed
fix: Bug fixes & function return type hints
1 parent 14c5613 commit 7e739f0

File tree

8 files changed

+76
-59
lines changed

8 files changed

+76
-59
lines changed

UPDATES.md

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

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

5-
### Version 6.1.0 (20 September 2022)
5+
### Version 6.1.0 (20 September 2023)
6+
7+
- Various bug fixes
8+
- Added `-> None` type hint to remaining functions
9+
10+
### Version 6.0.1 & 6.0.2 (20 September 2023)
611

712
- Added two context menu commands, one for users and one for messages
813

bot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def __init__(self) -> None:
141141
self.config = config
142142
self.database = None
143143

144-
async def init_db(self):
144+
async def init_db(self) -> None:
145145
async with aiosqlite.connect(
146146
f"{os.path.realpath(os.path.dirname(__file__))}/database/database.db"
147147
) as db:

cogs/fun.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,27 @@
1515

1616

1717
class Choice(discord.ui.View):
18-
def __init__(self):
18+
def __init__(self) -> None:
1919
super().__init__()
2020
self.value = None
2121

2222
@discord.ui.button(label="Heads", style=discord.ButtonStyle.blurple)
2323
async def confirm(
2424
self, button: discord.ui.Button, interaction: discord.Interaction
25-
):
25+
) -> None:
2626
self.value = "heads"
2727
self.stop()
2828

2929
@discord.ui.button(label="Tails", style=discord.ButtonStyle.blurple)
30-
async def cancel(self, button: discord.ui.Button, interaction: discord.Interaction):
30+
async def cancel(
31+
self, button: discord.ui.Button, interaction: discord.Interaction
32+
) -> None:
3133
self.value = "tails"
3234
self.stop()
3335

3436

3537
class RockPaperScissors(discord.ui.Select):
36-
def __init__(self):
38+
def __init__(self) -> None:
3739
options = [
3840
discord.SelectOption(
3941
label="Scissors", description="You choose scissors.", emoji="✂"
@@ -52,7 +54,7 @@ def __init__(self):
5254
options=options,
5355
)
5456

55-
async def callback(self, interaction: discord.Interaction):
57+
async def callback(self, interaction: discord.Interaction) -> None:
5658
choices = {
5759
"rock": 0,
5860
"paper": 1,
@@ -86,13 +88,13 @@ async def callback(self, interaction: discord.Interaction):
8688

8789

8890
class RockPaperScissorsView(discord.ui.View):
89-
def __init__(self):
91+
def __init__(self) -> None:
9092
super().__init__()
9193
self.add_item(RockPaperScissors())
9294

9395

9496
class Fun(commands.Cog, name="fun"):
95-
def __init__(self, bot):
97+
def __init__(self, bot) -> None:
9698
self.bot = bot
9799

98100
@commands.hybrid_command(name="randomfact", description="Get a random fact.")
@@ -157,5 +159,5 @@ async def rock_paper_scissors(self, context: Context) -> None:
157159
await context.send("Please make your choice", view=view)
158160

159161

160-
async def setup(bot):
162+
async def setup(bot) -> None:
161163
await bot.add_cog(Fun(bot))

cogs/general.py

+50-40
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,56 @@
1717

1818

1919
class General(commands.Cog, name="general"):
20-
def __init__(self, bot):
20+
def __init__(self, bot) -> None:
2121
self.bot = bot
22+
self.context_menu_user = app_commands.ContextMenu(
23+
name="Grab ID", callback=self.grab_id
24+
)
25+
self.bot.tree.add_command(self.context_menu_user)
26+
self.context_menu_message = app_commands.ContextMenu(
27+
name="Remove spoilers", callback=self.remove_spoilers
28+
)
29+
self.bot.tree.add_command(self.context_menu_message)
30+
31+
# Message context menu command
32+
async def remove_spoilers(
33+
self, interaction: discord.Interaction, message: discord.Message
34+
) -> None:
35+
"""
36+
Removes the spoilers from the message. This command requires the MESSAGE_CONTENT intent to work properly.
37+
38+
:param interaction: The application command interaction.
39+
:param message: The message that is being interacted with.
40+
"""
41+
spoiler_attachment = None
42+
for attachment in message.attachments:
43+
if attachment.is_spoiler():
44+
spoiler_attachment = attachment
45+
break
46+
embed = discord.Embed(
47+
title="Message without spoilers",
48+
description=message.content.replace("||", ""),
49+
color=0xBEBEFE,
50+
)
51+
if spoiler_attachment is not None:
52+
embed.set_image(url=attachment.url)
53+
await interaction.response.send_message(embed=embed, ephemeral=True)
54+
55+
# User context menu command
56+
async def grab_id(
57+
self, interaction: discord.Interaction, user: discord.User
58+
) -> None:
59+
"""
60+
Grabs the ID of the user.
61+
62+
:param interaction: The application command interaction.
63+
:param user: The user that is being interacted with.
64+
"""
65+
embed = discord.Embed(
66+
description=f"The ID of {user.mention} is `{user.id}`.",
67+
color=0xBEBEFE,
68+
)
69+
await interaction.response.send_message(embed=embed, ephemeral=True)
2270

2371
@commands.hybrid_command(
2472
name="help", description="List all commands the bot has loaded."
@@ -231,44 +279,6 @@ async def bitcoin(self, context: Context) -> None:
231279
)
232280
await context.send(embed=embed)
233281

234-
@app_commands.context_menu(name="Remove spoilers")
235-
async def remove_spoilers(
236-
self, interaction: discord.Interaction, message: discord.Message
237-
):
238-
"""
239-
Removes the spoilers from the message. This command requires the MESSAGE_CONTENT intent to work properly.
240-
241-
:param interaction: The application command interaction.
242-
:param message: The message that is being interacted with.
243-
"""
244-
spoiler_attachment = None
245-
for attachment in message.attachments:
246-
if attachment.is_spoiler():
247-
spoiler_attachment = attachment
248-
break
249-
embed = discord.Embed(
250-
title="Message without spoilers",
251-
description=message.content.replace("||", ""),
252-
color=0xBEBEFE,
253-
)
254-
if spoiler_attachment is not None:
255-
embed.set_image(url=attachment.url)
256-
await interaction.response.send_message(embed=embed, ephemeral=True)
257-
258-
@app_commands.context_menu(name="Grab ID")
259-
async def grab_id(self, interaction: discord.Interaction, user: discord.User):
260-
"""
261-
Grabs the ID of the user.
262-
263-
:param interaction: The application command interaction.
264-
:param user: The user that is being interacted with.
265-
"""
266-
embed = discord.Embed(
267-
description=f"The ID of the user is `{user.id}`.",
268-
color=0xBEBEFE,
269-
)
270-
await interaction.response.send_message(embed=embed, ephemeral=True)
271-
272282

273-
async def setup(bot):
283+
async def setup(bot) -> None:
274284
await bot.add_cog(General(bot))

cogs/moderation.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
class Moderation(commands.Cog, name="moderation"):
19-
def __init__(self, bot):
19+
def __init__(self, bot) -> None:
2020
self.bot = bot
2121

2222
@commands.hybrid_command(
@@ -253,7 +253,7 @@ async def warning_remove(
253253
)
254254
@commands.has_guild_permissions(manage_messages=True)
255255
@app_commands.describe(user="The user you want to get the warnings of.")
256-
async def warning_list(self, context: Context, user: discord.User):
256+
async def warning_list(self, context: Context, user: discord.User) -> None:
257257
"""
258258
Shows the warnings of a user in the server.
259259
@@ -371,5 +371,5 @@ async def archive(self, context: Context, limit: int = 10) -> None:
371371
os.remove(log_file)
372372

373373

374-
async def setup(bot):
374+
async def setup(bot) -> None:
375375
await bot.add_cog(Moderation(bot))

cogs/owner.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
class Owner(commands.Cog, name="owner"):
16-
def __init__(self, bot):
16+
def __init__(self, bot) -> None:
1717
self.bot = bot
1818

1919
@commands.command(
@@ -327,5 +327,5 @@ async def blacklist_remove(self, context: Context, user: discord.User) -> None:
327327
await context.send(embed=embed)
328328

329329

330-
async def setup(bot):
330+
async def setup(bot) -> None:
331331
await bot.add_cog(Owner(bot))

cogs/template.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
# Here we name the cog and create a new class for the cog.
1414
class Template(commands.Cog, name="template"):
15-
def __init__(self, bot):
15+
def __init__(self, bot) -> None:
1616
self.bot = bot
1717

1818
# Here you can just add your own commands, you'll always need to provide "self" as first parameter.
@@ -21,7 +21,7 @@ def __init__(self, bot):
2121
name="testcommand",
2222
description="This is a testing command that does nothing.",
2323
)
24-
async def testcommand(self, context: Context):
24+
async def testcommand(self, context: Context) -> None:
2525
"""
2626
This is a testing command that does nothing.
2727
@@ -34,5 +34,5 @@ async def testcommand(self, context: Context):
3434

3535

3636
# And then we finally add the cog to the bot so that it can load, unload, reload and use it's content.
37-
async def setup(bot):
37+
async def setup(bot) -> None:
3838
await bot.add_cog(Template(bot))

database/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
class DatabaseManager:
14-
def __init__(self, *, connection: aiosqlite.Connection):
14+
def __init__(self, *, connection: aiosqlite.Connection) -> None:
1515
self.connection = connection
1616

1717
async def add_warn(

0 commit comments

Comments
 (0)