Support multiple embeds
All checks were successful
Build and Publish Jimmy.2 / build_and_publish (push) Successful in 6s
All checks were successful
Build and Publish Jimmy.2 / build_and_publish (push) Successful in 6s
This commit is contained in:
parent
5e14c2bd90
commit
991b80ff59
1 changed files with 16 additions and 12 deletions
|
@ -18,24 +18,28 @@ class Starboard(commands.Cog):
|
||||||
self.log = logging.getLogger("jimmy.cogs.starboard")
|
self.log = logging.getLogger("jimmy.cogs.starboard")
|
||||||
self.redis = Redis(**CONFIG["redis"])
|
self.redis = Redis(**CONFIG["redis"])
|
||||||
|
|
||||||
async def generate_starboard_embed(self, message: discord.Message) -> tuple[discord.Embed, int]:
|
async def generate_starboard_embed(self, message: discord.Message) -> tuple[list[discord.Embed], int]:
|
||||||
"""
|
"""
|
||||||
Generates an embed ready for a starboard message.
|
Generates an embed ready for a starboard message.
|
||||||
|
|
||||||
:param message: The message to base off of.
|
:param message: The message to base off of.
|
||||||
:return: The created embed
|
:return: The created embed
|
||||||
"""
|
"""
|
||||||
reactions: list[discord.Reaction] = [x for x in message.reactions if x.emoji == self.emoji]
|
reactions: list[discord.Reaction] = [x for x in message.reactions if str(x.emoji) == str(self.emoji)]
|
||||||
downvote_reactions = [x for x in message.reactions if x.emoji == self.DOWNVOTE_EMOJI]
|
downvote_reactions = [x for x in message.reactions if str(x.emoji) == str(self.DOWNVOTE_EMOJI)]
|
||||||
if not reactions:
|
if not reactions:
|
||||||
# Nobody has added the star reaction.
|
# Nobody has added the star reaction.
|
||||||
star_count = 0
|
star_count = 0
|
||||||
|
self.log.debug("There are no star reactions at all on message.")
|
||||||
else:
|
else:
|
||||||
# Count the number of reactions
|
# Count the number of reactions
|
||||||
star_count = sum([x.count for x in reactions])
|
star_count = sum([x.count for x in reactions])
|
||||||
|
self.log.debug("There are a total of %d star reactions on message.", star_count)
|
||||||
|
|
||||||
if downvote_reactions:
|
if downvote_reactions:
|
||||||
star_count -= sum([x.count for x in downvote_reactions])
|
_dv = sum([x.count for x in downvote_reactions])
|
||||||
|
star_count -= _dv
|
||||||
|
self.log.debug("There are %d downvotes on message, resulting in %d stars.", _dv, star_count)
|
||||||
|
|
||||||
if star_count >= 0:
|
if star_count >= 0:
|
||||||
star_emoji_count = (str(self.emoji) * star_count)[:10]
|
star_emoji_count = (str(self.emoji) * star_count)[:10]
|
||||||
|
@ -69,7 +73,7 @@ class Starboard(commands.Cog):
|
||||||
continue
|
continue
|
||||||
if message_embed.description:
|
if message_embed.description:
|
||||||
embed.description = message_embed.description
|
embed.description = message_embed.description
|
||||||
if not message.attachments and not embed.description:
|
if not message.attachments:
|
||||||
raise ValueError("Message does not appear to contain any text, embeds, or attachments.")
|
raise ValueError("Message does not appear to contain any text, embeds, or attachments.")
|
||||||
|
|
||||||
if message.attachments:
|
if message.attachments:
|
||||||
|
@ -97,7 +101,7 @@ class Starboard(commands.Cog):
|
||||||
# This whacky reverse -> perform -> reverse basically just means we can set the first image/*
|
# This whacky reverse -> perform -> reverse basically just means we can set the first image/*
|
||||||
# attachment as the image.
|
# attachment as the image.
|
||||||
|
|
||||||
return embed, star_count
|
return [embed, *filter(lambda e: e.type == "rich", message.embeds)], star_count
|
||||||
|
|
||||||
async def get_or_fetch_message(self, channel_id: int, message_id: int) -> discord.Message:
|
async def get_or_fetch_message(self, channel_id: int, message_id: int) -> discord.Message:
|
||||||
"""
|
"""
|
||||||
|
@ -160,7 +164,7 @@ class Starboard(commands.Cog):
|
||||||
"starboard_channel_id": starboard_channel,
|
"starboard_channel_id": starboard_channel,
|
||||||
"starboard_message_id": None
|
"starboard_message_id": None
|
||||||
}
|
}
|
||||||
if not starboard_channel.can_send(embed):
|
if not starboard_channel.can_send(embed[0]):
|
||||||
self.log.warning(
|
self.log.warning(
|
||||||
"Cannot send starboard messages in %d, %d (#%s @ %s)",
|
"Cannot send starboard messages in %d, %d (#%s @ %s)",
|
||||||
starboard_channel.id,
|
starboard_channel.id,
|
||||||
|
@ -171,7 +175,7 @@ class Starboard(commands.Cog):
|
||||||
return
|
return
|
||||||
|
|
||||||
starboard_message = await starboard_channel.send(
|
starboard_message = await starboard_channel.send(
|
||||||
embed=embed,
|
embeds=embed,
|
||||||
silent=True
|
silent=True
|
||||||
)
|
)
|
||||||
data["starboard_message_id"] = starboard_message.id
|
data["starboard_message_id"] = starboard_message.id
|
||||||
|
@ -186,7 +190,7 @@ class Starboard(commands.Cog):
|
||||||
if star_count <= 0:
|
if star_count <= 0:
|
||||||
return
|
return
|
||||||
starboard_message = await starboard_channel.send(
|
starboard_message = await starboard_channel.send(
|
||||||
embed=embed,
|
embeds=embed,
|
||||||
silent=True
|
silent=True
|
||||||
)
|
)
|
||||||
data["starboard_message_id"] = starboard_message.id
|
data["starboard_message_id"] = starboard_message.id
|
||||||
|
@ -201,8 +205,8 @@ class Starboard(commands.Cog):
|
||||||
starboard_message.channel.name,
|
starboard_message.channel.name,
|
||||||
starboard_message.guild.name
|
starboard_message.guild.name
|
||||||
)
|
)
|
||||||
elif starboard_message.embeds[0] != embed:
|
elif starboard_message.embeds[0] != embed[0]:
|
||||||
await starboard_message.edit(embed=embed)
|
await starboard_message.edit(embeds=embed)
|
||||||
|
|
||||||
@commands.message_command(name="Preview Starboard Message")
|
@commands.message_command(name="Preview Starboard Message")
|
||||||
async def preview_starboard_message(self, ctx: discord.ApplicationContext, message: discord.Message):
|
async def preview_starboard_message(self, ctx: discord.ApplicationContext, message: discord.Message):
|
||||||
|
@ -210,7 +214,7 @@ class Starboard(commands.Cog):
|
||||||
data = await self.redis.get(str(message.id))
|
data = await self.redis.get(str(message.id))
|
||||||
return await ctx.respond(
|
return await ctx.respond(
|
||||||
f"```json\n{json.dumps(data, indent=4)}\n```\nStars: {stars:,}",
|
f"```json\n{json.dumps(data, indent=4)}\n```\nStars: {stars:,}",
|
||||||
embed=embed
|
embeds=embed
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue