feat: link command basic functionaility

This commit is contained in:
TrèsAbhi committed 2023-10-10 20:33:24 -05:00
1 parent 97d427a6a8
commit 19298b9ad6
19 files changed
+225 -166

No files matched your search

+7 -33
View File
@@ -1,8 +1,6 @@
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
import { Region } from '../../constants/regions';
import listPlayers, {
usernamePatternWithoutPosition,
} from '../blitz/listPlayers';
import { getBlitzFromDiscord } from '../cockroackdb/discordBlitz';
import throwError from '../node/throwError';
export const serverAndIdPattern = /(com|eu|asia)\/[0-9]+/;
@@ -15,38 +13,14 @@ export interface ResolvedPlayer {
export default async function resolvePlayerFromCommand(
interaction: ChatInputCommandInteraction<CacheType>,
) {
const commandUsername = interaction.options.getString('username');
const displayName = interaction.guild?.members.cache
.get(interaction.user.id)
?.displayName.match(usernamePatternWithoutPosition)?.[0];
const username = commandUsername ?? displayName;
const account = await getBlitzFromDiscord(parseInt(interaction.user.id));
if (username === undefined) {
if (account) {
return { region: account.region, id: account.blitz };
} else {
throw throwError(
'Username could not be inferred',
"You didn't provide me a username in the command nor was I able to infer your Blitz username from your Discord nickname. Try providing a username manually in the command or using the `/verify` command to set your Discord nickname to your Blitz username.",
"You're not linked",
'Use the `/link` command to link your Discord and Blitz accounts.',
);
}
if (serverAndIdPattern.test(username)) {
const [server, accountId] = username.split('/');
return {
region: server as Region,
id: Number(accountId),
} satisfies ResolvedPlayer;
} else {
const accounts = await listPlayers(username);
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 \`${username}\`. Try providing a valid username manually or using the \`/verify\` command to set your Discord nickname to your Blitz username.`,
);
}
}
}