Skip to content

Commit

Permalink
chore: Improve file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
seriaati committed Mar 9, 2024
1 parent 50ff1f2 commit a9c903b
Show file tree
Hide file tree
Showing 24 changed files with 88 additions and 87 deletions.
2 changes: 1 addition & 1 deletion src/bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from cachetools import TTLCache
from discord.ext import commands, tasks

from ..hoyo.novelai_client import NAIClient
from ..hoyo.clients.novelai_client import NAIClient
from ..utils import get_now
from .command_tree import CommandTree
from .translator import AppCommandTranslator, Translator
Expand Down
2 changes: 1 addition & 1 deletion src/cogs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from discord import ButtonStyle, ui
from discord.ext import commands

from ..hoyo.daily_checkin import DailyCheckin
from ..hoyo.auto_tasks.daily_checkin import DailyCheckin

if TYPE_CHECKING:
from discord.ext.commands.context import Context
Expand Down
93 changes: 47 additions & 46 deletions src/cogs/hoyo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
from ..emojis import PROJECT_AMBER
from ..enums import Game
from ..exceptions import IncompleteParamError, InvalidQueryError, NoAccountFoundError
from ..hoyo.enka_client import EnkaAPI
from ..hoyo.genshin import ambr
from ..hoyo.hsr import yatta
from ..hoyo.mihomo_client import MihomoAPI
from ..hoyo.clients import ambr_client, yatta_client
from ..hoyo.clients.enka_client import EnkaAPI
from ..hoyo.clients.mihomo_client import MihomoAPI
from ..hoyo.transformers import HoyoAccountTransformer # noqa: TCH001
from ..models import DrawInput
from ..ui import URLButtonView
Expand All @@ -40,15 +39,15 @@ def __init__(self, bot: "HoyoBuddy") -> None:
self.bot = bot

self._search_categories: dict[Game, list[str]] = {
Game.GENSHIN: [c.value for c in ambr.ItemCategory],
Game.STARRAIL: [c.value for c in yatta.ItemCategory],
Game.GENSHIN: [c.value for c in ambr_client.ItemCategory],
Game.STARRAIL: [c.value for c in yatta_client.ItemCategory],
}

# [game][category][locale][item_name] -> item_id
self._search_autocomplete_choices: dict[
Game,
dict[
ambr.ItemCategory | yatta.ItemCategory,
ambr_client.ItemCategory | yatta_client.ItemCategory,
dict[str, dict[str, str]],
],
] = {}
Expand All @@ -61,15 +60,17 @@ async def cog_unload(self) -> None:

async def _fetch_item_task(
self,
api: ambr.AmbrAPIClient | yatta.YattaAPIClient,
item_category: ambr.ItemCategory | yatta.ItemCategory,
api: ambr_client.AmbrAPIClient | yatta_client.YattaAPIClient,
item_category: ambr_client.ItemCategory | yatta_client.ItemCategory,
locale: discord.Locale,
) -> None:
if isinstance(api, ambr.AmbrAPIClient) and isinstance(item_category, ambr.ItemCategory):
if isinstance(api, ambr_client.AmbrAPIClient) and isinstance(
item_category, ambr_client.ItemCategory
):
game = Game.GENSHIN
items = await api.fetch_items(item_category)
elif isinstance(api, yatta.YattaAPIClient) and isinstance(
item_category, yatta.ItemCategory
elif isinstance(api, yatta_client.YattaAPIClient) and isinstance(
item_category, yatta_client.ItemCategory
):
game = Game.STARRAIL
items = await api.fetch_items_(item_category)
Expand All @@ -89,14 +90,14 @@ async def _setup_search_autocomplete_choices(self) -> None:
LOGGER_.info("Setting up search autocomplete choices")
start = self.bot.loop.time()

for locale in ambr.LOCALE_TO_AMBR_LANG:
async with ambr.AmbrAPIClient(locale, self.bot.translator) as api:
for item_category in ambr.ItemCategory:
for locale in ambr_client.LOCALE_TO_AMBR_LANG:
async with ambr_client.AmbrAPIClient(locale, self.bot.translator) as api:
for item_category in ambr_client.ItemCategory:
await self._fetch_item_task(api, item_category, locale)

for locale in yatta.LOCALE_TO_YATTA_LANG:
async with yatta.YattaAPIClient(locale, self.bot.translator) as api:
for item_category in yatta.ItemCategory:
for locale in yatta_client.LOCALE_TO_YATTA_LANG:
async with yatta_client.YattaAPIClient(locale, self.bot.translator) as api:
for item_category in yatta_client.ItemCategory:
await self._fetch_item_task(api, item_category, locale)

LOGGER_.info(
Expand Down Expand Up @@ -249,11 +250,11 @@ async def search_command( # noqa: C901, PLR0911, PLR0912, PLR0914, PLR0915

if game is Game.GENSHIN:
try:
category = ambr.ItemCategory(category_value)
category = ambr_client.ItemCategory(category_value)
except ValueError as e:
raise InvalidQueryError from e

if category is ambr.ItemCategory.CHARACTERS:
if category is ambr_client.ItemCategory.CHARACTERS:
character_ui = CharacterUI(
query,
author=i.user,
Expand All @@ -262,7 +263,7 @@ async def search_command( # noqa: C901, PLR0911, PLR0912, PLR0914, PLR0915
)
return await character_ui.update(i)

if category is ambr.ItemCategory.WEAPONS:
if category is ambr_client.ItemCategory.WEAPONS:
weapon_ui = WeaponUI(
query,
author=i.user,
Expand All @@ -271,14 +272,14 @@ async def search_command( # noqa: C901, PLR0911, PLR0912, PLR0914, PLR0915
)
return await weapon_ui.start(i)

if category is ambr.ItemCategory.NAMECARDS:
async with ambr.AmbrAPIClient(locale, i.client.translator) as api:
if category is ambr_client.ItemCategory.NAMECARDS:
async with ambr_client.AmbrAPIClient(locale, i.client.translator) as api:
await i.response.defer()
namecard_detail = await api.fetch_namecard_detail(int(query))
embed = api.get_namecard_embed(namecard_detail)
return await i.followup.send(embed=embed)

if category is ambr.ItemCategory.ARTIFACT_SETS:
if category is ambr_client.ItemCategory.ARTIFACT_SETS:
artifact_set_ui = ArtifactSetUI(
query,
author=i.user,
Expand All @@ -287,22 +288,22 @@ async def search_command( # noqa: C901, PLR0911, PLR0912, PLR0914, PLR0915
)
return await artifact_set_ui.start(i)

if category is ambr.ItemCategory.FOOD:
async with ambr.AmbrAPIClient(locale, i.client.translator) as api:
if category is ambr_client.ItemCategory.FOOD:
async with ambr_client.AmbrAPIClient(locale, i.client.translator) as api:
await i.response.defer()
food_detail = await api.fetch_food_detail(int(query))
embed = api.get_food_embed(food_detail)
return await i.followup.send(embed=embed)

if category is ambr.ItemCategory.MATERIALS:
async with ambr.AmbrAPIClient(locale, i.client.translator) as api:
if category is ambr_client.ItemCategory.MATERIALS:
async with ambr_client.AmbrAPIClient(locale, i.client.translator) as api:
await i.response.defer()
material_detail = await api.fetch_material_detail(int(query))
embed = api.get_material_embed(material_detail)
return await i.followup.send(embed=embed)

if category is ambr.ItemCategory.FURNISHINGS:
async with ambr.AmbrAPIClient(locale, i.client.translator) as api:
if category is ambr_client.ItemCategory.FURNISHINGS:
async with ambr_client.AmbrAPIClient(locale, i.client.translator) as api:
await i.response.defer()
furniture_detail = await api.fetch_furniture_detail(int(query))
embed = api.get_furniture_embed(furniture_detail)
Expand All @@ -317,8 +318,8 @@ async def search_command( # noqa: C901, PLR0911, PLR0912, PLR0914, PLR0915
),
)

if category is ambr.ItemCategory.FURNISHING_SETS:
async with ambr.AmbrAPIClient(locale, i.client.translator) as api:
if category is ambr_client.ItemCategory.FURNISHING_SETS:
async with ambr_client.AmbrAPIClient(locale, i.client.translator) as api:
await i.response.defer()
furniture_set_detail = await api.fetch_furniture_set_detail(int(query))
embed = api.get_furniture_set_embed(furniture_set_detail)
Expand All @@ -333,8 +334,8 @@ async def search_command( # noqa: C901, PLR0911, PLR0912, PLR0914, PLR0915
),
)

if category is ambr.ItemCategory.LIVING_BEINGS:
async with ambr.AmbrAPIClient(locale, i.client.translator) as api:
if category is ambr_client.ItemCategory.LIVING_BEINGS:
async with ambr_client.AmbrAPIClient(locale, i.client.translator) as api:
await i.response.defer()
monster_detail = await api.fetch_monster_detail(int(query))
embed = api.get_monster_embed(monster_detail)
Expand All @@ -349,8 +350,8 @@ async def search_command( # noqa: C901, PLR0911, PLR0912, PLR0914, PLR0915
),
)

if category is ambr.ItemCategory.BOOKS:
async with ambr.AmbrAPIClient(locale, i.client.translator) as api:
if category is ambr_client.ItemCategory.BOOKS:
async with ambr_client.AmbrAPIClient(locale, i.client.translator) as api:
await i.response.defer()
book = await api.fetch_book_detail(int(query))
book_volume_ui = BookVolumeUI(
Expand All @@ -362,25 +363,25 @@ async def search_command( # noqa: C901, PLR0911, PLR0912, PLR0914, PLR0915
)
return await book_volume_ui.start(i)

if category is ambr.ItemCategory.TCG:
if category is ambr_client.ItemCategory.TCG:
tcg_card_ui = TCGCardUI(
int(query), author=i.user, locale=locale, translator=i.client.translator
)
return await tcg_card_ui.start(i)
elif game is Game.STARRAIL:
try:
category = yatta.ItemCategory(category_value)
category = yatta_client.ItemCategory(category_value)
except ValueError as e:
raise InvalidQueryError from e

if category is yatta.ItemCategory.ITEMS:
async with yatta.YattaAPIClient(locale, i.client.translator) as api:
if category is yatta_client.ItemCategory.ITEMS:
async with yatta_client.YattaAPIClient(locale, i.client.translator) as api:
await i.response.defer()
item = await api.fetch_item_detail(int(query))
embed = api.get_item_embed(item)
return await i.followup.send(embed=embed)

if category is yatta.ItemCategory.LIGHT_CONES:
if category is yatta_client.ItemCategory.LIGHT_CONES:
light_cone_ui = LightConeUI(
query,
author=i.user,
Expand All @@ -389,19 +390,19 @@ async def search_command( # noqa: C901, PLR0911, PLR0912, PLR0914, PLR0915
)
return await light_cone_ui.start(i)

if category is yatta.ItemCategory.BOOKS:
if category is yatta_client.ItemCategory.BOOKS:
book_ui = BookUI(
query, author=i.user, locale=locale, translator=i.client.translator
)
return await book_ui.start(i)

if category is yatta.ItemCategory.RELICS:
if category is yatta_client.ItemCategory.RELICS:
relic_set_ui = RelicSetUI(
query, author=i.user, locale=locale, translator=i.client.translator
)
return await relic_set_ui.start(i)

if category is yatta.ItemCategory.CHARACTERS:
if category is yatta_client.ItemCategory.CHARACTERS:
try:
character_id = int(query)
except ValueError as e:
Expand Down Expand Up @@ -444,9 +445,9 @@ async def search_command_query_autocomplete(

try:
if game is Game.GENSHIN:
category = ambr.ItemCategory(i.namespace.category)
category = ambr_client.ItemCategory(i.namespace.category)
elif game is Game.STARRAIL:
category = yatta.ItemCategory(i.namespace.category)
category = yatta_client.ItemCategory(i.namespace.category)
else:
return [self._get_error_app_command_choice("Invalid game selected")]
except ValueError:
Expand Down
4 changes: 2 additions & 2 deletions src/cogs/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from discord.ext import commands, tasks

from ..hoyo.daily_checkin import DailyCheckin
from ..hoyo.notes_check import NotesChecker
from ..hoyo.auto_tasks.daily_checkin import DailyCheckin
from ..hoyo.auto_tasks.notes_check import NotesChecker
from ..utils import get_now

if TYPE_CHECKING:
Expand Down
2 changes: 1 addition & 1 deletion src/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from tortoise import fields

from ..enums import GAME_CONVERTER, Game, NotesNotifyType
from ..hoyo.gpy_client import GenshinClient
from ..hoyo.clients.gpy_client import GenshinClient

if TYPE_CHECKING:
import datetime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
import discord
import genshin

from ..bot.error_handler import get_error_embed
from ..bot.translator import LocaleStr, Translator
from ..db.models import HoyoAccount, User
from ..embeds import DefaultEmbed, Embed, ErrorEmbed
from ..enums import GAME_THUMBNAILS
from ...bot.error_handler import get_error_embed
from ...bot.translator import LocaleStr, Translator
from ...db.models import HoyoAccount, User
from ...embeds import DefaultEmbed, Embed, ErrorEmbed
from ...enums import GAME_THUMBNAILS

if TYPE_CHECKING:
import aiohttp

from ..bot.bot import HoyoBuddy
from ...bot.bot import HoyoBuddy

LOGGER_ = logging.getLogger(__name__)

Expand Down
22 changes: 11 additions & 11 deletions src/hoyo/notes_check.py → src/hoyo/auto_tasks/notes_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
from discord import Locale
from genshin.models import Notes, StarRailNote

from ..bot.error_handler import get_error_embed
from ..bot.translator import LocaleStr
from ..db.models import NotesNotify
from ..draw.main_funcs import draw_gi_notes_card, draw_hsr_notes_card
from ..embeds import DefaultEmbed, ErrorEmbed
from ..enums import Game, NotesNotifyType
from ..icons import RESIN_ICON, RTBP_ICON, TBP_ICON
from ..models import DrawInput
from ..ui.hoyo.notes.view import NotesView
from ..utils import get_now
from ...bot.error_handler import get_error_embed
from ...bot.translator import LocaleStr
from ...db.models import NotesNotify
from ...draw.main_funcs import draw_gi_notes_card, draw_hsr_notes_card
from ...embeds import DefaultEmbed, ErrorEmbed
from ...enums import Game, NotesNotifyType
from ...icons import RESIN_ICON, RTBP_ICON, TBP_ICON
from ...models import DrawInput
from ...ui.hoyo.notes.view import NotesView
from ...utils import get_now

if TYPE_CHECKING:
from ..bot.bot import HoyoBuddy
from ...bot.bot import HoyoBuddy

LOGGER_ = logging.getLogger(__name__)

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/hoyo/enka_client.py → src/hoyo/clients/enka_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import enka

from ..constants import LOCALE_TO_ENKA_LANG
from ..db.models import EnkaCache
from ...constants import LOCALE_TO_ENKA_LANG
from ...db.models import EnkaCache

if TYPE_CHECKING:
import discord
Expand Down
8 changes: 4 additions & 4 deletions src/hoyo/gpy_client.py → src/hoyo/clients/gpy_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import genshin

from ..bot.translator import LocaleStr, Translator
from ..constants import LOCALE_TO_GPY_LANG
from ..embeds import DefaultEmbed
from ..enums import GAME_CONVERTER, GAME_THUMBNAILS
from ...bot.translator import LocaleStr, Translator
from ...constants import LOCALE_TO_GPY_LANG
from ...embeds import DefaultEmbed
from ...enums import GAME_CONVERTER, GAME_THUMBNAILS

if TYPE_CHECKING:
from discord import Locale
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import mihomo

from ..constants import LOCALE_TO_MIHOMO_LANG
from ..db.models import EnkaCache
from ...constants import LOCALE_TO_MIHOMO_LANG
from ...db.models import EnkaCache

if TYPE_CHECKING:
import discord
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/ui/account/items/enter_cookies_btn.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from src.bot.translator import LocaleStr
from src.embeds import ErrorEmbed
from src.emojis import COOKIE
from src.hoyo.gpy_client import GenshinClient
from src.hoyo.clients.gpy_client import GenshinClient

from ...components import Button, GoBackButton, Modal, TextInput
from .add_acc_select import AddAccountSelect
Expand Down
2 changes: 1 addition & 1 deletion src/ui/hoyo/genshin/abyss.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from src.bot.translator import LocaleStr
from src.draw.main_funcs import draw_item_list_card
from src.hoyo.genshin.ambr import AmbrAPIClient
from src.hoyo.clients.ambr_client import AmbrAPIClient
from src.models import DrawInput

from ...components import Button, Select, SelectOption, View
Expand Down
Loading

0 comments on commit a9c903b

Please sign in to comment.