Update current functions to give data relevant to only the registered region

This commit is contained in:
Emily Doherty 2024-12-07 16:21:52 -08:00
parent 895e50bdd3
commit c064c6bade
4 changed files with 48 additions and 20 deletions

View File

@ -1,14 +1,14 @@
import logging import logging
import aiohttp import aiohttp
from interactions import Extension from interactions import Extension, is_owner, check
from interactions import slash_command, listen from interactions import slash_command, listen
from interactions.api.events import Startup from interactions.api.events import Startup
from token_bot.token_database import database as pdb from token_bot.token_database import database as pdb
from token_bot.token_database import database as tdb from token_bot.token_database import database as tdb
VERSION = "0.1.0" VERSION = "0.9.0"
class Core(Extension): class Core(Extension):
@ -23,25 +23,10 @@ class Core(Extension):
self._tdb = tdb.Database(aiohttp.ClientSession()) self._tdb = tdb.Database(aiohttp.ClientSession())
@slash_command() @slash_command()
@check(is_owner())
async def version(self, ctx): async def version(self, ctx):
await ctx.send(f"This is bot version {VERSION}", ephemeral=True) await ctx.send(f"This is bot version {VERSION}", ephemeral=True)
@slash_command() @slash_command()
async def help(self, ctx): async def help(self, ctx):
await ctx.send(f"This is bot help command", ephemeral=True) 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)

View File

@ -30,6 +30,10 @@ class History:
def last_price_datum(self) -> Tuple[datetime.datetime, int] | None: def last_price_datum(self) -> Tuple[datetime.datetime, int] | None:
return self._latest_price_datum return self._latest_price_datum
@property
def last_price_movement(self) -> int:
return self._last_price_movement
@property @property
def history(self) -> List[Tuple[datetime.datetime, int]]: def history(self) -> List[Tuple[datetime.datetime, int]]:
return self._history return self._history

View File

@ -62,5 +62,5 @@ class HistoryManager:
return [] return []
async def get_history(self, flavor, region) -> History: def get_history(self, flavor, region) -> History:
return self._history[flavor][region] return self._history[flavor][region]

View File

@ -1,4 +1,6 @@
import asyncio
import copy import copy
import datetime
import logging import logging
from typing import Type, Dict, List from typing import Type, Dict, List
@ -113,6 +115,7 @@ class Tracker(Extension):
menu = copy.deepcopy(REGION_MENU) menu = copy.deepcopy(REGION_MENU)
await ctx.send(text, components=menu, ephemeral=True) await ctx.send(text, components=menu, ephemeral=True)
@slash_command( @slash_command(
name="remove-registration", name="remove-registration",
description="Remove all alerts and registration from TokenBot" 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) await ctx.send("All alert subscriptions and user registration deleted", ephemeral=True)
@slash_command( @slash_command(
name="exists", name="exists",
description="Check if you are registered with TokenBot" description="Check if you are registered with TokenBot"
@ -134,6 +138,23 @@ class Tracker(Extension):
async def exists(self, ctx: SlashContext): async def exists(self, ctx: SlashContext):
await ctx.send(str(await self._users.exists(ctx.user.id)), ephemeral=True) 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( @slash_command(
name="add-alert", name="add-alert",
description="List all alerts you have signed up for" description="List all alerts you have signed up for"
@ -259,6 +280,19 @@ class Tracker(Extension):
# Helper Functions # # 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): async def remove_alert_select_menu(self, ctx: SlashContext, user: User):
alerts_by_flavor = await gather_alerts_by_flavor(user.subscribed_alerts) alerts_by_flavor = await gather_alerts_by_flavor(user.subscribed_alerts)
select_options: List[StringSelectOption] = [] select_options: List[StringSelectOption] = []
@ -290,6 +324,7 @@ class Tracker(Extension):
alert_type = AlertType.from_str(' '.join(selection_split[1:])) alert_type = AlertType.from_str(' '.join(selection_split[1:]))
return Alert(alert_type, flavor, user.region) return Alert(alert_type, flavor, user.region)
async def flavor_select_menu(self, ctx: SlashContext) -> Type[Flavor]: async def flavor_select_menu(self, ctx: SlashContext) -> Type[Flavor]:
flavor_menu = copy.deepcopy(FLAVOR_MENU) flavor_menu = copy.deepcopy(FLAVOR_MENU)
@ -310,6 +345,7 @@ class Tracker(Extension):
await flavor_message.edit(context=ctx, components=flavor_menu) await flavor_message.edit(context=ctx, components=flavor_menu)
return flavor return flavor
async def alert_category_select_menu(self, ctx: SlashContext) -> AlertCategory: async def alert_category_select_menu(self, ctx: SlashContext) -> AlertCategory:
alert_type_button = copy.deepcopy(ALERT_TYPE_ROW) alert_type_button = copy.deepcopy(ALERT_TYPE_ROW)
alert_type_message = await ctx.send( alert_type_message = await ctx.send(
@ -345,6 +381,7 @@ class Tracker(Extension):
await message.edit(context=ctx, components=menu) await message.edit(context=ctx, components=menu)
return AlertType.from_str(component.ctx.values[0]) return AlertType.from_str(component.ctx.values[0])
async def high_alert_select_menu(self, ctx: SlashContext) -> AlertType: async def high_alert_select_menu(self, ctx: SlashContext) -> AlertType:
high_menu = copy.deepcopy(HIGH_ALERT_MENU) high_menu = copy.deepcopy(HIGH_ALERT_MENU)
high_message = await ctx.send( high_message = await ctx.send(
@ -353,6 +390,7 @@ class Tracker(Extension):
ephemeral=True) ephemeral=True)
return await self._alert_select_menu_handler(ctx, high_menu, high_message) return await self._alert_select_menu_handler(ctx, high_menu, high_message)
async def low_alert_select_menu(self, ctx: SlashContext) -> AlertType: async def low_alert_select_menu(self, ctx: SlashContext) -> AlertType:
low_menu = copy.deepcopy(LOW_ALERT_MENU) low_menu = copy.deepcopy(LOW_ALERT_MENU)
low_message = await ctx.send( low_message = await ctx.send(
@ -361,12 +399,13 @@ class Tracker(Extension):
ephemeral=True) ephemeral=True)
return await self._alert_select_menu_handler(ctx, low_menu, low_message) 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: async def render_alert_flavor(self, alerts: List[Alert], user: User | None = None) -> Embed:
region = alerts[0].region region = alerts[0].region
flavor = alerts[0].flavor flavor = alerts[0].flavor
fields: List[EmbedField] = [] fields: List[EmbedField] = []
for alert in alerts: 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) trigger = await history.find_update_trigger_from_alert(alert)
if trigger.last_trigger is not None: if trigger.last_trigger is not None:
alert_str = (f"Last Alerting Price Value: {format(trigger.last_alerting[1], ",")}\n" alert_str = (f"Last Alerting Price Value: {format(trigger.last_alerting[1], ",")}\n"