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")
@check(admin_or_permissions(Permissions.MANAGE_GUILD))
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 != []:
message = "Available Starboards:\n"
for s in starboards:
@ -71,16 +71,16 @@ class StarboardCog(Scale):
await ctx.send(f"Starboard already exists at {channel.mention}.", ephemeral=True)
return
count = Starboard.objects(guild=ctx.guild.id).count()
count = await Starboard.count_documents(q(guild=ctx.guild.id))
if count >= 25:
await ctx.send("25 starboard limit reached", ephemeral=True)
return
_ = Starboard(
await Starboard(
guild=ctx.guild.id,
channel=channel.id,
admin=ctx.author.id,
).save()
).commit()
await ctx.send(f"Starboard created. Check it out at {channel.mention}.")
@slash_command(
@ -94,29 +94,13 @@ class StarboardCog(Scale):
)
@check(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()
if deleted:
_ = Star.objects(starboard=channel.id).delete()
found = await Starboard.find_one(q(channel=channel.id, guild=ctx.guild.id))
if found:
await found.delete()
await ctx.send(f"Starboard deleted from {channel.mention}.")
else:
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(
self,
ctx: InteractionContext,
@ -125,7 +109,7 @@ class StarboardCog(Scale):
) -> None:
if not 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:
await ctx.send("No starboards exist.", ephemeral=True)
return
@ -135,7 +119,7 @@ class StarboardCog(Scale):
if not isinstance(message, Message):
if message.startswith("https://"):
message = message.split("/")[-1]
message = await channel.get_message(int(message))
message = await channel.fetch_message(int(message))
if not message:
await ctx.send("Message not found", ephemeral=True)
@ -167,12 +151,14 @@ class StarboardCog(Scale):
starboard = channel_list[int(com_ctx.context.values[0])]
exists = Star.objects(
message=message.id,
channel=message.channel.id,
guild=message.guild.id,
starboard=starboard.id,
).first()
exists = await Star.find_one(
q(
message=message.id,
channel=channel.id,
guild=ctx.guild.id,
starboard=starboard.id,
)
)
if exists:
await ctx.send(
@ -181,7 +167,7 @@ class StarboardCog(Scale):
)
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
attachments = message.attachments
@ -204,24 +190,24 @@ class StarboardCog(Scale):
embed.set_author(
name=message.author.display_name,
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:
embed.set_image(url=image_url)
star = await starboard.send(embed=embed)
_ = Star(
await Star(
index=count,
message=message.id,
channel=message.channel.id,
guild=message.guild.id,
channel=channel.id,
guild=ctx.guild.id,
starboard=starboard.id,
admin=ctx.author.id,
star=star.id,
active=True,
).save()
).commit()
components[0].components[0].disabled = True
@ -230,6 +216,27 @@ class StarboardCog(Scale):
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_option(
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):
await ctx.send("Channel must be a GuildText channel", ephemeral=True)
return
exists = await Starboard.find_one(q(channel=starboard.id, guild=ctx.guild.id))
if not exists:
# TODO: automagically create starboard
await ctx.send(
f"Starboard does not exist in {starboard.mention}. Please create it first",
ephemeral=True,
)
return
star = Star.objects(
starboard=starboard.id,
index=id,
guild=ctx.guild.id,
active=True,
).first()
star = await Star.find_one(
q(
starboard=starboard.id,
index=id,
guild=ctx.guild.id,
active=True,
)
)
if not star:
await ctx.send(f"No star exists with id {id}", ephemeral=True)
return
message = await starboard.get_message(star.star)
message = await starboard.fetch_message(star.star)
if message:
await message.delete()
star.active = False
star.save()
await star.delete()
await ctx.send(f"Star {id} deleted from {starboard.mention}")