Skip to content

Commit

Permalink
Bug fixes and deferring contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
greystorm101 committed Jan 26, 2025
1 parent 21670de commit 9ad3695
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
26 changes: 16 additions & 10 deletions src/cogs/eraser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def __init__(self, bot: Bot) -> None:
async def former_spriter(self, ctx: Context, name: User = None):
former_spriter = ctx.guild.get_member(name.id)

await ctx.defer()

global ROLES_TO_RM
if ROLES_TO_RM == []:
for id in ROLES_TO_RM_IDS:
Expand All @@ -52,6 +54,8 @@ async def former_spriter(self, ctx: Context, name: User = None):
async def undo_former_spriter(self, ctx: Context, name: User = None):
former_spriter = ctx.guild.get_member(name.id)

await ctx.defer()

with open(os.path.join(FILEPACK_DIR, "former-spriters.txt"), "r+") as fd:
new_names_list = [line for line in fd.readlines() if line.rstrip() != str(former_spriter.id)]
new_names = "".join(new_names_list)
Expand All @@ -65,7 +69,7 @@ async def undo_former_spriter(self, ctx: Context, name: User = None):
await former_spriter.remove_roles(utils.get(ctx.guild.roles,id=int(FORMER_SPRITER_ROLE_ID)))

await ctx.message.delete(delay=2)
await ctx.send(f"Undid {former_spriter.name}'s former spriter status")
await ctx.send(f"Undid {former_spriter.mention}'s former spriter status")
return

@has_any_role("Sprite Manager", "Bot Manager", "Creator")
Expand All @@ -74,6 +78,8 @@ async def undo_former_spriter(self, ctx: Context, name: User = None):
brief = "(Debugging) removed former spriter")
async def former_spriter_by_id(self, ctx: Context, id: str):

await ctx.defer()

former_spriters_fd = open(os.path.join(FILEPACK_DIR, "former-spriters.txt"), "a")
former_spriters_fd.write(f"{id}\n")
former_spriters_fd.close()
Expand All @@ -88,22 +94,22 @@ async def on_member_join(self, member: Member):
"""
Listen for if a former spriter re-joins
"""
await asyncio.sleep(2) # We run into an error sometimes if we read or send the message too fast after thread was created
# await asyncio.sleep(2) # We run into an error sometimes if we read or send the message too fast after thread was created

if is_former_spriter(member):
await member.add_roles(member.guild.get_role(FORMER_SPRITER_ROLE_ID))
zigzag_chat_channel = self.bot.get_channel(ZIGZAG_CHATTER_CHANNEL_ID)
message = f"{member.mention} has rejoined the server.\n\nThis user is flagged as a former spriter and may need to former spriter role re-added to them."
await zigzag_chat_channel.send(content=message)

@Cog.listener()
async def on_message(self, message: Message):
if is_former_spriter(message.author):
if message.channel.id in FORMER_SPRITER_RESTRICTED_CHANNEL_IDS:
await message.delete()
reply = f"Hey {message.author.mention}, looks like you are a withdrawn artist and therefore you cannot"\
"send messages in this restricted channel. If you believe this is an error, please contact a sprite manager"
await message.channel.send(reply, delete_after=30)
# @Cog.listener()
# async def on_message(self, message: Message):
# if is_former_spriter(message.author):
# if message.channel.id in FORMER_SPRITER_RESTRICTED_CHANNEL_IDS:
# await message.delete()
# reply = f"Hey {message.author.mention}, looks like you are a withdrawn artist and therefore you cannot"\
# "send messages in this restricted channel. If you believe this is an error, please contact a sprite manager"
# await message.channel.send(reply, delete_after=30)


# @Cog.listener()
Expand Down
4 changes: 4 additions & 0 deletions src/cogs/klefki.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ async def appdeny(self, ctx: Context, name: User, reason:str, comment: str = "")
async def applicant(self, ctx: Context, name: User = None):
selected_user = ctx.guild.get_member(name.id)

await ctx.defer(ephemeral=True)

if await former_spriter_safeguard(selected_user, ctx):
return

Expand All @@ -167,6 +169,8 @@ async def applicant(self, ctx: Context, name: User = None):
brief = "Removes the spriter applicant role (silent)")
async def remove(self, ctx: Context, name: User = None):
selected_user = ctx.guild.get_member(name.id)

await ctx.defer(ephemeral=True)

applicant_role = utils.get(ctx.guild.roles,id=SPRITER_APPLICANT_ID)

Expand Down
6 changes: 2 additions & 4 deletions src/cogs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,14 @@ def print_typo_list():
def is_former_spriter(user: Member, is_prod = True):
# Fill cache if needed
if len(FORMER_SPRITERS) == 0:
former_spriters_fd = open(os.path.join(FILEPACK_DIR, "former-spriters.txt"), "r")
for line in former_spriters_fd:
FORMER_SPRITERS.append(line.strip('\n'))
update_former_spriter_cache()

global FORMER_SPRITER_ROLE_ID
if FORMER_SPRITER_ROLE_ID is None:
FORMER_SPRITER_ROLE_ID = os.environ.get("FORMER_SPRITER_ROLE_ID") if is_prod else os.environ.get("FORMER_SPRITER_ROLE_ID")
FORMER_SPRITER_ROLE_ID = int(FORMER_SPRITER_ROLE_ID)

return (user.id in FORMER_SPRITERS) or (FORMER_SPRITER_ROLE_ID in [role.id for role in user.roles])
return (user.id in FORMER_SPRITERS) or (FORMER_SPRITER_ROLE_ID in [role.id for role in user.roles if hasattr(user, "roles")])

def update_former_spriter_cache():
# Fill it first in case we haven't hit it
Expand Down
18 changes: 10 additions & 8 deletions src/cogs/zigzag.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ async def oldest(self, ctx: Context, start_args:str = None):

mark_user_stats(ctx.author, ZigzagStats.DIG)

await ctx.defer()

# Parse args and determine start date
if start_args is not None:
if len(start_args.split('/')) == 3:
Expand Down Expand Up @@ -241,7 +243,8 @@ def __init__(self, thread: Thread, thread_owner: User, fusion:list, image:Attach
super().__init__(placeholder='Choose action...', min_values=1, max_values=1, options=options)

async def callback(self, interaction: discord.Interaction):


await interaction.response.defer()
choice = self.values[0]
if choice == 'Post':
gal_tag = spritepost_tags["gallery"]
Expand Down Expand Up @@ -292,7 +295,6 @@ async def callback(self, interaction: discord.Interaction):
message = "How did you do this???? Your choice was {}".format(choice)

await interaction.message.edit(content = message)
await interaction.response.defer()
return True


Expand Down Expand Up @@ -607,11 +609,11 @@ def is_user_immune(user: Member):

def is_user_post_immune(user: Member):
"""Determines if a user has yanmega/posting immunity"""
if is_former_spriter(user):
return True

if user is None or type(user) == User:
return False

if is_former_spriter(user):
return True

role_ids = [role.id for role in user.roles]
if (POST_IMMUNITY_ID in role_ids):
Expand All @@ -620,12 +622,12 @@ def is_user_post_immune(user: Member):

def is_user_harvest_immune(user: Member):
"""Determines if a user has yanmega/posting immunity"""
if is_former_spriter(user):
return True

if user is None or type(user) == User:
return False

if is_former_spriter(user):
return True

role_ids = [role.id for role in user.roles]
if (HARVEST_IMMUNITY_ID in role_ids):
return True
Expand Down

0 comments on commit 9ad3695

Please sign in to comment.