feat: re-added manual username entry

This commit is contained in:
TrèsAbhi committed 2023-10-10 21:45:00 -05:00
1 parent be644bb3d9
commit f965452c5d
1 file changed
+38 -7
+38 -7
View File
@@ -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<CacheType>,
) {
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.',
);
}
}
}