Fix add star. TODO: make sure starboard works

This commit is contained in:
Zeva Rose 2022-03-18 19:27:04 -06:00
parent 51c3298b86
commit bf991aa600

View file

@ -36,7 +36,7 @@ class StarboardCog(Scale):
@slash_command(name="starboard", sub_cmd_name="list", sub_cmd_description="List all starboards") @slash_command(name="starboard", sub_cmd_name="list", sub_cmd_description="List all starboards")
@check(admin_or_permissions(Permissions.MANAGE_GUILD)) @check(admin_or_permissions(Permissions.MANAGE_GUILD))
async def _list(self, ctx: InteractionContext) -> None: async def _list(self, ctx: InteractionContext) -> None:
starboards = Starboard.objects(guild=ctx.guild.id) starboards = await Starboard.find(q(guild=ctx.guild.id)).to_list(None)
if starboards != []: if starboards != []:
message = "Available Starboards:\n" message = "Available Starboards:\n"
for s in starboards: for s in starboards:
@ -71,16 +71,16 @@ class StarboardCog(Scale):
await ctx.send(f"Starboard already exists at {channel.mention}.", ephemeral=True) await ctx.send(f"Starboard already exists at {channel.mention}.", ephemeral=True)
return return
count = Starboard.objects(guild=ctx.guild.id).count() count = await Starboard.count_documents(q(guild=ctx.guild.id))
if count >= 25: if count >= 25:
await ctx.send("25 starboard limit reached", ephemeral=True) await ctx.send("25 starboard limit reached", ephemeral=True)
return return
_ = Starboard( await Starboard(
guild=ctx.guild.id, guild=ctx.guild.id,
channel=channel.id, channel=channel.id,
admin=ctx.author.id, admin=ctx.author.id,
).save() ).commit()
await ctx.send(f"Starboard created. Check it out at {channel.mention}.") await ctx.send(f"Starboard created. Check it out at {channel.mention}.")
@slash_command( @slash_command(
@ -94,29 +94,13 @@ class StarboardCog(Scale):
) )
@check(admin_or_permissions(Permissions.MANAGE_GUILD)) @check(admin_or_permissions(Permissions.MANAGE_GUILD))
async def _delete(self, ctx: InteractionContext, channel: GuildText) -> None: async def _delete(self, ctx: InteractionContext, channel: GuildText) -> None:
deleted = Starboard.objects(channel=channel.id, guild=ctx.guild.id).delete() found = await Starboard.find_one(q(channel=channel.id, guild=ctx.guild.id))
if deleted: if found:
_ = Star.objects(starboard=channel.id).delete() await found.delete()
await ctx.send(f"Starboard deleted from {channel.mention}.") await ctx.send(f"Starboard deleted from {channel.mention}.")
else: else:
await ctx.send(f"Starboard not found in {channel.mention}.", ephemeral=True) await ctx.send(f"Starboard not found in {channel.mention}.", ephemeral=True)
@context_menu(name="Star Message", context_type=CommandTypes.MESSAGE)
async def _star_message(self, ctx: InteractionContext) -> None:
await self._star_add._can_run(ctx)
await self._star_add.callback(ctx, message=str(ctx.target_id))
@slash_command(name="star", sub_cmd_name="add", description="Star a message")
@slash_option(
name="message", description="Message to star", opt_type=OptionTypes.STRING, required=True
)
@slash_option(
name="channel",
description="Channel that has the message, not required if used in same channel",
opt_type=OptionTypes.CHANNEL,
required=False,
)
@check(admin_or_permissions(Permissions.MANAGE_GUILD))
async def _star_add( async def _star_add(
self, self,
ctx: InteractionContext, ctx: InteractionContext,
@ -125,7 +109,7 @@ class StarboardCog(Scale):
) -> None: ) -> None:
if not channel: if not channel:
channel = ctx.channel channel = ctx.channel
starboards = Starboard.objects(guild=ctx.guild.id) starboards = await Starboard.find(q(guild=ctx.guild.id)).to_list(None)
if not starboards: if not starboards:
await ctx.send("No starboards exist.", ephemeral=True) await ctx.send("No starboards exist.", ephemeral=True)
return return
@ -135,7 +119,7 @@ class StarboardCog(Scale):
if not isinstance(message, Message): if not isinstance(message, Message):
if message.startswith("https://"): if message.startswith("https://"):
message = message.split("/")[-1] message = message.split("/")[-1]
message = await channel.get_message(int(message)) message = await channel.fetch_message(int(message))
if not message: if not message:
await ctx.send("Message not found", ephemeral=True) await ctx.send("Message not found", ephemeral=True)
@ -167,12 +151,14 @@ class StarboardCog(Scale):
starboard = channel_list[int(com_ctx.context.values[0])] starboard = channel_list[int(com_ctx.context.values[0])]
exists = Star.objects( exists = await Star.find_one(
q(
message=message.id, message=message.id,
channel=message.channel.id, channel=channel.id,
guild=message.guild.id, guild=ctx.guild.id,
starboard=starboard.id, starboard=starboard.id,
).first() )
)
if exists: if exists:
await ctx.send( await ctx.send(
@ -181,7 +167,7 @@ class StarboardCog(Scale):
) )
return return
count = Star.objects(guild=message.guild.id, starboard=starboard.id).count() count = await Star.count_documents(q(guild=ctx.guild.id, starboard=starboard.id))
content = message.content content = message.content
attachments = message.attachments attachments = message.attachments
@ -204,24 +190,24 @@ class StarboardCog(Scale):
embed.set_author( embed.set_author(
name=message.author.display_name, name=message.author.display_name,
url=message.jump_url, url=message.jump_url,
icon_url=message.author.display_avatar.url, icon_url=message.author.avatar.url,
) )
embed.set_footer(text=message.guild.name + " | " + message.channel.name) embed.set_footer(text=ctx.guild.name + " | " + channel.name)
if image_url: if image_url:
embed.set_image(url=image_url) embed.set_image(url=image_url)
star = await starboard.send(embed=embed) star = await starboard.send(embed=embed)
_ = Star( await Star(
index=count, index=count,
message=message.id, message=message.id,
channel=message.channel.id, channel=channel.id,
guild=message.guild.id, guild=ctx.guild.id,
starboard=starboard.id, starboard=starboard.id,
admin=ctx.author.id, admin=ctx.author.id,
star=star.id, star=star.id,
active=True, active=True,
).save() ).commit()
components[0].components[0].disabled = True components[0].components[0].disabled = True
@ -230,6 +216,27 @@ class StarboardCog(Scale):
components=components, components=components,
) )
@context_menu(name="Star Message", context_type=CommandTypes.MESSAGE)
@check(admin_or_permissions(Permissions.MANAGE_GUILD))
async def _star_message(self, ctx: InteractionContext) -> None:
await self._star_add(ctx, message=str(ctx.target_id))
@slash_command(name="star", sub_cmd_name="add", description="Star a message")
@slash_option(
name="message", description="Message to star", opt_type=OptionTypes.STRING, required=True
)
@slash_option(
name="channel",
description="Channel that has the message, not required if used in same channel",
opt_type=OptionTypes.CHANNEL,
required=False,
)
@check(admin_or_permissions(Permissions.MANAGE_GUILD))
async def _star_message_slash(
self, ctx: InteractionContext, message: str, channel: GuildText
) -> None:
await self._star_add(ctx, message, channel)
@slash_command(name="star", sub_cmd_name="delete", description="Delete a starred message") @slash_command(name="star", sub_cmd_name="delete", description="Delete a starred message")
@slash_option( @slash_option(
name="id", description="Star ID to delete", opt_type=OptionTypes.INTEGER, required=True name="id", description="Star ID to delete", opt_type=OptionTypes.INTEGER, required=True
@ -250,30 +257,33 @@ class StarboardCog(Scale):
if not isinstance(starboard, GuildText): if not isinstance(starboard, GuildText):
await ctx.send("Channel must be a GuildText channel", ephemeral=True) await ctx.send("Channel must be a GuildText channel", ephemeral=True)
return return
exists = await Starboard.find_one(q(channel=starboard.id, guild=ctx.guild.id)) exists = await Starboard.find_one(q(channel=starboard.id, guild=ctx.guild.id))
if not exists: if not exists:
# TODO: automagically create starboard
await ctx.send( await ctx.send(
f"Starboard does not exist in {starboard.mention}. Please create it first", f"Starboard does not exist in {starboard.mention}. Please create it first",
ephemeral=True, ephemeral=True,
) )
return return
star = Star.objects( star = await Star.find_one(
q(
starboard=starboard.id, starboard=starboard.id,
index=id, index=id,
guild=ctx.guild.id, guild=ctx.guild.id,
active=True, active=True,
).first() )
)
if not star: if not star:
await ctx.send(f"No star exists with id {id}", ephemeral=True) await ctx.send(f"No star exists with id {id}", ephemeral=True)
return return
message = await starboard.get_message(star.star) message = await starboard.fetch_message(star.star)
if message: if message:
await message.delete() await message.delete()
star.active = False await star.delete()
star.save()
await ctx.send(f"Star {id} deleted from {starboard.mention}") await ctx.send(f"Star {id} deleted from {starboard.mention}")