Skip to content

Commit

Permalink
fix: price alert update embed
Browse files Browse the repository at this point in the history
  • Loading branch information
cathaypacific8747 committed May 11, 2024
1 parent 5d5c9df commit 12d62a2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
"python.analysis.extraPaths": [
"./docs/assets/scripts"
],
"editor.formatOnSave": true
}
31 changes: 16 additions & 15 deletions src/am4/bot/cogs/price.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import time
from typing import Literal

import cmocean
import discord
from discord.ext import commands

Expand All @@ -19,7 +18,6 @@
class PriceCog(BaseCog):
def __init__(self, bot: commands.Bot):
super().__init__(bot)
self.cmap = cmocean.tools.crop_by_percent(cmocean.cm.dense_r, 50, which="min")

@commands.command(
brief=f"Reports a price to <#{cfg.bot.PRICEALERT_CHANNELID}>",
Expand Down Expand Up @@ -69,31 +67,35 @@ async def price(
mul, vmin, vmax = bounds[k]
s = 1 - (v - vmin) / (vmax - vmin)
score += max(min(s, 1), 0) * mul
embed = discord.Embed(
title=f"{prices_f}",
description=f"Sent by {ctx.author.mention}, expiry: {t_expiry_f}",
colour=discord.Colour.from_rgb(*[int(x * 255) for x in self.cmap(score)[:3]]),
)
contact_f = f"If there was a mistake, contact a <@&{cfg.bot.HELPER_ROLEID}> or <@&{cfg.bot.MODERATOR_ROLEID}>."

async def send_update_success(jump_url: str, is_edit: bool = False):
desc = f"{jump_url}\n> {prices_f}."
if not is_edit:
desc += (
f"\n\nIf there was a mistake, send the `{cfg.bot.COMMAND_PREFIX}price` command again"
f"**within 60 seconds** to update it. Otherwise, contact one of our <@&{cfg.bot.HELPER_ROLEID}>"
f"or <@&{cfg.bot.MODERATOR_ROLEID}>."
)
await ctx.send(
embed=discord.Embed(
title=f"Price alert {'updated' if is_edit else 'sent'}!",
description=f"{jump_url}\n> {prices_f}." + ("" if is_edit else f"\n\n{contact_f}"),
description=desc,
colour=COLOUR_SUCCESS,
)
)

# if the the price is already sent, error if the user is not a mod, otherwise edit it
async for msg in channels.price_alert.history(limit=1):
if msg.created_at.timestamp() > t_start:
if (t_now := msg.created_at.timestamp()) > t_start:
for r in ctx.author.roles:
if r.id == cfg.bot.MODERATOR_ROLEID or r.id == cfg.bot.HELPER_ROLEID:
embed.description = msg.embeds[0].description.replace(
", expiry", f", updated by {ctx.author.mention}, expiry"
if r.id in [cfg.bot.MODERATOR_ROLEID, cfg.bot.HELPER_ROLEID] or t_now - t_start < 60:
old_senders = msg.content.split(". (Sent by ")[0].split(") ")[1]
await msg.edit(
content=(
f"{prices_f}. ({old_senders}, Updated by {ctx.author.mention}) "
f"<@&{cfg.bot.PRICEALERT_ROLEID}>"
)
)
await msg.edit(content=None, embed=embed)
await send_update_success(msg.jump_url)
return
embed = discord.Embed(
Expand All @@ -111,7 +113,6 @@ async def send_update_success(jump_url: str, is_edit: bool = False):
msg = await channels.price_alert.send(
content=f"{prices_f}. (Sent by {ctx.author.mention}) <@&{cfg.bot.PRICEALERT_ROLEID}>"
)
# await msg.edit(content=None, embed=embed)
await send_update_success(msg.jump_url)

@price.error
Expand Down

0 comments on commit 12d62a2

Please sign in to comment.