aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar wookie184 <[email protected]>2022-04-30 19:28:47 +0100
committerGravatar GitHub <[email protected]>2022-04-30 19:28:47 +0100
commit587475465c2c78c47c48af73c19ecf23d2db7268 (patch)
tree4f13a53d207d2670956764b12211d6bc22151150
parentMerge pull request #2160 from python-discord/2128 (diff)
parentMerge branch 'main' into improve-thread-bump (diff)
Merge pull request #2129 from python-discord/improve-thread-bump
Use site api over redis for thread bumping
-rw-r--r--bot/exts/utils/thread_bumper.py41
1 files changed, 30 insertions, 11 deletions
diff --git a/bot/exts/utils/thread_bumper.py b/bot/exts/utils/thread_bumper.py
index ee0636b37..743919d4e 100644
--- a/bot/exts/utils/thread_bumper.py
+++ b/bot/exts/utils/thread_bumper.py
@@ -2,6 +2,7 @@ import typing as t
import discord
from async_rediscache import RedisCache
+from botcore.site_api import ResponseCodeError
from discord.ext import commands
from bot import constants
@@ -11,6 +12,7 @@ from bot.pagination import LinePaginator
from bot.utils import channel
log = get_logger(__name__)
+THREAD_BUMP_ENDPOINT = "bot/bumped-threads"
class ThreadBumper(commands.Cog):
@@ -45,7 +47,7 @@ class ThreadBumper(commands.Cog):
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)
@@ -54,18 +56,23 @@ class ThreadBumper(commands.Cog):
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:
@@ -82,10 +89,15 @@ class ThreadBumper(commands.Cog):
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"))
@@ -97,21 +109,23 @@ class ThreadBumper(commands.Cog):
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:
@@ -123,7 +137,12 @@ class ThreadBumper(commands.Cog):
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: