From 09b7b45961a966a0e16f46ff7205fc216e167d7c Mon Sep 17 00:00:00 2001 From: seria Date: Wed, 15 Jan 2025 21:49:31 +0900 Subject: [PATCH] feat: Auto update lang settings --- hoyo_buddy/bot/command_tree.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/hoyo_buddy/bot/command_tree.py b/hoyo_buddy/bot/command_tree.py index 9f26a268..1bd0a9ee 100644 --- a/hoyo_buddy/bot/command_tree.py +++ b/hoyo_buddy/bot/command_tree.py @@ -17,10 +17,18 @@ class CommandTree(app_commands.CommandTree): async def interaction_check(self, i: Interaction) -> Literal[True]: - if ( - i.type not in {InteractionType.application_command, InteractionType.autocomplete} - or i.user.id in i.client.user_ids - ): + if i.type not in {InteractionType.application_command, InteractionType.autocomplete}: + return True + + # Set user's language if not set + async with i.client.pool.acquire() as conn: + await conn.execute( + 'UPDATE "user" SET lang = $2 WHERE id = $1 AND lang IS NULL;', + i.user.id, + i.locale.value, + ) + + if i.user.id in i.client.user_ids: return True async with i.client.pool.acquire() as conn: @@ -30,8 +38,9 @@ async def interaction_check(self, i: Interaction) -> Literal[True]: "{}", ) await conn.execute( - 'INSERT INTO "settings" (user_id) VALUES ($1) ON CONFLICT (user_id) DO NOTHING;', + 'INSERT INTO "settings" (user_id, lang) VALUES ($1, $2) ON CONFLICT (user_id) DO NOTHING;', i.user.id, + i.locale.value, ) i.client.user_ids.add(i.user.id)