From f965452c5d10219bf4dd2880aa131e2ca0f7b9c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Tue, 10 Oct 2023 21:45:00 -0500 Subject: [PATCH] feat: re-added manual username entry --- src/core/discord/resolvePlayerFromCommand.ts | 45 +++++++++++++++++--- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/src/core/discord/resolvePlayerFromCommand.ts b/src/core/discord/resolvePlayerFromCommand.ts index 4958bee03..0b34a317f 100644 --- a/src/core/discord/resolvePlayerFromCommand.ts +++ b/src/core/discord/resolvePlayerFromCommand.ts @@ -1,5 +1,7 @@ import { CacheType, ChatInputCommandInteraction } from 'discord.js'; +import markdownEscape from 'markdown-escape'; import { Region } from '../../constants/regions'; +import listPlayers from '../blitz/listPlayers'; import { getBlitzFromDiscord } from '../cockroackdb/discordBlitz'; import throwError from '../node/throwError'; @@ -13,14 +15,43 @@ export interface ResolvedPlayer { export default async function resolvePlayerFromCommand( interaction: ChatInputCommandInteraction, ) { - const account = await getBlitzFromDiscord(parseInt(interaction.user.id)); + const commandUsername = interaction.options.getString('username'); - if (account) { - return { region: account.region, id: account.blitz }; + if (commandUsername) { + if (serverAndIdPattern.test(commandUsername)) { + const [server, accountId] = commandUsername.split('/'); + + return { + region: server as Region, + id: Number(accountId), + } satisfies ResolvedPlayer; + } else { + const accounts = await listPlayers(commandUsername); + + if (accounts[0]) { + return { + region: accounts[0].region, + id: accounts[0].account_id, + } satisfies ResolvedPlayer; + } else { + throw throwError( + 'Could not find user', + `I couldn't find user "${markdownEscape( + commandUsername, + )}". Try picking an user from the search result, typing in a valid username, or using the \`/link\` command.`, + ); + } + } } else { - throw throwError( - "You're not linked", - 'Use the `/link` command to link your Discord and Blitz accounts.', - ); + const account = await getBlitzFromDiscord(parseInt(interaction.user.id)); + + if (account) { + return { region: account.region, id: account.blitz }; + } else { + throw throwError( + "You're not linked", + 'Use the `/link` command to link your Discord and Blitz accounts.', + ); + } } }