From 547c804b72fdb50ae9080b078660130f5fac712b Mon Sep 17 00:00:00 2001 From: Isaac Beh Date: Sat, 9 Mar 2024 12:00:13 +1000 Subject: [PATCH] Fixed various typing issues (#195) * Fixed various typing issues * Fixed stub issue for error_handler * Added typing for member counter --- pyproject.toml | 4 ---- uqcsbot/error_handler.py | 7 ++++--- uqcsbot/member_counter.py | 26 ++++++++++++++++++++++---- uqcsbot/uptime.py | 2 +- uqcsbot/working_on.py | 4 ++-- 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index b6c2175a..91963233 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,15 +43,11 @@ build-backend = "poetry.core.masonry.api" strict = ["**"] exclude = [ "**/bot.py", - "**/error_handler.py", "**/events.py", "**/gaming.py", - "**/member_counter.py", "**/remindme.py", "**/snailrace.py", "**/starboard.py", - "**/uptime.py", - "**/working_on.py", "**/utils/snailrace_utils.py", ] diff --git a/uqcsbot/error_handler.py b/uqcsbot/error_handler.py index 98a55bc2..de605876 100644 --- a/uqcsbot/error_handler.py +++ b/uqcsbot/error_handler.py @@ -1,6 +1,7 @@ from discord.ext import commands -from discord.ext.commands.errors import MissingRequiredArgument import logging +from typing import Any +from uqcsbot.bot import UQCSBot """ TODO: this is bundled with advent.py and should be removed. @@ -9,8 +10,8 @@ class ErrorHandler(commands.Cog): @commands.Cog.listener() - async def on_command_error(self, ctx: commands.Context, err): - if isinstance(err, MissingRequiredArgument): + async def on_command_error(self, ctx: commands.Context[UQCSBot], err: Any): + if isinstance(err, commands.errors.MissingRequiredArgument): await ctx.send( "Missing required argument. For further information refer to `!help`" ) diff --git a/uqcsbot/member_counter.py b/uqcsbot/member_counter.py index a6cda54d..2e6939f2 100644 --- a/uqcsbot/member_counter.py +++ b/uqcsbot/member_counter.py @@ -7,13 +7,15 @@ from discord import app_commands from discord.ext import commands +from uqcsbot.bot import UQCSBot + class MemberCounter(commands.Cog): MEMBER_COUNT_PREFIX = "Member Count: " RATE_LIMIT = timedelta(minutes=5) NEW_MEMBER_TIME = timedelta(days=7) - def __init__(self, bot: commands.Bot): + def __init__(self, bot: UQCSBot): self.bot = bot self.last_rename_time = datetime.now() self.waiting_for_rename = False @@ -43,7 +45,13 @@ async def on_ready(self): ) return + if self.bot.user is None: + logging.warning("Could not find the bot's own id.") + return bot_member = self.bot.uqcs_server.get_member(self.bot.user.id) + if bot_member is None: + logging.warning("Could not find the bot's user.") + return permissions = self.member_count_channel.permissions_for(bot_member) if not permissions.manage_channels: logging.warning( @@ -55,17 +63,27 @@ async def on_ready(self): @app_commands.command(name="membercount") async def member_count(self, interaction: discord.Interaction, force: bool = False): """Display the number of members""" + if interaction.guild is None: + logging.warning( + "Could not update member count as could not access members." + ) + return new_members = [ member for member in interaction.guild.members - if member.joined_at + if member.joined_at is not None + and member.joined_at > datetime.now(tz=ZoneInfo("Australia/Brisbane")) - self.NEW_MEMBER_TIME ] await interaction.response.send_message( f"There are currently {interaction.guild.member_count} members in the UQ Computing Society discord server, with {len(new_members)} joining in the last 7 days." ) - if interaction.user.guild_permissions.manage_guild and force: + if ( + isinstance(interaction.user, discord.Member) + and interaction.user.guild_permissions.manage_guild + and force + ): # this is dodgy, but the alternative is to restart the bot # if it gets caught in a loop of waiting for a broken rename self.waiting_for_rename = False @@ -107,5 +125,5 @@ async def _update_member_count_channel_name(self): self.waiting_for_rename = False -async def setup(bot: commands.Bot): +async def setup(bot: UQCSBot): await bot.add_cog(MemberCounter(bot)) diff --git a/uqcsbot/uptime.py b/uqcsbot/uptime.py index ad38d7c5..f1caaa2a 100644 --- a/uqcsbot/uptime.py +++ b/uqcsbot/uptime.py @@ -22,7 +22,7 @@ async def on_ready(self): self.bot.uqcs_server.channels, name=self.CHANNEL_NAME ) - if channel is not None: + if isinstance(channel, discord.TextChannel): if random.randint(1, 100) == 1: await channel.send("Oopsie, I webooted uwu >_<") else: diff --git a/uqcsbot/working_on.py b/uqcsbot/working_on.py index e3af632c..8ebfea12 100644 --- a/uqcsbot/working_on.py +++ b/uqcsbot/working_on.py @@ -19,7 +19,7 @@ def __init__(self, bot: UQCSBot): async def workingon(self): """5pm ping for 2 lucky server members to share what they have been working on.""" members = list(self.bot.get_all_members()) - message = [] + message: list[str] = [] while len(message) < 2: chosen = choice(members) @@ -33,7 +33,7 @@ async def workingon(self): self.bot.uqcs_server.channels, name=GENERAL_CHANNEL ) - if general_channel is not None: + if isinstance(general_channel, discord.TextChannel): await general_channel.send( "\n".join(message), allowed_mentions=discord.AllowedMentions(