GloryBot/cogs/cog_member_remove.py

53 lines
1.7 KiB
Python
Raw Normal View History

2024-06-02 22:51:07 +03:00
import logging
from discord import Cog, Member
from classes.enums import MemberStatus
from classes.errors import GuildNotFoundError, MemberNotFoundError
from classes.pycord_bot import PycordBot
from classes.pycord_guild import PycordGuild
from classes.pycord_member import PycordMember
logger = logging.getLogger(__name__)
class CogMemberRemove(Cog):
def __init__(self, client: PycordBot) -> None:
super().__init__()
self.client = client
@Cog.listener()
async def on_member_remove(self, member: Member):
# Check whether the guild exists in the database
# We should not create it here when it does not exist. Config will be empty
try:
pycord_guild: PycordGuild = await self.client.find_guild(member.guild.id)
except GuildNotFoundError:
# Let's notify guild admins about this
logger.error(
"Guild %s is not set up properly: guild is missing from the database.",
member.guild.id,
)
return
# Get the member
try:
pycord_member: PycordMember = await self.client.find_member(
member.id, pycord_guild._id
)
except MemberNotFoundError:
return
# Verify whether a member has an active check
if check := await pycord_member.get_check():
# Update member's status
await pycord_member.set_status(MemberStatus.FAILED)
# Delete the thread (if possible)
if thread := member.guild.get_thread(check.thread_id):
await thread.delete()
def setup(client: PycordBot):
client.add_cog(CogMemberRemove(client))