Skip to content

Commit ad801b1

Browse files
Fixes thread_auto_close execution when disabled. (#3423)
* Fixes thread_auto_close execution when disabled. This fixes the issue #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 <[email protected]> Co-authored-by: lorenzo132 <[email protected]>
1 parent ab458f5 commit ad801b1

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

cogs/utility.py

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

885904
return await ctx.send(embed=embed)
886905

core/thread.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def __init__(
6767
self.wait_tasks = []
6868
self.close_task = None
6969
self.auto_close_task = None
70+
self.auto_close_cancelled = False # Track if auto-close was explicitly cancelled
7071
self._cancelled = False
7172
self._dm_menu_msg_id = None
7273
self._dm_menu_channel_id = None
@@ -1078,6 +1079,7 @@ async def close(
10781079
self.auto_close_task = task
10791080
else:
10801081
self.close_task = task
1082+
self.auto_close_cancelled = False # Reset flag when manually closing
10811083
else:
10821084
await self._close(closer, silent, delete_channel, message)
10831085

@@ -1278,6 +1280,7 @@ async def cancel_closure(self, auto_close: bool = False, all: bool = False) -> N
12781280
if self.auto_close_task is not None and (auto_close or all):
12791281
self.auto_close_task.cancel()
12801282
self.auto_close_task = None
1283+
self.auto_close_cancelled = True # Mark auto-close as explicitly cancelled
12811284

12821285
to_update = self.bot.config["closures"].pop(str(self.id), None)
12831286
if to_update is not None:
@@ -1810,7 +1813,11 @@ async def send(
18101813
return await destination.send(embed=embed)
18111814

18121815
if not note and from_mod:
1813-
self.bot.loop.create_task(self._restart_close_timer()) # Start or restart thread auto close
1816+
# Only restart auto-close if it wasn't explicitly cancelled
1817+
if not self.auto_close_cancelled:
1818+
self.bot.loop.create_task(self._restart_close_timer()) # Start or restart thread auto close
1819+
elif not note and not from_mod:
1820+
await self.cancel_closure(all=True)
18141821

18151822
if self.close_task is not None:
18161823
# cancel closing if a thread message is sent.

0 commit comments

Comments
 (0)