Skip to content

Commit a14f454

Browse files
authored
Merge pull request #143 from python-discord/jb3/deleted-channel-column
Add column for a channel that has been deleted
2 parents ac45f4c + d192c7d commit a14f454

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Add deleted column to channel model.
3+
4+
Revision ID: c09a64cac3cb
5+
Revises: 03655ce2097b
6+
Create Date: 2024-04-07 22:58:53.186355
7+
8+
"""
9+
10+
import sqlalchemy as sa
11+
12+
from alembic import op
13+
14+
# revision identifiers, used by Alembic.
15+
revision = "c09a64cac3cb"
16+
down_revision = "03655ce2097b"
17+
branch_labels = None
18+
depends_on = None
19+
20+
21+
def upgrade() -> None:
22+
"""Apply the current migration."""
23+
op.add_column("channels", sa.Column("deleted", sa.Boolean(), nullable=False, server_default="False", default=False))
24+
25+
26+
def downgrade() -> None:
27+
"""Revert the current migration."""
28+
op.drop_column("channels", "deleted")

metricity/exts/event_listeners/guild_listeners.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,22 @@ async def sync_channels(self, guild: discord.Guild) -> None:
153153
name=channel.name,
154154
category_id=category_id,
155155
is_staff=is_staff,
156+
deleted=False,
156157
))
157158

158159
await sess.commit()
159160

160-
log.info("Channel synchronisation process complete, synchronising threads")
161+
log.info("Channel synchronisation process complete, synchronising deleted channels")
162+
163+
async with async_session() as sess:
164+
await sess.execute(
165+
update(models.Channel)
166+
.where(~models.Channel.id.in_([str(channel.id) for channel in guild.channels]))
167+
.values(deleted=True),
168+
)
169+
await sess.commit()
170+
171+
log.info("Deleted channel synchronisation process complete, synchronising threads")
161172

162173
async with async_session() as sess:
163174
for thread in guild.threads:
@@ -189,6 +200,14 @@ async def on_guild_channel_create(self, channel: discord.abc.GuildChannel) -> No
189200

190201
await self.sync_channels(channel.guild)
191202

203+
@commands.Cog.listener()
204+
async def on_guild_channel_delete(self, channel: discord.abc.GuildChannel) -> None:
205+
"""Set the deleted flag to true when a channel is deleted."""
206+
if channel.guild.id != BotConfig.guild_id:
207+
return
208+
209+
await self.sync_channels(channel.guild)
210+
192211
@commands.Cog.listener()
193212
async def on_guild_channel_update(
194213
self,

metricity/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Channel(Base):
3030
name: Mapped[str]
3131
category_id: Mapped[str | None] = mapped_column(ForeignKey("categories.id", ondelete="CASCADE"))
3232
is_staff: Mapped[bool]
33+
deleted: Mapped[bool] = mapped_column(default=False)
3334

3435

3536
class Thread(Base):

0 commit comments

Comments
 (0)