Skip to content

Commit

Permalink
feat: Allow users to choose default account for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
seriaati committed Mar 5, 2024
1 parent 1664750 commit 2b2b423
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 21 deletions.
8 changes: 8 additions & 0 deletions migrations/models/4_20240305192811_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
async def upgrade(db) -> str:
return """
ALTER TABLE "hoyoaccount" ADD "current" BOOL NOT NULL DEFAULT False;"""


async def downgrade(db) -> str:
return """
ALTER TABLE "hoyoaccount" DROP COLUMN "current";"""
20 changes: 14 additions & 6 deletions src/cogs/hoyo.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ def _get_error_app_command_choice(error_message: str) -> app_commands.Choice[str
)
@app_commands.describe(
account=app_commands.locale_str(
"Account to run this command with, defaults to the first one",
"Account to run this command with, defaults to the selected one in /accounts",
key="account_autocomplete_param_description",
replace_command_mentions=False,
)
)
async def checkin_command(
Expand All @@ -166,10 +167,13 @@ async def checkin_command(
account: app_commands.Transform[HoyoAccount | None, HoyoAccountTransformer] = None,
) -> Any:
settings = await Settings.get(user_id=i.user.id)
account = (
account
or await HoyoAccount.filter(user_id=i.user.id, current=True).first()
or await HoyoAccount.filter(user_id=i.user.id).first()
)
if account is None:
account = await HoyoAccount.filter(user_id=i.user.id).first()
if account is None:
raise NoAccountFoundError
raise NoAccountFoundError

view = CheckInUI(
account,
Expand Down Expand Up @@ -475,8 +479,9 @@ async def search_command_query_autocomplete(
)
@app_commands.describe(
account=app_commands.locale_str(
"Account to run this command with, defaults to the first one",
"Account to run this command with, defaults to the selected one in /accounts",
key="account_autocomplete_param_description",
replace_command_mentions=False,
),
uid=app_commands.locale_str(
"UID of the player, this overrides the account parameter if provided",
Expand Down Expand Up @@ -559,7 +564,10 @@ async def _get_uid_and_game(
)
game = Game(game_value)
elif account is None:
account_ = await HoyoAccount.filter(user_id=user_id).first()
account_ = (
await HoyoAccount.filter(user_id=user_id, current=True).first()
or await HoyoAccount.filter(user_id=user_id).first()
)
if account_ is None:
raise NoAccountFoundError
uid_ = account_.uid
Expand Down
9 changes: 5 additions & 4 deletions src/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class User(Model):
id = fields.BigIntField(pk=True, index=True, generated=False) # noqa: A003
settings: fields.BackwardOneToOneRelation["Settings"]
temp_data: dict[str, Any] = fields.JSONField(default=dict) # type: ignore
temp_data: fields.Field[dict[str, Any]] = fields.JSONField(default=dict) # type: ignore
accounts: fields.ReverseRelation["HoyoAccount"]

def __str__(self) -> str:
Expand All @@ -32,6 +32,7 @@ class HoyoAccount(Model):
"models.User", related_name="accounts"
)
daily_checkin = fields.BooleanField(default=True)
current = fields.BooleanField(default=False)
notif_settings: fields.BackwardOneToOneRelation["AccountNotifSettings"]

class Meta:
Expand Down Expand Up @@ -75,10 +76,10 @@ class CardSettings(Model):
"models.User", related_name="card_settings"
)
dark_mode = fields.BooleanField()
custom_images: list[str] = fields.JSONField(default=[]) # type: ignore
custom_images: fields.Field[list[str]] = fields.JSONField(default=[]) # type: ignore
"""URLs of custom images."""
custom_primary_color: str | None = fields.CharField(max_length=7, null=True) # type: ignore
current_image: str | None = fields.CharField(max_length=100, null=True) # type: ignore
custom_primary_color: fields.Field[str | None] = fields.CharField(max_length=7, null=True) # type: ignore
current_image: fields.Field[str | None] = fields.CharField(max_length=100, null=True) # type: ignore
template = fields.CharField(max_length=32, default="hb1")

class Meta:
Expand Down
10 changes: 7 additions & 3 deletions src/ui/account/items/acc_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from discord.utils import get as dget

from src.db.models import HoyoAccount

from ...components import Select, SelectOption

if TYPE_CHECKING:
Expand All @@ -17,9 +19,11 @@ def __init__(self, options: list[SelectOption]) -> None:
async def callback(self, i: "INTERACTION") -> None:
uid, game = self.values[0].split("_")
selected_account = dget(self.view.accounts, uid=int(uid), game__value=game)
if selected_account is None:
msg = "Invalid account selected"
raise ValueError(msg)
assert selected_account is not None

await HoyoAccount.filter(user=self.view.user).update(current=False)
selected_account.current = True
await selected_account.save(update_fields=["current"])

self.view.selected_account = selected_account
await self.view.refresh(i, soft=True)
4 changes: 2 additions & 2 deletions src/ui/account/items/add_acc_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from tortoise.exceptions import IntegrityError

from src.bot.translator import LocaleStr
from src.db.models import AccountNotifSettings, HoyoAccount
from src.emojis import get_game_emoji
from src.enums import GAME_CONVERTER

from ....db.models import AccountNotifSettings, HoyoAccount
from ....enums import GAME_CONVERTER
from ...components import Select, SelectOption

if TYPE_CHECKING:
Expand Down
4 changes: 2 additions & 2 deletions src/ui/account/items/with_email_pswd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from discord import ButtonStyle

from src.bot.translator import LocaleStr
from src.db.models import User
from src.embeds import DefaultEmbed, ErrorEmbed
from src.emojis import PASSWORD
from src.hoyo.dataclasses import LoginNotifPayload

from ....db.models import User
from ....hoyo.dataclasses import LoginNotifPayload
from ...components import Button, GoBackButton, Modal, TextInput
from .add_acc_select import AddAccountSelect

Expand Down
13 changes: 9 additions & 4 deletions src/ui/account/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def __init__(

async def init(self) -> None:
if self.accounts:
self.selected_account = self.accounts[0]
self.selected_account = (
next((a for a in self.accounts if a.current), None) or self.accounts[0]
)
self.add_item(AccountSelect(self.get_account_options()))
self.add_item(AddAccountButton())
self.add_item(EditNicknameButton())
Expand Down Expand Up @@ -69,19 +71,22 @@ def get_account_embed(self) -> DefaultEmbed:
embed.add_field(
name=LocaleStr("Game", key="account_game"),
value=LocaleStr(account.game.value, warn_no_key=False),
inline=False,
)
embed.add_field(
name=LocaleStr("Server", key="account_server"),
value=LocaleStr(account.server, warn_no_key=False),
inline=False,
)
if account.nickname:
embed.add_field(
name=LocaleStr("Username", key="account_username"),
value=account.username,
inline=False,
)
embed.set_footer(
text=LocaleStr(
"Selected account will be the default one used for all commands",
key="account_manager_footer",
)
)
return embed

def get_account_options(self) -> list[SelectOption]:
Expand Down

0 comments on commit 2b2b423

Please sign in to comment.