Skip to content

Commit

Permalink
Added typing for member counter
Browse files Browse the repository at this point in the history
  • Loading branch information
49Indium committed Mar 7, 2024
1 parent 49eb5eb commit 33367e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ exclude = [
"**/bot.py",
"**/events.py",
"**/gaming.py",
"**/member_counter.py",
"**/remindme.py",
"**/snailrace.py",
"**/starboard.py",
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))

0 comments on commit 33367e2

Please sign in to comment.