Skip to content

Commit d7c3543

Browse files
martinbndrlorenzo132
authored andcommitted
Fixes thread_auto_close execution when disabled. (modmail-dev#3423)
* Fixes thread_auto_close execution when disabled. This fixes the issue modmail-dev#3290 which caused threads to be auto-closed even if `thread_auto_close` has been disabled. There was also an issue that closed the thread when the user has responded to mods. The thread should stay open and only auto close when the staff has replied back. * fix: prevent autoclosing when close has been cancelled. This solves the thread from autoclosing if the closure has been cancelled earlier in a thread. * fix: AttributeError / lower mongo calls. I had added a small bugfix aswell for pagination when an invalid config var was given. This happened to occur upon removing the `thread_auto_close` config. --------- Co-authored-by: lorenzo132 <lhoorn4@gmail.com> Co-authored-by: lorenzo132 <50767078+lorenzo132@users.noreply.github.com>
1 parent 555fb86 commit d7c3543

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

cogs/utility.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -875,14 +875,33 @@ async def config_remove(self, ctx, *, key: str.lower):
875875
color=self.bot.main_color,
876876
description=f"`{key}` had been reset to default.",
877877
)
878+
879+
# Cancel exsisting active closures from thread_auto_close due to being disabled.
880+
if key == "thread_auto_close":
881+
closures = self.bot.config["closures"]
882+
for recipient_id, items in tuple(closures.items()):
883+
if items.get("auto_close", False) is True:
884+
self.bot.config["closures"].pop(recipient_id)
885+
thread = await self.bot.threads.find(recipient_id=int(recipient_id))
886+
if thread:
887+
await thread.cancel_closure(all=True)
888+
else:
889+
self.bot.config["closures"].pop(recipient_id)
890+
# Only update config once after processing all closures
891+
await self.bot.config.update()
878892
else:
879-
embed = discord.Embed(
880-
title="Error",
881-
color=self.bot.error_color,
882-
description=f"{key} is an invalid key.",
883-
)
884-
valid_keys = [f"`{k}`" for k in sorted(keys)]
885-
embed.add_field(name="Valid keys", value=", ".join(valid_keys))
893+
embeds = []
894+
for names in zip_longest(*(iter(sorted(keys)),) * 15):
895+
description = "\n".join(f"`{name}`" for name in takewhile(lambda x: x is not None, names))
896+
embed = discord.Embed(
897+
title="Error - Invalid Key",
898+
color=self.bot.error_color,
899+
description=f"`{key}` is an invalid key.\n\n**Valid configuration keys:**\n{description}",
900+
)
901+
embeds.append(embed)
902+
903+
session = EmbedPaginatorSession(ctx, *embeds)
904+
return await session.run()
886905

887906
return await ctx.send(embed=embed)
888907

core/thread.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def __init__(
7474
self.wait_tasks = []
7575
self.close_task = None
7676
self.auto_close_task = None
77+
self.auto_close_cancelled = False # Track if auto-close was explicitly cancelled
7778
self._cancelled = False
7879
self._dm_menu_msg_id = None
7980
self._dm_menu_channel_id = None
@@ -1100,6 +1101,7 @@ async def close(
11001101
self.auto_close_task = task
11011102
else:
11021103
self.close_task = task
1104+
self.auto_close_cancelled = False # Reset flag when manually closing
11031105
else:
11041106
await self._close(closer, silent, delete_channel, message)
11051107

@@ -1298,6 +1300,7 @@ async def cancel_closure(self, auto_close: bool = False, all: bool = False) -> N
12981300
if self.auto_close_task is not None and (auto_close or all):
12991301
self.auto_close_task.cancel()
13001302
self.auto_close_task = None
1303+
self.auto_close_cancelled = True # Mark auto-close as explicitly cancelled
13011304

13021305
to_update = self.bot.config["closures"].pop(str(self.id), None)
13031306
if to_update is not None:
@@ -1832,7 +1835,11 @@ async def send(
18321835
return await destination.send(embed=embed)
18331836

18341837
if not note and from_mod:
1835-
self.bot.loop.create_task(self._restart_close_timer()) # Start or restart thread auto close
1838+
# Only restart auto-close if it wasn't explicitly cancelled
1839+
if not self.auto_close_cancelled:
1840+
self.bot.loop.create_task(self._restart_close_timer()) # Start or restart thread auto close
1841+
elif not note and not from_mod:
1842+
await self.cancel_closure(all=True)
18361843

18371844
if self.close_task is not None:
18381845
# cancel closing if a thread message is sent.

0 commit comments

Comments
 (0)