Skip to content

Commit

Permalink
Add clear server post cache command
Browse files Browse the repository at this point in the history
  • Loading branch information
amadejkastelic committed Aug 19, 2024
1 parent 477d5eb commit 74d4e5a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
51 changes: 41 additions & 10 deletions bot/integrations/discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from bot import logger
from bot import service
from bot.common import utils
from bot.integrations import mixins


class CustomView(ui.View):
Expand All @@ -38,30 +39,37 @@ async def on_click(
)


class DiscordClient(discord.Client):
class DiscordClient(mixins.BotMixin, discord.Client):
VENDOR = constants.ServerVendor.DISCORD

def __init__(self, *, intents: discord.Intents, **options: typing.Any) -> None:
super().__init__(intents=intents, **options)

commands: typing.List[app_commands.Command] = [
app_commands.Command(
name='embed',
description='Embeds media directly into discord',
callback=self.command_embed,
callback=self.embed_cmd,
),
app_commands.Command(
name='help',
description='Prints configuration for this server',
callback=self.command_help,
callback=self.help_cmd,
),
app_commands.Command(
name='silence',
description='(Un)Ban a user from using embed commands',
callback=self.silence_user,
callback=self.silence_user_cmd,
),
app_commands.Command(
name='postfmt',
description='Fetches post format for specified site',
callback=self.get_post_format,
callback=self.get_post_format_cmd,
),
app_commands.Command(
name='clear_cache',
description='Clears post cache for server',
callback=self.clear_cache_cmd,
),
]

Expand Down Expand Up @@ -136,7 +144,7 @@ async def on_reaction_add(self, reaction: discord.Reaction, user: discord.User):
)
await reaction.message.delete()

async def command_embed(self, interaction: discord.Interaction, url: str, spoiler: bool = False) -> None:
async def embed_cmd(self, interaction: discord.Interaction, url: str, spoiler: bool = False) -> None:
await interaction.response.defer()

if service.should_handle_url(url) is False:
Expand Down Expand Up @@ -167,7 +175,7 @@ async def command_embed(self, interaction: discord.Interaction, url: str, spoile
author=interaction.user,
)

async def command_help(self, interaction: discord.Interaction) -> None:
async def help_cmd(self, interaction: discord.Interaction) -> None:
await interaction.response.defer()

response = service.get_server_info(
Expand All @@ -184,7 +192,9 @@ async def command_help(self, interaction: discord.Interaction) -> None:
await interaction.followup.send(content=f'{interaction.user.mention}\n{response}', view=CustomView())

@checks.has_permissions(administrator=True)
async def silence_user(self, interaction: discord.Interaction, member: discord.Member, unban: bool = False) -> None:
async def silence_user_cmd(
self, interaction: discord.Interaction, member: discord.Member, unban: bool = False
) -> None:
await interaction.response.defer(ephemeral=True)

if not isinstance(interaction.user, discord.Member):
Expand Down Expand Up @@ -220,18 +230,39 @@ async def silence_user(self, interaction: discord.Interaction, member: discord.M
)

@checks.has_permissions(administrator=True)
async def get_post_format(self, interaction: discord.Interaction, site: constants.Integration) -> None:
async def get_post_format_cmd(self, interaction: discord.Interaction, integration: constants.Integration) -> None:
await interaction.response.defer(ephemeral=True)

await interaction.followup.send(
content=service.get_post_format(
server_vendor=constants.ServerVendor.DISCORD,
server_uid=str(interaction.guild_id),
integration=site,
integration=integration,
),
ephemeral=True,
)

@checks.has_permissions(administrator=True)
async def clear_cache_cmd(
self,
interaction: discord.Interaction,
integration: typing.Optional[constants.Integration],
) -> None:
await interaction.response.defer(ephemeral=True)

res = self.clear_cache(server_vendor_uid=str(interaction.guild_id), integration=integration)
logger.info(
'Admin cleared post cache',
deleted_cnt=res,
integration=integration.value if integration else 'all',
admin=interaction.user.id,
)

await interaction.followup.send(
content=f'Deleted {res} posts from cache.',
ephemeral=True,
)

async def _send_post(
self,
post: domain.Post,
Expand Down
20 changes: 20 additions & 0 deletions bot/integrations/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import typing

from bot import constants
from bot import models


class BotMixin:
VENDOR: constants.ServerVendor

def clear_cache(self, server_vendor_uid: str, integration: typing.Optional[constants.Integration] = None) -> int:
kwargs = {
'serverpost__server__vendor_uid': server_vendor_uid,
'serverpost__server__vendor': self.VENDOR,
}
if integration:
kwargs['integration'] = integration

num_deleted, _ = models.Post.objects.filter(**kwargs).delete()

return num_deleted
2 changes: 0 additions & 2 deletions bot/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
def create_server(
vendor_uid: str,
vendor: constants.ServerVendor,
owner_id: typing.Optional[int] = None,
tier: constants.ServerTier = constants.ServerTier.FREE,
status: constants.ServerStatus = constants.ServerStatus.ACTIVE,
) -> domain.Server:
Expand All @@ -24,7 +23,6 @@ def create_server(
vendor=vendor,
tier=tier,
status=status,
owner_id=owner_id,
)
# Add some default integrations to server
integrations = models.ServerIntegration.objects.bulk_create(
Expand Down

0 comments on commit 74d4e5a

Please sign in to comment.