Skip to content

Commit

Permalink
fix: Bug fixes & function return type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
kkrypt0nn committed Sep 20, 2023
1 parent 14c5613 commit 7e739f0
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 59 deletions.
7 changes: 6 additions & 1 deletion UPDATES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

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

### Version 6.1.0 (20 September 2022)
### Version 6.1.0 (20 September 2023)

- Various bug fixes
- Added `-> None` type hint to remaining functions

### Version 6.0.1 & 6.0.2 (20 September 2023)

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

Expand Down
2 changes: 1 addition & 1 deletion bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def __init__(self) -> None:
self.config = config
self.database = None

async def init_db(self):
async def init_db(self) -> None:
async with aiosqlite.connect(
f"{os.path.realpath(os.path.dirname(__file__))}/database/database.db"
) as db:
Expand Down
18 changes: 10 additions & 8 deletions cogs/fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@


class Choice(discord.ui.View):
def __init__(self):
def __init__(self) -> None:
super().__init__()
self.value = None

@discord.ui.button(label="Heads", style=discord.ButtonStyle.blurple)
async def confirm(
self, button: discord.ui.Button, interaction: discord.Interaction
):
) -> None:
self.value = "heads"
self.stop()

@discord.ui.button(label="Tails", style=discord.ButtonStyle.blurple)
async def cancel(self, button: discord.ui.Button, interaction: discord.Interaction):
async def cancel(
self, button: discord.ui.Button, interaction: discord.Interaction
) -> None:
self.value = "tails"
self.stop()


class RockPaperScissors(discord.ui.Select):
def __init__(self):
def __init__(self) -> None:
options = [
discord.SelectOption(
label="Scissors", description="You choose scissors.", emoji="✂"
Expand All @@ -52,7 +54,7 @@ def __init__(self):
options=options,
)

async def callback(self, interaction: discord.Interaction):
async def callback(self, interaction: discord.Interaction) -> None:
choices = {
"rock": 0,
"paper": 1,
Expand Down Expand Up @@ -86,13 +88,13 @@ async def callback(self, interaction: discord.Interaction):


class RockPaperScissorsView(discord.ui.View):
def __init__(self):
def __init__(self) -> None:
super().__init__()
self.add_item(RockPaperScissors())


class Fun(commands.Cog, name="fun"):
def __init__(self, bot):
def __init__(self, bot) -> None:
self.bot = bot

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


async def setup(bot):
async def setup(bot) -> None:
await bot.add_cog(Fun(bot))
90 changes: 50 additions & 40 deletions cogs/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,56 @@


class General(commands.Cog, name="general"):
def __init__(self, bot):
def __init__(self, bot) -> None:
self.bot = bot
self.context_menu_user = app_commands.ContextMenu(
name="Grab ID", callback=self.grab_id
)
self.bot.tree.add_command(self.context_menu_user)
self.context_menu_message = app_commands.ContextMenu(
name="Remove spoilers", callback=self.remove_spoilers
)
self.bot.tree.add_command(self.context_menu_message)

# Message context menu command
async def remove_spoilers(
self, interaction: discord.Interaction, message: discord.Message
) -> None:
"""
Removes the spoilers from the message. This command requires the MESSAGE_CONTENT intent to work properly.
:param interaction: The application command interaction.
:param message: The message that is being interacted with.
"""
spoiler_attachment = None
for attachment in message.attachments:
if attachment.is_spoiler():
spoiler_attachment = attachment
break
embed = discord.Embed(
title="Message without spoilers",
description=message.content.replace("||", ""),
color=0xBEBEFE,
)
if spoiler_attachment is not None:
embed.set_image(url=attachment.url)
await interaction.response.send_message(embed=embed, ephemeral=True)

# User context menu command
async def grab_id(
self, interaction: discord.Interaction, user: discord.User
) -> None:
"""
Grabs the ID of the user.
:param interaction: The application command interaction.
:param user: The user that is being interacted with.
"""
embed = discord.Embed(
description=f"The ID of {user.mention} is `{user.id}`.",
color=0xBEBEFE,
)
await interaction.response.send_message(embed=embed, ephemeral=True)

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

@app_commands.context_menu(name="Remove spoilers")
async def remove_spoilers(
self, interaction: discord.Interaction, message: discord.Message
):
"""
Removes the spoilers from the message. This command requires the MESSAGE_CONTENT intent to work properly.
:param interaction: The application command interaction.
:param message: The message that is being interacted with.
"""
spoiler_attachment = None
for attachment in message.attachments:
if attachment.is_spoiler():
spoiler_attachment = attachment
break
embed = discord.Embed(
title="Message without spoilers",
description=message.content.replace("||", ""),
color=0xBEBEFE,
)
if spoiler_attachment is not None:
embed.set_image(url=attachment.url)
await interaction.response.send_message(embed=embed, ephemeral=True)

@app_commands.context_menu(name="Grab ID")
async def grab_id(self, interaction: discord.Interaction, user: discord.User):
"""
Grabs the ID of the user.
:param interaction: The application command interaction.
:param user: The user that is being interacted with.
"""
embed = discord.Embed(
description=f"The ID of the user is `{user.id}`.",
color=0xBEBEFE,
)
await interaction.response.send_message(embed=embed, ephemeral=True)


async def setup(bot):
async def setup(bot) -> None:
await bot.add_cog(General(bot))
6 changes: 3 additions & 3 deletions cogs/moderation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


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

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


async def setup(bot):
async def setup(bot) -> None:
await bot.add_cog(Moderation(bot))
4 changes: 2 additions & 2 deletions cogs/owner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


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

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


async def setup(bot):
async def setup(bot) -> None:
await bot.add_cog(Owner(bot))
6 changes: 3 additions & 3 deletions cogs/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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

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


# And then we finally add the cog to the bot so that it can load, unload, reload and use it's content.
async def setup(bot):
async def setup(bot) -> None:
await bot.add_cog(Template(bot))
2 changes: 1 addition & 1 deletion database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class DatabaseManager:
def __init__(self, *, connection: aiosqlite.Connection):
def __init__(self, *, connection: aiosqlite.Connection) -> None:
self.connection = connection

async def add_warn(
Expand Down

0 comments on commit 7e739f0

Please sign in to comment.