Skip to content

Commit

Permalink
Fixed various typing issues (#195)
Browse files Browse the repository at this point in the history
* Fixed various typing issues

* Fixed stub issue for error_handler

* Added typing for member counter
  • Loading branch information
49Indium authored Mar 9, 2024
1 parent 0c38624 commit 547c804
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
4 changes: 0 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]

7 changes: 4 additions & 3 deletions uqcsbot/error_handler.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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`"
)
Expand Down
26 changes: 22 additions & 4 deletions uqcsbot/member_counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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))
2 changes: 1 addition & 1 deletion uqcsbot/uptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions uqcsbot/working_on.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand Down

0 comments on commit 547c804

Please sign in to comment.