Skip to content
Merged
Show file tree
Hide file tree
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: 33 additions & 8 deletions metricity/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,13 @@ async def sync_channels(guild: Guild) -> None:

log.info("Channel synchronisation process complete, synchronising threads")

active_thread_ids = []
for thread in guild.threads:
active_thread_ids.append(str(thread.id))
if thread.parent and thread.parent.category:
if thread.parent.category.id in BotConfig.ignore_categories:
continue
else:
# This is a forum channel, not currently supported by Discord.py. Ignore it.
continue

if db_thread := await Thread.get(str(thread.id)):
await db_thread.update(
Expand All @@ -133,12 +134,15 @@ async def sync_channels(guild: Guild) -> None:
).apply()
else:
await insert_thread(thread)
channel_sync_in_progress.set()

async with db.transaction():
async for db_thread in Thread.query.gino.iterate():
await db_thread.update(archived=db_thread.id not in active_thread_ids).apply()

channel_sync_in_progress.set()
async def sync_thread_archive_state(guild: Guild) -> None:
"""Sync the archive state of all threads in the database with the state in guild."""
active_thread_ids = [str(thread.id) for thread in guild.threads]
async with db.transaction() as tx:
async for db_thread in tx.connection.iterate(Thread.query):
await db_thread.update(archived=db_thread.id not in active_thread_ids).apply()


def gen_chunks(
Expand Down Expand Up @@ -180,6 +184,23 @@ async def on_guild_channel_update(_before: Messageable, channel: Messageable) ->
await sync_channels(channel.guild)


@bot.event
async def on_thread_join(thread: ThreadChannel) -> None:
"""
Sync channels when thread join is triggered.

Unlike what the name suggested, this is also triggered when:
- A thread is created.
- An un-cached thread is un-archived.
"""
await db_ready.wait()

if thread.guild.id != BotConfig.guild_id:
return

await sync_channels(thread.guild)


@bot.event
async def on_thread_update(_before: Messageable, thread: Messageable) -> None:
"""Sync the channels when one is updated."""
Expand All @@ -203,6 +224,9 @@ async def on_guild_available(guild: Guild) -> None:

await sync_channels(guild)

log.info("Beginning thread archive state synchronisation process")
await sync_thread_archive_state(guild)

log.info("Beginning user synchronisation process")

await User.update.values(in_guild=False).gino.status()
Expand Down Expand Up @@ -381,11 +405,12 @@ async def on_message(message: DiscordMessage) -> None:
}

if isinstance(message.channel, ThreadChannel):
if not message.channel.parent:
# This is a forum channel, not currently supported by Discord.py. Ignore it.
return
thread = message.channel
args["channel_id"] = str(thread.parent_id)
args["thread_id"] = str(thread.id)
if not await Thread.get(str(thread.id)):
await insert_thread(thread)

await Message.create(**args)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "metricity"
version = "1.3.0"
version = "1.3.1"
description = "Advanced metric collection for the Python Discord server"
authors = ["Joe Banks <joseph@josephbanks.me>"]
license = "MIT"
Expand Down