2023-06-27 02:07:14 +00:00
|
|
|
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
2023-10-10 21:45:00 -05:00
|
|
|
import markdownEscape from 'markdown-escape';
|
2023-07-15 18:44:20 +00:00
|
|
|
import { Region } from '../../constants/regions';
|
2023-10-14 11:36:10 -05:00
|
|
|
import searchPlayersAcrossRegions from '../blitz/searchPlayersAcrossRegions';
|
|
|
|
|
import { getBlitzFromDiscord } from '../blitzkrieg/discordBlitz';
|
2023-10-17 20:58:36 -05:00
|
|
|
import { UserError } from '../blitzkrieg/userError';
|
2023-06-27 02:07:14 +00:00
|
|
|
|
|
|
|
|
export const serverAndIdPattern = /(com|eu|asia)\/[0-9]+/;
|
|
|
|
|
|
|
|
|
|
export interface ResolvedPlayer {
|
2023-07-15 18:44:20 +00:00
|
|
|
region: Region;
|
2023-06-27 02:07:14 +00:00
|
|
|
id: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default async function resolvePlayerFromCommand(
|
|
|
|
|
interaction: ChatInputCommandInteraction<CacheType>,
|
|
|
|
|
) {
|
2023-10-10 21:45:00 -05:00
|
|
|
const commandUsername = interaction.options.getString('username');
|
2023-06-27 02:07:14 +00:00
|
|
|
|
2023-10-10 21:45:00 -05:00
|
|
|
if (commandUsername) {
|
|
|
|
|
if (serverAndIdPattern.test(commandUsername)) {
|
|
|
|
|
const [server, accountId] = commandUsername.split('/');
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
region: server as Region,
|
|
|
|
|
id: Number(accountId),
|
|
|
|
|
} satisfies ResolvedPlayer;
|
|
|
|
|
} else {
|
2023-10-14 11:15:42 -05:00
|
|
|
const accounts = await searchPlayersAcrossRegions(commandUsername);
|
2023-10-10 21:45:00 -05:00
|
|
|
|
|
|
|
|
if (accounts[0]) {
|
|
|
|
|
return {
|
|
|
|
|
region: accounts[0].region,
|
|
|
|
|
id: accounts[0].account_id,
|
|
|
|
|
} satisfies ResolvedPlayer;
|
|
|
|
|
} else {
|
2024-01-19 16:07:15 -06:00
|
|
|
throw new UserError(
|
2024-01-21 18:29:32 -06:00
|
|
|
`# Could not find user\nI couldn't find user "${markdownEscape(
|
2023-10-10 21:45:00 -05:00
|
|
|
commandUsername,
|
|
|
|
|
)}". Try picking an user from the search result, typing in a valid username, or using the \`/link\` command.`,
|
2024-01-19 16:07:15 -06:00
|
|
|
);
|
2023-10-10 21:45:00 -05:00
|
|
|
}
|
|
|
|
|
}
|
2023-10-10 20:33:24 -05:00
|
|
|
} else {
|
2023-10-10 21:45:00 -05:00
|
|
|
const account = await getBlitzFromDiscord(parseInt(interaction.user.id));
|
|
|
|
|
|
|
|
|
|
if (account) {
|
|
|
|
|
return { region: account.region, id: account.blitz };
|
|
|
|
|
} else {
|
2024-01-19 16:07:15 -06:00
|
|
|
throw new UserError(
|
2024-01-21 18:29:32 -06:00
|
|
|
"# I don't know who you are\nUse the `/link` command to get started.",
|
2024-01-19 16:07:15 -06:00
|
|
|
);
|
2023-10-10 21:45:00 -05:00
|
|
|
}
|
2023-06-27 02:07:14 +00:00
|
|
|
}
|
|
|
|
|
}
|