Skip to content

Commit 312a7bc

Browse files
committed
Move channel & thread syncer to sync utils file
1 parent 8ed588a commit 312a7bc

File tree

2 files changed

+111
-106
lines changed

2 files changed

+111
-106
lines changed

metricity/exts/event_listeners/_syncer_utils.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import discord
22
from pydis_core.utils import logging
3+
from sqlalchemy import update
34
from sqlalchemy.ext.asyncio import AsyncSession
45

56
from metricity import models
7+
from metricity.bot import Bot
8+
from metricity.config import BotConfig
9+
from metricity.database import async_session
610

711
log = logging.get_logger(__name__)
812

@@ -39,3 +43,110 @@ async def sync_message(message: discord.Message, sess: AsyncSession, *, from_thr
3943
args["thread_id"] = str(thread.id)
4044

4145
sess.add(models.Message(**args))
46+
47+
48+
async def sync_channels(bot: Bot, guild: discord.Guild) -> None:
49+
"""Sync channels and categories with the database."""
50+
bot.channel_sync_in_progress.clear()
51+
52+
log.info("Beginning category synchronisation process")
53+
54+
async with async_session() as sess:
55+
for channel in guild.channels:
56+
if isinstance(channel, discord.CategoryChannel):
57+
if existing_cat := await sess.get(models.Category, str(channel.id)):
58+
existing_cat.name = channel.name
59+
else:
60+
sess.add(models.Category(id=str(channel.id), name=channel.name, deleted=False))
61+
62+
await sess.commit()
63+
64+
log.info("Category synchronisation process complete, synchronising deleted categories")
65+
66+
async with async_session() as sess:
67+
await sess.execute(
68+
update(models.Category)
69+
.where(~models.Category.id.in_(
70+
[str(channel.id) for channel in guild.channels if isinstance(channel, discord.CategoryChannel)],
71+
))
72+
.values(deleted=True),
73+
)
74+
await sess.commit()
75+
76+
log.info("Deleted category synchronisation process complete, synchronising channels")
77+
78+
async with async_session() as sess:
79+
for channel in guild.channels:
80+
if channel.category and channel.category.id in BotConfig.ignore_categories:
81+
continue
82+
83+
if not isinstance(channel, discord.CategoryChannel):
84+
category_id = str(channel.category.id) if channel.category else None
85+
# Cast to bool so is_staff is False if channel.category is None
86+
is_staff = channel.id in BotConfig.staff_channels or bool(
87+
channel.category and channel.category.id in BotConfig.staff_categories,
88+
)
89+
if db_chan := await sess.get(models.Channel, str(channel.id)):
90+
db_chan.name = channel.name
91+
else:
92+
sess.add(models.Channel(
93+
id=str(channel.id),
94+
name=channel.name,
95+
category_id=category_id,
96+
is_staff=is_staff,
97+
deleted=False,
98+
))
99+
100+
await sess.commit()
101+
102+
log.info("Channel synchronisation process complete, synchronising deleted channels")
103+
104+
async with async_session() as sess:
105+
await sess.execute(
106+
update(models.Channel)
107+
.where(~models.Channel.id.in_([str(channel.id) for channel in guild.channels]))
108+
.values(deleted=True),
109+
)
110+
await sess.commit()
111+
112+
log.info("Deleted channel synchronisation process complete, synchronising threads")
113+
114+
async with async_session() as sess:
115+
for thread in guild.threads:
116+
if thread.parent and thread.parent.category:
117+
if thread.parent.category.id in BotConfig.ignore_categories:
118+
continue
119+
else:
120+
# This is a forum channel, not currently supported by Discord.py. Ignore it.
121+
continue
122+
123+
if db_thread := await sess.get(models.Thread, str(thread.id)):
124+
db_thread.name = thread.name
125+
db_thread.archived = thread.archived
126+
db_thread.auto_archive_duration = thread.auto_archive_duration
127+
db_thread.locked = thread.locked
128+
db_thread.type = thread.type.name
129+
else:
130+
insert_thread(thread, sess)
131+
await sess.commit()
132+
133+
log.info("Thread synchronisation process complete, finished synchronising guild.")
134+
bot.channel_sync_in_progress.set()
135+
136+
137+
async def sync_thread_archive_state(guild: discord.Guild) -> None:
138+
"""Sync the archive state of all threads in the database with the state in guild."""
139+
active_thread_ids = [str(thread.id) for thread in guild.threads]
140+
141+
async with async_session() as sess:
142+
await sess.execute(
143+
update(models.Thread)
144+
.where(models.Thread.id.in_(active_thread_ids))
145+
.values(archived=False),
146+
)
147+
await sess.execute(
148+
update(models.Thread)
149+
.where(~models.Thread.id.in_(active_thread_ids))
150+
.values(archived=True),
151+
)
152+
await sess.commit()

metricity/exts/event_listeners/guild_listeners.py

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -98,112 +98,6 @@ async def sync_guild(self) -> None:
9898

9999
self.bot.sync_process_complete.set()
100100

101-
@staticmethod
102-
async def sync_thread_archive_state(guild: discord.Guild) -> None:
103-
"""Sync the archive state of all threads in the database with the state in guild."""
104-
active_thread_ids = [str(thread.id) for thread in guild.threads]
105-
106-
async with async_session() as sess:
107-
await sess.execute(
108-
update(models.Thread)
109-
.where(models.Thread.id.in_(active_thread_ids))
110-
.values(archived=False),
111-
)
112-
await sess.execute(
113-
update(models.Thread)
114-
.where(~models.Thread.id.in_(active_thread_ids))
115-
.values(archived=True),
116-
)
117-
await sess.commit()
118-
119-
async def sync_channels(self, guild: discord.Guild) -> None:
120-
"""Sync channels and categories with the database."""
121-
self.bot.channel_sync_in_progress.clear()
122-
123-
log.info("Beginning category synchronisation process")
124-
125-
async with async_session() as sess:
126-
for channel in guild.channels:
127-
if isinstance(channel, discord.CategoryChannel):
128-
if existing_cat := await sess.get(models.Category, str(channel.id)):
129-
existing_cat.name = channel.name
130-
else:
131-
sess.add(models.Category(id=str(channel.id), name=channel.name, deleted=False))
132-
133-
await sess.commit()
134-
135-
log.info("Category synchronisation process complete, synchronising deleted categories")
136-
137-
async with async_session() as sess:
138-
await sess.execute(
139-
update(models.Category)
140-
.where(~models.Category.id.in_(
141-
[str(channel.id) for channel in guild.channels if isinstance(channel, discord.CategoryChannel)],
142-
))
143-
.values(deleted=True),
144-
)
145-
await sess.commit()
146-
147-
log.info("Deleted category synchronisation process complete, synchronising channels")
148-
149-
async with async_session() as sess:
150-
for channel in guild.channels:
151-
if channel.category and channel.category.id in BotConfig.ignore_categories:
152-
continue
153-
154-
if not isinstance(channel, discord.CategoryChannel):
155-
category_id = str(channel.category.id) if channel.category else None
156-
# Cast to bool so is_staff is False if channel.category is None
157-
is_staff = channel.id in BotConfig.staff_channels or bool(
158-
channel.category and channel.category.id in BotConfig.staff_categories,
159-
)
160-
if db_chan := await sess.get(models.Channel, str(channel.id)):
161-
db_chan.name = channel.name
162-
else:
163-
sess.add(models.Channel(
164-
id=str(channel.id),
165-
name=channel.name,
166-
category_id=category_id,
167-
is_staff=is_staff,
168-
deleted=False,
169-
))
170-
171-
await sess.commit()
172-
173-
log.info("Channel synchronisation process complete, synchronising deleted channels")
174-
175-
async with async_session() as sess:
176-
await sess.execute(
177-
update(models.Channel)
178-
.where(~models.Channel.id.in_([str(channel.id) for channel in guild.channels]))
179-
.values(deleted=True),
180-
)
181-
await sess.commit()
182-
183-
log.info("Deleted channel synchronisation process complete, synchronising threads")
184-
185-
async with async_session() as sess:
186-
for thread in guild.threads:
187-
if thread.parent and thread.parent.category:
188-
if thread.parent.category.id in BotConfig.ignore_categories:
189-
continue
190-
else:
191-
# This is a forum channel, not currently supported by Discord.py. Ignore it.
192-
continue
193-
194-
if db_thread := await sess.get(models.Thread, str(thread.id)):
195-
db_thread.name = thread.name
196-
db_thread.archived = thread.archived
197-
db_thread.auto_archive_duration = thread.auto_archive_duration
198-
db_thread.locked = thread.locked
199-
db_thread.type = thread.type.name
200-
else:
201-
_utils.insert_thread(thread, sess)
202-
await sess.commit()
203-
204-
log.info("Thread synchronisation process complete, finished synchronising guild.")
205-
self.bot.channel_sync_in_progress.set()
206-
207101
@commands.Cog.listener()
208102
async def on_guild_channel_create(self, channel: discord.abc.GuildChannel) -> None:
209103
"""Sync the channels when one is created."""

0 commit comments

Comments
 (0)