Skip to content

Commit

Permalink
refactor: Use get_locale utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
seriaati committed Jun 5, 2024
1 parent a6feed0 commit 635e188
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions hoyo_buddy/bot/command_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from discord import InteractionType, app_commands

from ..db.models import Settings
from ..db.models import get_locale
from ..utils import get_now
from .error_handler import get_error_embed

Expand Down Expand Up @@ -38,7 +38,7 @@ async def interaction_check(self, i: INTERACTION) -> Literal[True]:

async def on_error(self, i: INTERACTION, e: app_commands.AppCommandError) -> None:
error = e.original if isinstance(e, app_commands.errors.CommandInvokeError) else e
locale = (await Settings.get(user_id=i.user.id)).locale or i.locale
locale = await get_locale(i)
embed, recognized = get_error_embed(error, locale, i.client.translator)
if not recognized:
i.client.capture_exception(error)
Expand Down
8 changes: 4 additions & 4 deletions hoyo_buddy/cogs/farm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ..bot.translator import LocaleStr
from ..commands.farm import Action, FarmCommand
from ..db.models import FarmNotify, HoyoAccount, Settings
from ..db.models import FarmNotify, HoyoAccount, Settings, get_locale
from ..enums import Game
from ..hoyo.clients.ambr import ItemCategory
from ..hoyo.transformers import HoyoAccountTransformer # noqa: TCH001
Expand Down Expand Up @@ -157,7 +157,7 @@ async def farm_reminder_command(
async def account_autocomplete(
self, i: INTERACTION, current: str
) -> list[app_commands.Choice[str]]:
locale = (await Settings.get(user_id=i.user.id)).locale or i.locale
locale = await get_locale(i)
user: USER = i.namespace.user
return await self.bot.get_account_autocomplete(
user, i.user.id, current, locale, self.bot.translator, (Game.GENSHIN,)
Expand All @@ -179,7 +179,7 @@ async def query_autocomplete(self, i: INTERACTION, current: str) -> list[app_com
]

if not current:
locale = (await Settings.get(user_id=i.user.id)).locale or i.locale
locale = await get_locale(i)
choice_dict = dict(
characters.get(locale.value, characters[Locale.american_english.value]).items()
)
Expand Down Expand Up @@ -209,7 +209,7 @@ async def user_query_autocomplete(
weapons = self.bot.autocomplete_choices[Game.GENSHIN][ItemCategory.WEAPONS]

if not current:
locale = (await Settings.get(user_id=i.user.id)).locale or i.locale
locale = await get_locale(i)
try:
choice_dict = dict(characters[locale.value].items()) | dict(
weapons[locale.value].items()
Expand Down
12 changes: 6 additions & 6 deletions hoyo_buddy/cogs/hoyo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ..bot.translator import LocaleStr
from ..commands.geetest import GeetestCommand
from ..commands.profile import ProfileCommand
from ..db.models import HoyoAccount, Settings
from ..db.models import HoyoAccount, Settings, get_locale
from ..draw.main_funcs import draw_exploration_card
from ..embeds import DefaultEmbed
from ..enums import Game, Platform
Expand Down Expand Up @@ -147,7 +147,7 @@ async def profile_command(
) -> None:
await i.response.defer()

locale = (await Settings.get(user_id=i.user.id)).locale or i.locale
locale = await get_locale(i)
user = user or i.user
uid_, game, account_ = await self._get_uid_and_game(user.id, account, uid, game_value)

Expand Down Expand Up @@ -385,7 +385,7 @@ async def redeem_command(
account_ = account or await self.bot.get_account(
user.id, (Game.GENSHIN, Game.STARRAIL), (Platform.HOYOLAB,)
)
locale = (await Settings.get(user_id=i.user.id)).locale or i.locale
locale = await get_locale(i)

view = RedeemUI(account_, author=i.user, locale=locale, translator=self.bot.translator)
await i.response.send_message(embed=view.start_embed, view=view)
Expand Down Expand Up @@ -421,7 +421,7 @@ async def geetest_command(
async def characters_command_autocomplete(
self, i: INTERACTION, current: str
) -> list[app_commands.Choice]:
locale = (await Settings.get(user_id=i.user.id)).locale or i.locale
locale = await get_locale(i)
user: USER = i.namespace.user
return await self.bot.get_account_autocomplete(
user,
Expand All @@ -437,7 +437,7 @@ async def characters_command_autocomplete(
@notes_command.autocomplete("account")
@redeem_command.autocomplete("account")
async def account_autocomplete(self, i: INTERACTION, current: str) -> list[app_commands.Choice]:
locale = (await Settings.get(user_id=i.user.id)).locale or i.locale
locale = await get_locale(i)
user: USER = i.namespace.user
return await self.bot.get_account_autocomplete(
user,
Expand All @@ -453,7 +453,7 @@ async def account_autocomplete(self, i: INTERACTION, current: str) -> list[app_c
async def checkin_command_autocomplete(
self, i: INTERACTION, current: str
) -> list[app_commands.Choice]:
locale = (await Settings.get(user_id=i.user.id)).locale or i.locale
locale = await get_locale(i)
user: USER = i.namespace.user
return await self.bot.get_account_autocomplete(
user,
Expand Down
4 changes: 2 additions & 2 deletions hoyo_buddy/cogs/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from discord.app_commands import locale_str
from discord.ext import commands

from ..db.models import HoyoAccount, Settings, User
from ..db.models import HoyoAccount, User, get_locale
from ..ui.account.view import AccountManager

if TYPE_CHECKING:
Expand All @@ -22,7 +22,7 @@ def __init__(self, bot: HoyoBuddy) -> None:
description=locale_str("Manage your accounts", key="accounts_command_description"),
)
async def accounts(self, i: INTERACTION) -> Any:
locale = (await Settings.get(user_id=i.user.id)).locale or i.locale
locale = await get_locale(i)
user = await User.get(id=i.user.id)
accounts = await HoyoAccount.filter(user=user).all()

Expand Down
6 changes: 3 additions & 3 deletions hoyo_buddy/cogs/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from loguru import logger

from ..bot.translator import LocaleStr
from ..db.models import Settings
from ..db.models import Settings, get_locale
from ..emojis import PROJECT_AMBER
from ..enums import Game
from ..exceptions import InvalidQueryError
Expand Down Expand Up @@ -417,7 +417,7 @@ async def search_command_category_autocomplete(
)
]

locale = (await Settings.get(user_id=i.user.id)).locale or i.locale
locale = await get_locale(i)
return [
app_commands.Choice(
name=LocaleStr(c, warn_no_key=False).translate(i.client.translator, locale),
Expand Down Expand Up @@ -473,7 +473,7 @@ async def search_command_query_autocomplete( # noqa: PLR0912, PLR0911
]

if not current:
locale = (await Settings.get(user_id=i.user.id)).locale or i.locale
locale = await get_locale(i)
try:
choice_dict = self.bot.autocomplete_choices[game][category][locale.value]
except KeyError:
Expand Down
4 changes: 2 additions & 2 deletions hoyo_buddy/commands/geetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ..bot.error_handler import get_error_embed
from ..bot.translator import LocaleStr
from ..constants import GEETEST_SERVERS
from ..db.models import HoyoAccount, Settings, User
from ..db.models import HoyoAccount, User, get_locale
from ..embeds import DefaultEmbed
from ..models import LoginNotifPayload
from ..ui.components import URLButtonView
Expand Down Expand Up @@ -94,7 +94,7 @@ async def run(self) -> None:
await i.response.defer()
assert i.channel is not None

self._locale = (await Settings.get(user_id=i.user.id)).locale or i.locale
self._locale = await get_locale(i)

client = self._account.client
client.set_lang(i.locale)
Expand Down
6 changes: 3 additions & 3 deletions hoyo_buddy/ui/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .. import emojis
from ..bot.error_handler import get_error_embed
from ..bot.translator import LocaleStr, Translator
from ..db.models import Settings
from ..db.models import Settings, get_locale
from ..embeds import ErrorEmbed
from ..exceptions import InvalidInputError

Expand Down Expand Up @@ -88,7 +88,7 @@ async def on_error(
error: Exception,
_: discord.ui.Item[Any],
) -> None:
locale = (await Settings.get(user_id=i.user.id)).locale or i.locale
locale = await get_locale(i)
embed, recognized = get_error_embed(error, locale, i.client.translator)
if not recognized:
i.client.capture_exception(error)
Expand Down Expand Up @@ -589,7 +589,7 @@ async def on_error(
i: INTERACTION,
error: Exception,
) -> None:
locale = (await Settings.get(user_id=i.user.id)).locale or i.locale
locale = await get_locale(i)
embed, recognized = get_error_embed(error, locale, i.client.translator)
if not recognized:
i.client.capture_exception(error)
Expand Down

0 comments on commit 635e188

Please sign in to comment.