Migrate starboard, closes #103

This commit is contained in:
Zeva Rose 2022-02-03 19:48:56 -07:00
parent 086af84cdc
commit e1917e870e

View file

@ -1,16 +1,15 @@
"""J.A.R.V.I.S. Starboard Cog.""" """J.A.R.V.I.S. Starboard Cog."""
from discord import TextChannel from dis_snek import InteractionContext, Permissions, Scale, Snake
from discord.ext import commands from dis_snek.client.utils import find
from discord.utils import find from dis_snek.models.discord.channel import GuildText
from discord_slash import SlashContext, cog_ext from dis_snek.models.discord.components import ActionRow, Select, SelectOption
from discord_slash.context import MenuContext from dis_snek.models.discord.message import Message
from discord_slash.model import ContextMenuType, SlashMessage from dis_snek.models.snek.application_commands import (
from discord_slash.utils.manage_commands import create_option CommandTypes,
from discord_slash.utils.manage_components import ( OptionTypes,
create_actionrow, context_menu,
create_select, slash_command,
create_select_option, slash_option,
wait_for_component,
) )
from jarvis.db.models import Star, Starboard from jarvis.db.models import Star, Starboard
@ -26,19 +25,15 @@ supported_images = [
] ]
class StarboardCog(commands.Cog): class StarboardCog(Scale):
"""J.A.R.V.I.S. Starboard Cog.""" """J.A.R.V.I.S. Starboard Cog."""
def __init__(self, bot: commands.Bot): def __init__(self, bot: Snake):
self.bot = bot self.bot = bot
@cog_ext.cog_subcommand( @slash_command(name="starboard", sub_cmd_name="list", sub_cmd_description="List all starboards")
base="starboard", @admin_or_permissions(Permissions.MANAGE_GUILD)
name="list", async def _list(self, ctx: InteractionContext) -> None:
description="Lists all Starboards",
)
@admin_or_permissions(manage_guild=True)
async def _list(self, ctx: SlashContext) -> None:
starboards = Starboard.objects(guild=ctx.guild.id) starboards = Starboard.objects(guild=ctx.guild.id)
if starboards != []: if starboards != []:
message = "Available Starboards:\n" message = "Available Starboards:\n"
@ -48,29 +43,25 @@ class StarboardCog(commands.Cog):
else: else:
await ctx.send("No Starboards available.") await ctx.send("No Starboards available.")
@cog_ext.cog_subcommand( @slash_command(
base="starboard", name="starboard", sub_cmd_name="create", sub_cmd_description="Create a starboard"
name="create",
description="Create a starboard",
options=[
create_option(
name="channel",
description="Starboard channel",
option_type=7,
required=True,
),
],
) )
@admin_or_permissions(manage_guild=True) @slash_option(
async def _create(self, ctx: SlashContext, channel: TextChannel) -> None: name="channel",
description="Starboard channel",
option_type=OptionTypes.CHANNEL,
required=True,
)
@admin_or_permissions(Permissions.MANAGE_GUILD)
async def _create(self, ctx: InteractionContext, channel: GuildText) -> None:
if channel not in ctx.guild.channels: if channel not in ctx.guild.channels:
await ctx.send( await ctx.send(
"Channel not in guild. Choose an existing channel.", "Channel not in guild. Choose an existing channel.",
hidden=True, hidden=True,
) )
return return
if not isinstance(channel, TextChannel): if not isinstance(channel, GuildText):
await ctx.send("Channel must be a TextChannel", hidden=True) await ctx.send("Channel must be a GuildText", hidden=True)
return return
exists = Starboard.objects(channel=channel.id, guild=ctx.guild.id).first() exists = Starboard.objects(channel=channel.id, guild=ctx.guild.id).first()
@ -90,21 +81,17 @@ class StarboardCog(commands.Cog):
).save() ).save()
await ctx.send(f"Starboard created. Check it out at {channel.mention}.") await ctx.send(f"Starboard created. Check it out at {channel.mention}.")
@cog_ext.cog_subcommand( @slash_command(
base="starboard", name="starboard", sub_cmd_name="delete", sub_cmd_description="Delete a starboard"
name="delete",
description="Delete a starboard",
options=[
create_option(
name="channel",
description="Starboard channel",
option_type=7,
required=True,
),
],
) )
@admin_or_permissions(manage_guild=True) @slash_option(
async def _delete(self, ctx: SlashContext, channel: TextChannel) -> None: name="channel",
description="Starboard channel",
option_type=OptionTypes.CHANNEL,
required=True,
)
@admin_or_permissions(Permissions.MANAGE_GUILD)
async def _delete(self, ctx: InteractionContext, channel: GuildText) -> None:
deleted = Starboard.objects(channel=channel.id, guild=ctx.guild.id).delete() deleted = Starboard.objects(channel=channel.id, guild=ctx.guild.id).delete()
if deleted: if deleted:
_ = Star.objects(starboard=channel.id).delete() _ = Star.objects(starboard=channel.id).delete()
@ -112,37 +99,26 @@ class StarboardCog(commands.Cog):
else: else:
await ctx.send(f"Starboard not found in {channel.mention}.", hidden=True) await ctx.send(f"Starboard not found in {channel.mention}.", hidden=True)
@cog_ext.cog_context_menu(name="Star Message", target=ContextMenuType.MESSAGE) @context_menu(name="Star Message", target=CommandTypes.MESSAGE)
async def _star_message(self, ctx: MenuContext) -> None: async def _star_message(self, ctx: InteractionContext) -> None:
await self._star_add.invoke(ctx, ctx.target_message) await self._star_add.invoke(ctx, ctx.target_message)
@cog_ext.cog_subcommand( @slash_command(name="star", sub_cmd_name="add", description="Star a message")
base="star", @slash_option(
name="add", name="message", description="Message to star", option_type=OptionTypes.STRING, required=True
description="Star a message",
options=[
create_option(
name="message",
description="Message to star",
option_type=3,
required=True,
),
create_option(
name="channel",
description=(
"Channel that has the message, " "required if different than command message"
),
option_type=7,
required=False,
),
],
) )
@admin_or_permissions(manage_guild=True) @slash_option(
name="channel",
description="Channel that has the message, not required if used in same channel",
option_type=OptionTypes.CHANNEL,
required=False,
)
@admin_or_permissions(Permissions.MANAGE_GUILD)
async def _star_add( async def _star_add(
self, self,
ctx: SlashContext, ctx: InteractionContext,
message: str, message: str,
channel: TextChannel = None, channel: GuildText = None,
) -> None: ) -> None:
if not channel: if not channel:
channel = ctx.channel channel = ctx.channel
@ -152,26 +128,35 @@ class StarboardCog(commands.Cog):
return return
await ctx.defer() await ctx.defer()
if not isinstance(message, Message):
if message.startswith("https://"):
message = message.split("/")[-1]
message = await channel.get_message(int(message))
if not message:
await ctx.send("Message not found", hidden=True)
return
channel_list = [] channel_list = []
for starboard in starboards: for starboard in starboards:
channel_list.append(find(lambda x: x.id == starboard.channel, ctx.guild.channels)) channel_list.append(find(lambda x: x.id == starboard.channel, ctx.guild.channels))
select_channels = [ select_channels = [
create_select_option(label=x.name, value=str(idx)) for idx, x in enumerate(channel_list) SelectOption(label=x.name, value=str(idx)) for idx, x in enumerate(channel_list)
] ]
select = create_select( select = Select(
options=select_channels, options=select_channels,
min_values=1, min_values=1,
max_values=1, max_values=1,
) )
components = [create_actionrow(select)] components = [ActionRow(select)]
msg = await ctx.send(content="Choose a starboard", components=components) msg = await ctx.send(content="Choose a starboard", components=components)
com_ctx = await wait_for_component( com_ctx = await self.bot.wait_for_component(
self.bot,
messages=msg, messages=msg,
components=components, components=components,
check=lambda x: x.author.id == ctx.author.id, check=lambda x: x.author.id == ctx.author.id,
@ -179,11 +164,6 @@ class StarboardCog(commands.Cog):
starboard = channel_list[int(com_ctx.selected_options[0])] starboard = channel_list[int(com_ctx.selected_options[0])]
if not isinstance(message, SlashMessage):
if message.startswith("https://"):
message = message.split("/")[-1]
message = await channel.fetch_message(message)
exists = Star.objects( exists = Star.objects(
message=message.id, message=message.id,
channel=message.channel.id, channel=message.channel.id,
@ -219,7 +199,7 @@ class StarboardCog(commands.Cog):
timestamp=message.created_at, timestamp=message.created_at,
) )
embed.set_author( embed.set_author(
name=message.author.name, name=message.author.display_name,
url=message.jump_url, url=message.jump_url,
icon_url=message.author.display_avatar, icon_url=message.author.display_avatar,
) )
@ -247,34 +227,25 @@ class StarboardCog(commands.Cog):
components=components, components=components,
) )
@cog_ext.cog_subcommand( @slash_command(name="star", sub_cmd_name="delete", description="Delete a starred message")
base="star", @slash_option(
name="delete", name="message", description="Star to delete", option_type=OptionTypes.INTEGER, required=True
description="Delete a starred message",
options=[
create_option(
name="id",
description="Star to delete",
option_type=4,
required=True,
),
create_option(
name="starboard",
description="Starboard to delete star from",
option_type=7,
required=True,
),
],
) )
@admin_or_permissions(manage_guild=True) @slash_option(
name="starboard",
description="Starboard to delete star from",
option_type=OptionTypes.CHANNEL,
required=True,
)
@admin_or_permissions(Permissions.MANAGE_GUILD)
async def _star_delete( async def _star_delete(
self, self,
ctx: SlashContext, ctx: InteractionContext,
id: int, id: int,
starboard: TextChannel, starboard: GuildText,
) -> None: ) -> None:
if not isinstance(starboard, TextChannel): if not isinstance(starboard, GuildText):
await ctx.send("Channel must be a TextChannel", hidden=True) await ctx.send("Channel must be a GuildText channel", hidden=True)
return return
exists = Starboard.objects(channel=starboard.id, guild=ctx.guild.id).first() exists = Starboard.objects(channel=starboard.id, guild=ctx.guild.id).first()
if not exists: if not exists:
@ -304,7 +275,7 @@ class StarboardCog(commands.Cog):
await ctx.send(f"Star {id} deleted") await ctx.send(f"Star {id} deleted")
def setup(bot: commands.Bot) -> None: def setup(bot: Snake) -> None:
"""Add StarboardCog to J.A.R.V.I.S.""" """Add StarboardCog to J.A.R.V.I.S."""
bot.add_cog(StarboardCog(bot)) bot.add_cog(StarboardCog(bot))
bot.add_cog(StarboardCog(bot)) bot.add_cog(StarboardCog(bot))