Skip to content

Commit

Permalink
Merge pull request #143 from python-discord/jb3/deleted-channel-column
Browse files Browse the repository at this point in the history
Add column for a channel that has been deleted
  • Loading branch information
jb3 authored Apr 7, 2024
2 parents ac45f4c + d192c7d commit a14f454
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Add deleted column to channel model.
Revision ID: c09a64cac3cb
Revises: 03655ce2097b
Create Date: 2024-04-07 22:58:53.186355
"""

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = "c09a64cac3cb"
down_revision = "03655ce2097b"
branch_labels = None
depends_on = None


def upgrade() -> None:
"""Apply the current migration."""
op.add_column("channels", sa.Column("deleted", sa.Boolean(), nullable=False, server_default="False", default=False))


def downgrade() -> None:
"""Revert the current migration."""
op.drop_column("channels", "deleted")
21 changes: 20 additions & 1 deletion metricity/exts/event_listeners/guild_listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,22 @@ async def sync_channels(self, guild: discord.Guild) -> None:
name=channel.name,
category_id=category_id,
is_staff=is_staff,
deleted=False,
))

await sess.commit()

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

async with async_session() as sess:
await sess.execute(
update(models.Channel)
.where(~models.Channel.id.in_([str(channel.id) for channel in guild.channels]))
.values(deleted=True),
)
await sess.commit()

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

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

await self.sync_channels(channel.guild)

@commands.Cog.listener()
async def on_guild_channel_delete(self, channel: discord.abc.GuildChannel) -> None:
"""Set the deleted flag to true when a channel is deleted."""
if channel.guild.id != BotConfig.guild_id:
return

await self.sync_channels(channel.guild)

@commands.Cog.listener()
async def on_guild_channel_update(
self,
Expand Down
1 change: 1 addition & 0 deletions metricity/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Channel(Base):
name: Mapped[str]
category_id: Mapped[str | None] = mapped_column(ForeignKey("categories.id", ondelete="CASCADE"))
is_staff: Mapped[bool]
deleted: Mapped[bool] = mapped_column(default=False)


class Thread(Base):
Expand Down

0 comments on commit a14f454

Please sign in to comment.