Migrate admin/purge, ref #91

This commit is contained in:
Zeva Rose 2022-02-02 20:38:55 -07:00
parent d4e3d17393
commit 716f16946d

View file

@ -1,33 +1,31 @@
"""J.A.R.V.I.S. PurgeCog."""
from discord import TextChannel
from discord.ext import commands
from discord_slash import SlashContext, cog_ext
from discord_slash.utils.manage_commands import create_option
from dis_snek import InteractionContext, Permissions, Scale, Snek
from dis_snek.models.discord.channel import TextChannel
from dis_snek.models.snek.application_commands import (
OptionTypes,
slash_command,
slash_option,
)
from jarvis.db.models import Autopurge, Purge
from jarvis.utils.permissions import admin_or_permissions
class PurgeCog(commands.Cog):
class PurgeCog(Scale):
"""J.A.R.V.I.S. PurgeCog."""
def __init__(self, bot: commands.Bot):
def __init__(self, bot: Snek):
self.bot = bot
@cog_ext.cog_slash(
name="purge",
description="Purge messages from channel",
options=[
create_option(
name="amount",
description="Amount of messages to purge",
required=False,
option_type=4,
)
],
@slash_command(name="purge", description="Purge messages from channel")
@slash_option(
name="amount",
description="Amount of messages to purge, default 10",
option_type=OptionTypes.INTEGER,
required=False,
)
@admin_or_permissions(manage_messages=True)
async def _purge(self, ctx: SlashContext, amount: int = 10) -> None:
@admin_or_permissions(Permissions.MANAGE_MESSAGES)
async def _purge(self, ctx: InteractionContext, amount: int = 10) -> None:
if amount < 1:
await ctx.send("Amount must be >= 1", hidden=True)
return
@ -44,28 +42,24 @@ class PurgeCog(commands.Cog):
count=amount,
).save()
@cog_ext.cog_subcommand(
base="autopurge",
name="add",
description="Automatically purge messages after x seconds",
options=[
create_option(
name="channel",
description="Channel to autopurge",
option_type=7,
required=True,
),
create_option(
name="delay",
description="Seconds to keep message before purge, default 30",
option_type=4,
required=False,
),
],
@slash_command(
name="autopurge", sub_cmd_name="add", sub_cmd_description="Automatically purge messages"
)
@admin_or_permissions(manage_messages=True)
@slash_option(
name="channel",
description="Channel to autopurge",
option_type=OptionTypes.CHANNEL,
required=True,
)
@slash_option(
name="delay",
description="Seconds to keep message before purge, default 30",
option_type=OptionTypes.INTEGER,
required=False,
)
@admin_or_permissions(Permissions.MANAGE_MESSAGES)
async def _autopurge_add(
self, ctx: SlashContext, channel: TextChannel, delay: int = 30
self, ctx: InteractionContext, channel: TextChannel, delay: int = 30
) -> None:
if not isinstance(channel, TextChannel):
await ctx.send("Channel must be a TextChannel", hidden=True)
@ -88,21 +82,17 @@ class PurgeCog(commands.Cog):
).save()
await ctx.send(f"Autopurge set up on {channel.mention}, delay is {delay} seconds")
@cog_ext.cog_subcommand(
base="autopurge",
name="remove",
description="Remove an autopurge",
options=[
create_option(
name="channel",
description="Channel to remove from autopurge",
option_type=7,
required=True,
),
],
@slash_command(
name="autopurge", sub_cmd_name="remove", sub_cmd_description="Remove an autopurge"
)
@admin_or_permissions(manage_messages=True)
async def _autopurge_remove(self, ctx: SlashContext, channel: TextChannel) -> None:
@slash_option(
name="channel",
description="Channel to remove from autopurge",
option_type=OptionTypes.CHANNEL,
required=True,
)
@admin_or_permissions(Permissions.MANAGE_MESSAGES)
async def _autopurge_remove(self, ctx: InteractionContext, channel: TextChannel) -> None:
autopurge = Autopurge.objects(guild=ctx.guild.id, channel=channel.id)
if not autopurge:
await ctx.send("Autopurge does not exist.", hidden=True)
@ -110,27 +100,27 @@ class PurgeCog(commands.Cog):
autopurge.delete()
await ctx.send(f"Autopurge removed from {channel.mention}.")
@cog_ext.cog_subcommand(
base="autopurge",
name="update",
description="Update autopurge on a channel",
options=[
create_option(
name="channel",
description="Channel to update",
option_type=7,
required=True,
),
create_option(
name="delay",
description="New time to save",
option_type=4,
required=True,
),
],
@slash_command(
name="autopurge",
sub_cmd_name="update",
sub_cmd_description="Update autopurge on a channel",
)
@admin_or_permissions(manage_messages=True)
async def _autopurge_update(self, ctx: SlashContext, channel: TextChannel, delay: int) -> None:
@slash_option(
name="channel",
description="Channel to update",
option_type=OptionTypes.CHANNEL,
required=True,
)
@slash_option(
name="delay",
description="New time to save",
option_type=OptionTypes.INTEGER,
required=True,
)
@admin_or_permissions(Permissions.MANAGE_MESSAGES)
async def _autopurge_update(
self, ctx: InteractionContext, channel: TextChannel, delay: int
) -> None:
autopurge = Autopurge.objects(guild=ctx.guild.id, channel=channel.id)
if not autopurge:
await ctx.send("Autopurge does not exist.", hidden=True)