mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 18:57:03 +02:00
feat: split resolvePlayer
This commit is contained in:
1 parent
c221c06511
commit
a67d1700fc
17 files changed
+101
-88
No files matched your search
@@ -11,7 +11,7 @@ import addUsernameOption from '../core/discord/addUsernameOption.js';
|
||||
import cleanTable, { TableInput } from '../core/discord/cleanTable.js';
|
||||
import negativeEmbed from '../core/discord/negativeEmbed.js';
|
||||
import positiveEmbed from '../core/discord/positiveEmbed.js';
|
||||
import resolvePlayer from '../core/discord/resolvePlayer.js';
|
||||
import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand.js';
|
||||
import { WARGAMING_APPLICATION_ID } from '../core/node/args.js';
|
||||
import { CommandRegistry } from '../events/interactionCreate/index.js';
|
||||
import { AccountInfo } from '../types/accountInfo.js';
|
||||
@@ -48,7 +48,7 @@ export default {
|
||||
|
||||
async handler(interaction) {
|
||||
const clan = interaction.options.getString('clan') as SkilledClan;
|
||||
const { id, server } = await resolvePlayer(interaction);
|
||||
const { id, server } = await resolvePlayerFromCommand(interaction);
|
||||
const accountInfo = await getWargamingResponse<AccountInfo>(
|
||||
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
|
||||
);
|
||||
|
||||
@@ -2,7 +2,7 @@ import { SlashCommandBuilder } from 'discord.js';
|
||||
import resolveTankId from '../core/blitz/resolveTankId.js';
|
||||
import addStatsSubCommandGroups from '../core/discord/addStatsSubCommandGroups.js';
|
||||
import resolvePeriodFromCommand from '../core/discord/resolvePeriodFromCommand.js';
|
||||
import resolvePlayer from '../core/discord/resolvePlayer.js';
|
||||
import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand.js';
|
||||
import tanksAutocomplete from '../core/discord/tanksAutocomplete.js';
|
||||
import usernameAutocomplete from '../core/discord/usernameAutocomplete.js';
|
||||
import { CommandRegistry } from '../events/interactionCreate/index.js';
|
||||
@@ -24,7 +24,7 @@ export default {
|
||||
const commandGroup = interaction.options.getSubcommandGroup(
|
||||
true,
|
||||
) as StatType;
|
||||
const player = await resolvePlayer(interaction);
|
||||
const player = await resolvePlayerFromCommand(interaction);
|
||||
const period = resolvePeriodFromCommand(interaction);
|
||||
const tankIdRaw = interaction.options.getString('tank')!;
|
||||
const tankId = commandGroup === 'tank' ? resolveTankId(tankIdRaw) : 0;
|
||||
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
tankopedia,
|
||||
} from '../core/blitz/tankopedia.js';
|
||||
import addUsernameOption from '../core/discord/addUsernameOption.js';
|
||||
import resolvePlayer from '../core/discord/resolvePlayer.js';
|
||||
import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand.js';
|
||||
import usernameAutocomplete from '../core/discord/usernameAutocomplete.js';
|
||||
import { WARGAMING_APPLICATION_ID } from '../core/node/args.js';
|
||||
import { CommandRegistry } from '../events/interactionCreate/index.js';
|
||||
@@ -71,7 +71,7 @@ export default {
|
||||
|
||||
async handler(interaction) {
|
||||
const tier = Number(interaction.options.getString('tier'));
|
||||
const account = await resolvePlayer(interaction);
|
||||
const account = await resolvePlayerFromCommand(interaction);
|
||||
const { id, server } = account;
|
||||
const accountInfo = await getWargamingResponse<AccountInfo>(
|
||||
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
|
||||
|
||||
@@ -5,7 +5,7 @@ import getWargamingResponse from '../core/blitz/getWargamingResponse.js';
|
||||
import addUsernameOption from '../core/discord/addUsernameOption.js';
|
||||
import cleanTable, { TableInputEntry } from '../core/discord/cleanTable.js';
|
||||
import infoEmbed from '../core/discord/infoEmbed.js';
|
||||
import resolvePlayer from '../core/discord/resolvePlayer.js';
|
||||
import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand.js';
|
||||
import usernameAutocomplete from '../core/discord/usernameAutocomplete.js';
|
||||
import { WARGAMING_APPLICATION_ID } from '../core/node/args.js';
|
||||
import { CommandRegistry } from '../events/interactionCreate/index.js';
|
||||
@@ -35,7 +35,7 @@ export default {
|
||||
|
||||
async handler(interaction) {
|
||||
const sortBy = (interaction.options.getString('sort') ?? 'name') as SortBy;
|
||||
const account = await resolvePlayer(interaction);
|
||||
const account = await resolvePlayerFromCommand(interaction);
|
||||
const { id, server } = account;
|
||||
const accounts = await getWargamingResponse<AccountInfo>(
|
||||
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
|
||||
|
||||
@@ -4,7 +4,7 @@ import getWargamingResponse from '../core/blitz/getWargamingResponse.js';
|
||||
import addUsernameOption from '../core/discord/addUsernameOption.js';
|
||||
import cleanTable from '../core/discord/cleanTable.js';
|
||||
import infoEmbed from '../core/discord/infoEmbed.js';
|
||||
import resolvePlayer from '../core/discord/resolvePlayer.js';
|
||||
import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand.js';
|
||||
import usernameAutocomplete from '../core/discord/usernameAutocomplete.js';
|
||||
import { WARGAMING_APPLICATION_ID } from '../core/node/args.js';
|
||||
import { CommandRegistry } from '../events/interactionCreate/index.js';
|
||||
@@ -21,7 +21,7 @@ export default {
|
||||
.addStringOption(addUsernameOption),
|
||||
|
||||
async handler(interaction) {
|
||||
const account = await resolvePlayer(interaction);
|
||||
const account = await resolvePlayerFromCommand(interaction);
|
||||
const { id, server } = account;
|
||||
const accounts = await getWargamingResponse<AccountInfo>(
|
||||
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
|
||||
|
||||
@@ -8,7 +8,8 @@ import linkButton from '../core/discord/linkButton.js';
|
||||
import primaryButton from '../core/discord/primaryButton.js';
|
||||
import resolvePeriodFromButton from '../core/discord/resolvePeriodFromButton.js';
|
||||
import resolvePeriodFromCommand from '../core/discord/resolvePeriodFromCommand.js';
|
||||
import resolvePlayer from '../core/discord/resolvePlayer.js';
|
||||
import resolvePlayerFromButton from '../core/discord/resolvePlayerFromButton.js';
|
||||
import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand.js';
|
||||
import tanksAutocomplete from '../core/discord/tanksAutocomplete.js';
|
||||
import usernameAutocomplete from '../core/discord/usernameAutocomplete.js';
|
||||
import { CommandRegistry } from '../events/interactionCreate/index.js';
|
||||
@@ -30,7 +31,7 @@ export default {
|
||||
true,
|
||||
) as StatType;
|
||||
const subcommand = interaction.options.getSubcommand() as Period;
|
||||
const player = await resolvePlayer(interaction);
|
||||
const player = await resolvePlayerFromCommand(interaction);
|
||||
const period = resolvePeriodFromCommand(interaction);
|
||||
const tankIdRaw = interaction.options.getString('tank')!;
|
||||
const tankId = commandGroup === 'tank' ? resolveTankId(tankIdRaw) : 0;
|
||||
@@ -59,7 +60,7 @@ export default {
|
||||
const path = url.pathname.split('/').filter(Boolean);
|
||||
const commandGroup = path[1] as StatType;
|
||||
const period = resolvePeriodFromButton(interaction);
|
||||
const player = await resolvePlayer(interaction);
|
||||
const player = await resolvePlayerFromButton(interaction);
|
||||
|
||||
return await stats(
|
||||
commandGroup,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { SlashCommandBuilder } from 'discord.js';
|
||||
import addUsernameOption from '../core/discord/addUsernameOption.js';
|
||||
import resolvePlayer from '../core/discord/resolvePlayer.js';
|
||||
import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand.js';
|
||||
import usernameAutocomplete from '../core/discord/usernameAutocomplete.js';
|
||||
import { CommandRegistry } from '../events/interactionCreate/index.js';
|
||||
import today from '../renderers/today.js';
|
||||
@@ -16,7 +16,7 @@ export default {
|
||||
.addStringOption(addUsernameOption),
|
||||
|
||||
async handler(interaction) {
|
||||
const player = await resolvePlayer(interaction);
|
||||
const player = await resolvePlayerFromCommand(interaction);
|
||||
|
||||
return await today(player);
|
||||
},
|
||||
|
||||
@@ -5,7 +5,7 @@ import getWargamingResponse from '../core/blitz/getWargamingResponse.js';
|
||||
import addUsernameOption from '../core/discord/addUsernameOption.js';
|
||||
import negativeEmbed from '../core/discord/negativeEmbed.js';
|
||||
import positiveEmbed from '../core/discord/positiveEmbed.js';
|
||||
import resolvePlayer from '../core/discord/resolvePlayer.js';
|
||||
import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand.js';
|
||||
import usernameAutocomplete from '../core/discord/usernameAutocomplete.js';
|
||||
import { WARGAMING_APPLICATION_ID } from '../core/node/args.js';
|
||||
import { CommandRegistry } from '../events/interactionCreate/index.js';
|
||||
@@ -23,7 +23,7 @@ export default {
|
||||
.addStringOption((option) => addUsernameOption(option).setRequired(true)),
|
||||
|
||||
async handler(interaction) {
|
||||
const blitzAccount = await resolvePlayer(interaction);
|
||||
const blitzAccount = await resolvePlayerFromCommand(interaction);
|
||||
const { id, server } = blitzAccount;
|
||||
const accountInfo = await getWargamingResponse<AccountInfo>(
|
||||
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
import { serverAndIdPattern } from '../discord/resolvePlayer.js';
|
||||
import { serverAndIdPattern } from '../discord/resolvePlayerFromCommand.js';
|
||||
import errorWithCause from '../node/errorWithCause.js';
|
||||
import listClansPanServer from './listClansPanServer.js';
|
||||
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
import {
|
||||
ButtonInteraction,
|
||||
CacheType,
|
||||
ChatInputCommandInteraction,
|
||||
} from 'discord.js';
|
||||
import { CYCLIC_API } from '../../constants/cyclic.js';
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
import listAccountsPanServer, {
|
||||
usernamePatternWithoutPosition,
|
||||
} from '../blitz/listAccountsPanServer.js';
|
||||
import errorWithCause from '../node/errorWithCause.js';
|
||||
|
||||
export const serverAndIdPattern = /(com|eu|asia)\/[0-9]+/;
|
||||
|
||||
export interface ResolvedPlayer {
|
||||
server: BlitzServer;
|
||||
id: number;
|
||||
}
|
||||
|
||||
export default async function resolvePlayer(
|
||||
interaction:
|
||||
| ChatInputCommandInteraction<CacheType>
|
||||
| ButtonInteraction<CacheType>,
|
||||
) {
|
||||
if (interaction instanceof ChatInputCommandInteraction) {
|
||||
const commandUsername = interaction.options.getString('username');
|
||||
const displayName = interaction.guild?.members.cache
|
||||
.get(interaction.user.id)
|
||||
?.displayName.match(usernamePatternWithoutPosition)?.[0];
|
||||
const username = commandUsername ?? displayName;
|
||||
|
||||
if (username === undefined) {
|
||||
throw errorWithCause(
|
||||
'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.",
|
||||
);
|
||||
}
|
||||
|
||||
if (serverAndIdPattern.test(username)) {
|
||||
const [server, accountId] = username.split('/');
|
||||
return {
|
||||
server: server as BlitzServer,
|
||||
id: Number(accountId),
|
||||
} satisfies ResolvedPlayer;
|
||||
} else {
|
||||
const accounts = await listAccountsPanServer(username);
|
||||
|
||||
if (accounts[0]) {
|
||||
return {
|
||||
server: accounts[0].server,
|
||||
id: accounts[0].account_id,
|
||||
} satisfies ResolvedPlayer;
|
||||
} else {
|
||||
throw errorWithCause(
|
||||
'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.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const url = new URL(`${CYCLIC_API}/${interaction.customId}`);
|
||||
return {
|
||||
id: parseInt(url.searchParams.get('id')!),
|
||||
server: url.searchParams.get('server') as BlitzServer,
|
||||
} satisfies ResolvedPlayer;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { ButtonInteraction, CacheType } from 'discord.js';
|
||||
import { CYCLIC_API } from '../../constants/cyclic.js';
|
||||
import resolvePlayerFromURL from '../express/resolvePlayerFromURL.js';
|
||||
|
||||
export default async function resolvePlayerFromButton(
|
||||
interaction: ButtonInteraction<CacheType>,
|
||||
) {
|
||||
return resolvePlayerFromURL(`${CYCLIC_API}/${interaction.customId}`);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
import listAccountsPanServer, {
|
||||
usernamePatternWithoutPosition,
|
||||
} from '../blitz/listAccountsPanServer.js';
|
||||
import errorWithCause from '../node/errorWithCause.js';
|
||||
|
||||
export const serverAndIdPattern = /(com|eu|asia)\/[0-9]+/;
|
||||
|
||||
export interface ResolvedPlayer {
|
||||
server: BlitzServer;
|
||||
id: number;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
if (username === undefined) {
|
||||
throw errorWithCause(
|
||||
'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.",
|
||||
);
|
||||
}
|
||||
|
||||
if (serverAndIdPattern.test(username)) {
|
||||
const [server, accountId] = username.split('/');
|
||||
return {
|
||||
server: server as BlitzServer,
|
||||
id: Number(accountId),
|
||||
} satisfies ResolvedPlayer;
|
||||
} else {
|
||||
const accounts = await listAccountsPanServer(username);
|
||||
|
||||
if (accounts[0]) {
|
||||
return {
|
||||
server: accounts[0].server,
|
||||
id: accounts[0].account_id,
|
||||
} satisfies ResolvedPlayer;
|
||||
} else {
|
||||
throw errorWithCause(
|
||||
'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.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { Request } from 'express';
|
||||
import { CYCLIC_API } from '../../constants/cyclic.js';
|
||||
import resolvePlayerFromURL from './resolvePlayerFromURL.js';
|
||||
|
||||
export default async function resolvePlayerFromRequest(request: Request) {
|
||||
return resolvePlayerFromURL(`${CYCLIC_API}${request.path}`);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
import { ResolvedPlayer } from '../discord/resolvePlayerFromCommand.js';
|
||||
|
||||
export default async function resolvePlayerFromURL(urlString: string) {
|
||||
const url = new URL(urlString);
|
||||
|
||||
return {
|
||||
id: parseInt(url.searchParams.get('id')!),
|
||||
server: url.searchParams.get('server') as BlitzServer,
|
||||
} satisfies ResolvedPlayer;
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import { tankopedia } from '../core/blitz/tankopedia.js';
|
||||
import getPlayerHistories from '../core/blitzstars/getPlayerHistories.js';
|
||||
import getTankHistories from '../core/blitzstars/getTankHistories.js';
|
||||
import { ResolvedPeriod } from '../core/discord/resolvePeriodFromCommand.js';
|
||||
import { ResolvedPlayer } from '../core/discord/resolvePlayer.js';
|
||||
import { ResolvedPlayer } from '../core/discord/resolvePlayerFromCommand.js';
|
||||
import { WARGAMING_APPLICATION_ID } from '../core/node/args.js';
|
||||
import { AccountInfo } from '../types/accountInfo.js';
|
||||
import { Histories } from '../types/histories.js';
|
||||
|
||||
@@ -13,7 +13,7 @@ import { Tier, tankopedia } from '../core/blitz/tankopedia.js';
|
||||
import getTankStatsOverTime from '../core/blitzstars/getTankStatsOverTime.js';
|
||||
import { tankAverages } from '../core/blitzstars/tankAverages.js';
|
||||
import { ResolvedPeriod } from '../core/discord/resolvePeriodFromCommand.js';
|
||||
import { ResolvedPlayer } from '../core/discord/resolvePlayer.js';
|
||||
import { ResolvedPlayer } from '../core/discord/resolvePlayerFromCommand.js';
|
||||
import { WARGAMING_APPLICATION_ID } from '../core/node/args.js';
|
||||
import {
|
||||
AccountInfo,
|
||||
|
||||
@@ -14,7 +14,7 @@ import getPeriodNow from '../core/blitzstars/getPeriodNow.js';
|
||||
import getTankStatsOverTime from '../core/blitzstars/getTankStatsOverTime.js';
|
||||
import getTimeDaysAgo from '../core/blitzstars/getTimeDaysAgo.js';
|
||||
import { tankAverages } from '../core/blitzstars/tankAverages.js';
|
||||
import { ResolvedPlayer } from '../core/discord/resolvePlayer.js';
|
||||
import { ResolvedPlayer } from '../core/discord/resolvePlayerFromCommand.js';
|
||||
import { WARGAMING_APPLICATION_ID } from '../core/node/args.js';
|
||||
import { AccountInfo, AllStats } from '../types/accountInfo.js';
|
||||
import { PlayerClanData } from '../types/playerClanData.js';
|
||||
|
||||
Reference in new issue
Block a user