Skip to content

Commit 78aec10

Browse files
committed
fix: thread_auto_close bug.
This resolves an issue introduced in: #3423 Autocloses would fail.
1 parent c3edf86 commit 78aec10

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

core/thread.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,10 @@ async def close(
10561056
"""Close a thread now or after a set time in seconds"""
10571057

10581058
# restarts the after timer
1059-
await self.cancel_closure(auto_close)
1059+
await self.cancel_closure(
1060+
auto_close,
1061+
mark_auto_close_cancelled=not auto_close,
1062+
)
10601063

10611064
if after > 0:
10621065
# TODO: Add somewhere to clean up broken closures
@@ -1100,7 +1103,7 @@ async def _close(self, closer, silent=False, delete_channel=True, message=None,
11001103
logger.error("Thread already closed: %s.", e)
11011104
return
11021105

1103-
await self.cancel_closure(all=True)
1106+
await self.cancel_closure(all=True, mark_auto_close_cancelled=False)
11041107

11051108
# Cancel auto closing the thread if closed by any means.
11061109

@@ -1274,18 +1277,32 @@ async def _disable_dm_creation_menu(self) -> None:
12741277
except Exception as inner_e:
12751278
logger.debug("Failed removing view from DM menu message: %s", inner_e)
12761279

1277-
async def cancel_closure(self, auto_close: bool = False, all: bool = False) -> None:
1280+
async def cancel_closure(
1281+
self,
1282+
auto_close: bool = False,
1283+
all: bool = False,
1284+
*,
1285+
mark_auto_close_cancelled: bool = True,
1286+
) -> None:
12781287
if self.close_task is not None and (not auto_close or all):
12791288
self.close_task.cancel()
12801289
self.close_task = None
12811290
if self.auto_close_task is not None and (auto_close or all):
12821291
self.auto_close_task.cancel()
12831292
self.auto_close_task = None
1284-
self.auto_close_cancelled = True # Mark auto-close as explicitly cancelled
1285-
1286-
to_update = self.bot.config["closures"].pop(str(self.id), None)
1287-
if to_update is not None:
1288-
await self.bot.config.update()
1293+
if mark_auto_close_cancelled:
1294+
self.auto_close_cancelled = True # Mark auto-close as explicitly cancelled
1295+
1296+
closure_key = str(self.id)
1297+
existing = self.bot.config["closures"].get(closure_key)
1298+
if existing is not None:
1299+
existing_is_auto = bool(existing.get("auto_close", False))
1300+
should_remove = (
1301+
all or (auto_close and existing_is_auto) or ((not auto_close) and (not existing_is_auto))
1302+
)
1303+
if should_remove:
1304+
self.bot.config["closures"].pop(closure_key, None)
1305+
await self.bot.config.update()
12891306

12901307
async def _restart_close_timer(self):
12911308
"""
@@ -1821,11 +1838,14 @@ async def send(
18211838
return await destination.send(embed=embed)
18221839

18231840
if not note and from_mod:
1824-
# Only restart auto-close if it wasn't explicitly cancelled
1841+
# Only restart auto-close if it wasn't explicitly cancelled.
1842+
# Auto-close is driven by the last moderator reply.
18251843
if not self.auto_close_cancelled:
18261844
self.bot.loop.create_task(self._restart_close_timer()) # Start or restart thread auto close
18271845
elif not note and not from_mod:
1828-
await self.cancel_closure(all=True)
1846+
# If the user replied last, the thread should not auto-close.
1847+
# Cancel any pending auto-close without marking it as an explicit cancellation.
1848+
await self.cancel_closure(auto_close=True, mark_auto_close_cancelled=False)
18291849

18301850
if self.close_task is not None:
18311851
# cancel closing if a thread message is sent.

0 commit comments

Comments
 (0)