From c064c6bade3175b289d4d73f012b7c593253955e Mon Sep 17 00:00:00 2001 From: Emily Doherty Date: Sat, 7 Dec 2024 16:21:52 -0800 Subject: [PATCH] Update current functions to give data relevant to only the registered region --- token_bot/core.py | 21 ++-------- token_bot/history_manager/history.py | 4 ++ token_bot/history_manager/history_manager.py | 2 +- token_bot/tracker.py | 41 +++++++++++++++++++- 4 files changed, 48 insertions(+), 20 deletions(-) diff --git a/token_bot/core.py b/token_bot/core.py index e527aab..7935e1c 100644 --- a/token_bot/core.py +++ b/token_bot/core.py @@ -1,14 +1,14 @@ import logging import aiohttp -from interactions import Extension +from interactions import Extension, is_owner, check from interactions import slash_command, listen from interactions.api.events import Startup from token_bot.token_database import database as pdb from token_bot.token_database import database as tdb -VERSION = "0.1.0" +VERSION = "0.9.0" class Core(Extension): @@ -23,25 +23,10 @@ class Core(Extension): self._tdb = tdb.Database(aiohttp.ClientSession()) @slash_command() + @check(is_owner()) async def version(self, ctx): await ctx.send(f"This is bot version {VERSION}", ephemeral=True) @slash_command() async def help(self, ctx): await ctx.send(f"This is bot help command", ephemeral=True) - - @slash_command( - description="The current retail token cost" - ) - async def current(self, ctx): - current = await self._tdb.current(tdb.Flavor.RETAIL) - await ctx.send(f"us: {current['us']}\neu: {current['eu']}\ntw: {current['tw']}\nkr: {current['kr']}", - ephemeral=True) - - @slash_command( - description="The current classic token cost" - ) - async def current_classic(self, ctx): - current = await self._tdb.current(tdb.Flavor.CLASSIC) - await ctx.send(f"us: {current['us']}\neu: {current['eu']}\ntw: {current['tw']}\nkr: {current['kr']}", - ephemeral=True) diff --git a/token_bot/history_manager/history.py b/token_bot/history_manager/history.py index eaa77b0..8769ece 100644 --- a/token_bot/history_manager/history.py +++ b/token_bot/history_manager/history.py @@ -30,6 +30,10 @@ class History: def last_price_datum(self) -> Tuple[datetime.datetime, int] | None: return self._latest_price_datum + @property + def last_price_movement(self) -> int: + return self._last_price_movement + @property def history(self) -> List[Tuple[datetime.datetime, int]]: return self._history diff --git a/token_bot/history_manager/history_manager.py b/token_bot/history_manager/history_manager.py index 7607555..9501ae6 100644 --- a/token_bot/history_manager/history_manager.py +++ b/token_bot/history_manager/history_manager.py @@ -62,5 +62,5 @@ class HistoryManager: return [] - async def get_history(self, flavor, region) -> History: + def get_history(self, flavor, region) -> History: return self._history[flavor][region] diff --git a/token_bot/tracker.py b/token_bot/tracker.py index 04fb770..5eb3857 100644 --- a/token_bot/tracker.py +++ b/token_bot/tracker.py @@ -1,4 +1,6 @@ +import asyncio import copy +import datetime import logging from typing import Type, Dict, List @@ -113,6 +115,7 @@ class Tracker(Extension): menu = copy.deepcopy(REGION_MENU) await ctx.send(text, components=menu, ephemeral=True) + @slash_command( name="remove-registration", description="Remove all alerts and registration from TokenBot" @@ -126,6 +129,7 @@ class Tracker(Extension): await ctx.send("All alert subscriptions and user registration deleted", ephemeral=True) + @slash_command( name="exists", description="Check if you are registered with TokenBot" @@ -134,6 +138,23 @@ class Tracker(Extension): async def exists(self, ctx: SlashContext): await ctx.send(str(await self._users.exists(ctx.user.id)), ephemeral=True) + + @slash_command( + description="The current retail token cost" + ) + async def current(self, ctx: SlashContext): + current_str = await self.get_current_token(ctx, tdb.Flavor.RETAIL) + await ctx.send(current_str, ephemeral=True) + + + @slash_command( + description="The current classic token cost" + ) + async def current_classic(self, ctx: SlashContext): + current_str = await self.get_current_token(ctx, tdb.Flavor.CLASSIC) + await ctx.send(current_str, ephemeral=True) + + @slash_command( name="add-alert", description="List all alerts you have signed up for" @@ -259,6 +280,19 @@ class Tracker(Extension): # Helper Functions # ################################### + async def get_current_token(self, ctx: SlashContext, flavor: Flavor) -> str: + user: User = await self._users.get(ctx.user.id) + region = user.region.name + region_history = self._history_manager.get_history(flavor, user.region) + price_movement_str = format(region_history.last_price_movement, ',') + if region_history.last_price_movement > 0: + price_movement_str = f"+{price_movement_str}" + + return (f"Last Price Value for {region}: {format(region_history.last_price_datum[1], ",")}\n" + f"Last Update Time: {region_history.last_price_datum[0].strftime('%Y-%m-%d %H:%M:%S UTC')}\n" + f"Last Price Movement: {price_movement_str}") + + async def remove_alert_select_menu(self, ctx: SlashContext, user: User): alerts_by_flavor = await gather_alerts_by_flavor(user.subscribed_alerts) select_options: List[StringSelectOption] = [] @@ -290,6 +324,7 @@ class Tracker(Extension): alert_type = AlertType.from_str(' '.join(selection_split[1:])) return Alert(alert_type, flavor, user.region) + async def flavor_select_menu(self, ctx: SlashContext) -> Type[Flavor]: flavor_menu = copy.deepcopy(FLAVOR_MENU) @@ -310,6 +345,7 @@ class Tracker(Extension): await flavor_message.edit(context=ctx, components=flavor_menu) return flavor + async def alert_category_select_menu(self, ctx: SlashContext) -> AlertCategory: alert_type_button = copy.deepcopy(ALERT_TYPE_ROW) alert_type_message = await ctx.send( @@ -345,6 +381,7 @@ class Tracker(Extension): await message.edit(context=ctx, components=menu) return AlertType.from_str(component.ctx.values[0]) + async def high_alert_select_menu(self, ctx: SlashContext) -> AlertType: high_menu = copy.deepcopy(HIGH_ALERT_MENU) high_message = await ctx.send( @@ -353,6 +390,7 @@ class Tracker(Extension): ephemeral=True) return await self._alert_select_menu_handler(ctx, high_menu, high_message) + async def low_alert_select_menu(self, ctx: SlashContext) -> AlertType: low_menu = copy.deepcopy(LOW_ALERT_MENU) low_message = await ctx.send( @@ -361,12 +399,13 @@ class Tracker(Extension): ephemeral=True) return await self._alert_select_menu_handler(ctx, low_menu, low_message) + async def render_alert_flavor(self, alerts: List[Alert], user: User | None = None) -> Embed: region = alerts[0].region flavor = alerts[0].flavor fields: List[EmbedField] = [] for alert in alerts: - history = await self._history_manager.get_history(alert.flavor, alert.region) + history = self._history_manager.get_history(alert.flavor, alert.region) trigger = await history.find_update_trigger_from_alert(alert) if trigger.last_trigger is not None: alert_str = (f"Last Alerting Price Value: {format(trigger.last_alerting[1], ",")}\n"