Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions bot/exts/utils/thread_bumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import discord
from async_rediscache import RedisCache
from botcore.site_api import ResponseCodeError
from discord.ext import commands

from bot import constants
Expand All @@ -11,6 +12,7 @@
from bot.utils import channel

log = get_logger(__name__)
THREAD_BUMP_ENDPOINT = "bot/bumped-threads"


class ThreadBumper(commands.Cog):
Expand Down Expand Up @@ -45,7 +47,7 @@ async def unarchive_threads_not_manually_archived(self, threads: list[discord.Th
thread.name,
thread.id
)
await self.threads_to_bump.delete(thread.id)
await self.bot.api_client.delete(f"{THREAD_BUMP_ENDPOINT}/{thread.id}")
else:
await thread.edit(archived=False)

Expand All @@ -54,18 +56,23 @@ async def cog_load(self) -> None:
await self.bot.wait_until_guild_available()

threads_to_maybe_bump = []
for thread_id, _ in await self.threads_to_bump.items():
for thread_id in await self.bot.api_client.get(THREAD_BUMP_ENDPOINT):
try:
thread = await channel.get_or_fetch_channel(thread_id)
except discord.NotFound:
log.info("Thread %d has been deleted, removing from bumped threads.", thread_id)
await self.threads_to_bump.delete(thread_id)
await self.bot.api_client.delete(f"{THREAD_BUMP_ENDPOINT}/{thread_id}")
continue

if not isinstance(thread, discord.Thread):
await self.bot.api_client.delete(f"{THREAD_BUMP_ENDPOINT}/{thread_id}")
continue

if thread.archived:
threads_to_maybe_bump.append(thread)

await self.unarchive_threads_not_manually_archived(threads_to_maybe_bump)
if threads_to_maybe_bump:
await self.unarchive_threads_not_manually_archived(threads_to_maybe_bump)

@commands.group(name="bump")
async def thread_bump_group(self, ctx: commands.Context) -> None:
Expand All @@ -82,10 +89,15 @@ async def add_thread_to_bump_list(self, ctx: commands.Context, thread: t.Optiona
else:
raise commands.BadArgument("You must provide a thread, or run this command within a thread.")

if await self.threads_to_bump.contains(thread.id):
try:
await self.bot.api_client.get(f"{THREAD_BUMP_ENDPOINT}/{thread.id}")
except ResponseCodeError as e:
if e.status != 404:
raise
else:
raise commands.BadArgument("This thread is already in the bump list.")

await self.threads_to_bump.set(thread.id, "sentinel")
await self.bot.api_client.post(THREAD_BUMP_ENDPOINT, data={"thread_id": thread.id})
await ctx.send(f":ok_hand:{thread.mention} has been added to the bump list.")

@thread_bump_group.command(name="remove", aliases=("r", "rem", "d", "del", "delete"))
Expand All @@ -97,21 +109,23 @@ async def remove_thread_from_bump_list(self, ctx: commands.Context, thread: t.Op
else:
raise commands.BadArgument("You must provide a thread, or run this command within a thread.")

if not await self.threads_to_bump.contains(thread.id):
try:
await self.bot.api_client.get(f"{THREAD_BUMP_ENDPOINT}/{thread.id}")
except ResponseCodeError:
raise commands.BadArgument("This thread is not in the bump list.")

await self.threads_to_bump.delete(thread.id)
await self.bot.api_client.delete(f"{THREAD_BUMP_ENDPOINT}/{thread.id}")
await ctx.send(f":ok_hand: {thread.mention} has been removed from the bump list.")

@thread_bump_group.command(name="list", aliases=("get",))
async def list_all_threads_in_bump_list(self, ctx: commands.Context) -> None:
"""List all the threads in the bump list."""
lines = [f"<#{k}>" for k, _ in await self.threads_to_bump.items()]
lines = [f"<#{thread_id}>" for thread_id in await self.bot.api_client.get(THREAD_BUMP_ENDPOINT)]
embed = discord.Embed(
title="Threads in the bump list",
colour=constants.Colours.blue
)
await LinePaginator.paginate(lines, ctx, embed)
await LinePaginator.paginate(lines, ctx, embed, max_lines=10)

@commands.Cog.listener()
async def on_thread_update(self, _: discord.Thread, after: discord.Thread) -> None:
Expand All @@ -123,7 +137,12 @@ async def on_thread_update(self, _: discord.Thread, after: discord.Thread) -> No
if not after.archived:
return

if await self.threads_to_bump.contains(after.id):
try:
await self.bot.api_client.get(f"{THREAD_BUMP_ENDPOINT}/{after.id}")
except ResponseCodeError as e:
if e.status != 404:
raise
else:
await self.unarchive_threads_not_manually_archived([after])

async def cog_check(self, ctx: commands.Context) -> bool:
Expand Down