Skip to content

Commit 1326e5d

Browse files
committed
Auto close limbo open threads
1 parent 4050d31 commit 1326e5d

File tree

4 files changed

+44
-17
lines changed

4 files changed

+44
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ however, insignificant breaking changes does not guarantee a major version bump,
2121
- This config takes precedence over `reply_without_command`.
2222
- `?logs responded [user]` command, it will show all logs that the user has sent an reply. (Thanks to papiersnipper PR#288)
2323
- `user` when not provided, defaults to the user who ran the command.
24+
- Open threads in limbo now auto closes if the channel cannot be found. This check is done every time the bot restarts.
2425

2526
### Changed
2627

bot.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,29 @@ async def on_ready(self):
502502
auto_close=items.get("auto_close", False),
503503
)
504504

505+
for log in await self.api.get_open_logs():
506+
if self.get_channel(int(log['channel_id'])) is None:
507+
logger.debug("Unable to resolve thread with channel %s.", log['channel_id'])
508+
log_data = await self.api.post_log(
509+
log['channel_id'],
510+
{
511+
"open": False,
512+
"closed_at": str(datetime.utcnow()),
513+
"close_message": "Channel has been deleted, no closer found.",
514+
"closer": {
515+
"id": str(self.user.id),
516+
"name": self.user.name,
517+
"discriminator": self.user.discriminator,
518+
"avatar_url": str(self.user.avatar_url),
519+
"mod": True,
520+
},
521+
},
522+
)
523+
if log_data:
524+
logger.debug("Successfully closed thread with channel %s.", log['channel_id'])
525+
else:
526+
logger.debug("Failed to close thread with channel %s, skipping.", log['channel_id'])
527+
505528
self.metadata_loop = tasks.Loop(
506529
self.post_metadata,
507530
seconds=0,

core/clients.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,23 @@ async def get_user_logs(self, user_id: Union[str, int]) -> list:
9393
return await self.logs.find(query, projection).to_list(None)
9494

9595
async def get_responded_logs(self, user_id: Union[str, int]) -> list:
96-
entries = []
97-
async for l in self.bot.db.logs.find(
98-
{
99-
"open": False,
100-
"messages": {
101-
"$elemMatch": {
102-
"author.id": str(user_id),
103-
"author.mod": True,
104-
"type": {"$in": ["anonymous", "thread_message"]},
105-
}
106-
},
107-
}
108-
):
109-
entries.append(l)
110-
return entries
96+
query = {
97+
"open": False,
98+
"messages": {
99+
"$elemMatch": {
100+
"author.id": str(user_id),
101+
"author.mod": True,
102+
"type": {"$in": ["anonymous", "thread_message"]},
103+
}
104+
},
105+
}
106+
return await self.logs.find(query).to_list(None)
107+
108+
async def get_open_logs(self) -> list:
109+
query = {
110+
"open": True
111+
}
112+
return await self.logs.find(query).to_list(None)
111113

112114
async def get_log(self, channel_id: Union[str, int]) -> dict:
113115
logger.debug("Retrieving channel %s logs.", channel_id)

core/thread.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,8 +762,9 @@ async def send(
762762

763763
if delete_message:
764764
try:
765-
await message.delete()
766-
except discord.HTTPException:
765+
if isinstance(message.channel, discord.TextChannel):
766+
await message.delete()
767+
except Exception:
767768
logger.warning('Cannot delete message.', exc_info=True)
768769
return msg
769770

0 commit comments

Comments
 (0)