Compare commits

..

No commits in common. "aa58f775ab0f0b79e81b92527dffd6203de08e67" and "719842c20e14673729d9168bfdc5b6e1de251821" have entirely different histories.

2 changed files with 41 additions and 70 deletions

View File

@ -8,7 +8,7 @@ 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.9.1" VERSION = "0.9.0"
class Core(Extension): class Core(Extension):

View File

@ -85,13 +85,13 @@ class Tracker(Extension):
description=f"Hello, you requested to be sent an alert when the price of the World of Warcraft " description=f"Hello, you requested to be sent an alert when the price of the World of Warcraft "
f"token reaches a certain value.\n\n" f"token reaches a certain value.\n\n"
f"As a reminder, you can remove an alert via ```/remove-alert```\n" f"As a reminder, you can remove an alert via ```/remove-alert```\n"
f"or you can remove all alerts and user data via ```/remove-registration```\n\n", f"or you can remove all registrations via ```/remove-registration```\n\n",
) )
] ]
alerts_by_flavor = await gather_alerts_by_flavor(users_alerts[user]) alerts_by_flavor = await gather_alerts_by_flavor(users_alerts[user])
for flavor in alerts_by_flavor: for flavor in alerts_by_flavor:
embeds.append( embeds.append(
await self._render_alert_flavor(alerts_by_flavor[flavor], user=user) await self.render_alert_flavor(alerts_by_flavor[flavor], user=user)
) )
await discord_user.send(embeds=embeds) await discord_user.send(embeds=embeds)
@ -118,7 +118,7 @@ class Tracker(Extension):
@slash_command( @slash_command(
name="register", name="register",
description="Register with a new GoblinBot Region for alerts on token price changes.", description="Register with TokenBot for alerts on token price changes.",
) )
async def register(self, ctx: SlashContext): async def register(self, ctx: SlashContext):
text = ( text = (
@ -126,14 +126,14 @@ class Tracker(Extension):
"Please note: \n" "Please note: \n"
"* You can only be registered with one region at a time \n" "* You can only be registered with one region at a time \n"
"* Changing your region will remove all previous alerts you have signed up for \n" "* Changing your region will remove all previous alerts you have signed up for \n"
"* You can remove all alerts and user data using ```/remove-registration```" "* You can remove all alerts and registration using ```/remove-registration```"
) )
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 GoblinBot", description="Remove all alerts and registration from TokenBot",
) )
async def remove_registration(self, ctx: SlashContext): async def remove_registration(self, ctx: SlashContext):
if await self._users.exists(ctx.user.id): if await self._users.exists(ctx.user.id):
@ -142,10 +142,12 @@ class Tracker(Extension):
await self._alerts.remove_user(alert, user) await self._alerts.remove_user(alert, user)
await self._users.delete(ctx.user.id) await self._users.delete(ctx.user.id)
await ctx.send("All alert subscriptions and user data deleted", ephemeral=True) await ctx.send(
"All alert subscriptions and user registration deleted", ephemeral=True
)
@slash_command( @slash_command(
name="exists", description="Check if you are registered with GoblinBot" "" name="exists", description="Check if you are registered with TokenBot"
) )
@check(is_owner()) @check(is_owner())
async def exists(self, ctx: SlashContext): async def exists(self, ctx: SlashContext):
@ -164,11 +166,12 @@ class Tracker(Extension):
@slash_command(name="add-alert", description="Add an alert listener") @slash_command(name="add-alert", description="Add an alert listener")
async def add_alert(self, ctx: SlashContext): async def add_alert(self, ctx: SlashContext):
if not await self._users.exists(ctx.user.id): if not await self._users.exists(ctx.user.id):
try: await ctx.send(
await self.region_select_menu(ctx) "You are not registered with any region\n"
except TimeoutError: "Please register with /register before adding alerts",
ephemeral=True,
)
return return
user = await self._users.get(ctx.user.id) user = await self._users.get(ctx.user.id)
try: try:
@ -203,7 +206,12 @@ class Tracker(Extension):
name="remove-alert", description="Remove an alert you have signed up for" name="remove-alert", description="Remove an alert you have signed up for"
) )
async def remove_alert(self, ctx: SlashContext): async def remove_alert(self, ctx: SlashContext):
if not self._user_is_registered(ctx): if not await self._users.exists(ctx.user.id):
await ctx.send(
"You are not registered with any region\n"
"Please register with /register before adding alerts",
ephemeral=True,
)
return return
user = await self._users.get(ctx.user.id) user = await self._users.get(ctx.user.id)
alerts = await self._users.list_alerts(user) alerts = await self._users.list_alerts(user)
@ -226,7 +234,12 @@ class Tracker(Extension):
name="list-alerts", description="List all alerts you have signed up for" name="list-alerts", description="List all alerts you have signed up for"
) )
async def list_alerts(self, ctx: SlashContext): async def list_alerts(self, ctx: SlashContext):
if not await self._user_is_registered(ctx): if not await self._users.exists(ctx.user.id):
await ctx.send(
"You are not registered with any region\n"
"Please register with /register before adding alerts",
ephemeral=True,
)
return return
user = await self._users.get(ctx.user.id) user = await self._users.get(ctx.user.id)
alerts = await self._users.list_alerts(user) alerts = await self._users.list_alerts(user)
@ -236,7 +249,7 @@ class Tracker(Extension):
alerts_str = f"You have {len(alerts)} out of 25 maximum alerts registered" alerts_str = f"You have {len(alerts)} out of 25 maximum alerts registered"
embeds = [ embeds = [
Embed( Embed(
title="List of GoblinBot Tracker Alerts", title="List of TokenBot Tracker Alerts",
color=0x0000B1, color=0x0000B1,
description=alerts_str, description=alerts_str,
) )
@ -244,7 +257,7 @@ class Tracker(Extension):
alerts_by_flavor = await gather_alerts_by_flavor(alerts) alerts_by_flavor = await gather_alerts_by_flavor(alerts)
for flavor in alerts_by_flavor: for flavor in alerts_by_flavor:
embeds.append( embeds.append(
await self._render_alert_flavor(alerts_by_flavor[flavor], user=user) await self.render_alert_flavor(alerts_by_flavor[flavor], user=user)
) )
await ctx.send(embeds=embeds, ephemeral=True) await ctx.send(embeds=embeds, ephemeral=True)
@ -272,14 +285,18 @@ class Tracker(Extension):
) )
@component_callback("region_menu") @component_callback("region_menu")
async def region_menu_cb(self, ctx: ComponentContext): async def region_menu(self, ctx: ComponentContext):
discord_user = await self.bot.fetch_user(ctx.user.id) user = User(ctx.user.id, Region(ctx.values[0].lower()), subscribed_alerts=[])
await self._users.add(user)
discord_user = await self.bot.fetch_user(user.user_id)
await discord_user.send( await discord_user.send(
"You have successfully registered your region with GoblinBot!\n" "You have successfully registered with TokenBot!\n"
"Most interactions will happen in direct messages with GoblinBot here.\n" "Most interactions will happen in direct messages with TokenBot here.\n"
"You can remove your user data and alerts at any time using ```/remove-registration```\n" "You can remove your registration and alerts at any time using ```/remove-registration```\n"
)
await ctx.send(
f"Successfully registered with the {ctx.values[0]} region", ephemeral=True
) )
await ctx.defer(edit_origin=True)
@component_callback("high_alert_button") @component_callback("high_alert_button")
async def high_alert_button(self, ctx: ComponentContext): async def high_alert_button(self, ctx: ComponentContext):
@ -347,41 +364,6 @@ 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 region_select_menu(self, ctx: SlashContext, user: User | None = None):
region_menu = copy.deepcopy(REGION_MENU)
region_menu_str = str()
if user is None:
region_menu_str += "You are not currently registered with a region, please select a region to register with.\n"
region_menu_str += (
"* You can only be registered with one region at a time.\n"
"* Registering for a new region will remove your old region's registration.\n"
)
region_message = await ctx.send(
region_menu_str,
components=region_menu,
ephemeral=True,
)
try:
region_component = await self.bot.wait_for_component(
messages=region_message, components=region_menu, timeout=30
)
except TimeoutError:
region_menu.disabled = True
await region_message.edit(
context=ctx, components=region_menu, content="Timed out"
)
raise TimeoutError
else:
region_menu.disabled = True
region = Region(region_component.ctx.values[0].lower())
user = User(ctx.user.id, region, subscribed_alerts=[])
await asyncio.gather(
self._users.add(user),
region_message.edit(context=ctx, components=region_menu),
)
return 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)
@ -440,7 +422,6 @@ class Tracker(Extension):
raise TimeoutError raise TimeoutError
else: else:
menu.disabled = True menu.disabled = True
await component.ctx.defer(edit_origin=True)
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])
@ -462,17 +443,7 @@ class Tracker(Extension):
) )
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 _user_is_registered(self, ctx: SlashContext) -> bool: async def render_alert_flavor(
if not await self._users.exists(ctx.user.id):
await ctx.send(
"You are not registered with any region\n"
"Please add an alert to get started ```/add-alert```",
ephemeral=True,
)
return False
return True
async def _render_alert_flavor(
self, alerts: List[Alert], user: User | None = None self, alerts: List[Alert], user: User | None = None
) -> Embed: ) -> Embed:
region = alerts[0].region region = alerts[0].region