mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 03:29:51 +02:00
feat: lenient username and clan lookup
This commit is contained in:
1 parent
9df4d52bb5
commit
9a9ea3a71a
19 files changed
+292
-209
No files matched your search
@@ -0,0 +1,29 @@
|
||||
import {
|
||||
ApplicationCommandOptionChoiceData,
|
||||
AutocompleteInteraction,
|
||||
CacheType,
|
||||
} from 'discord.js';
|
||||
import { BLITZ_SERVERS } from '../../constants/servers.js';
|
||||
import listClansPanServer from '../blitz/listClansPanServer.js';
|
||||
|
||||
export default async function clanAutocomplete(
|
||||
interaction: AutocompleteInteraction<CacheType>,
|
||||
) {
|
||||
const focusedOption = interaction.options.getFocused(true);
|
||||
if (focusedOption.name !== 'clan') return;
|
||||
const players = await listClansPanServer(focusedOption.value);
|
||||
|
||||
await interaction.respond(
|
||||
players
|
||||
? players.map(
|
||||
(player) =>
|
||||
({
|
||||
name: `${player.name} (${BLITZ_SERVERS[player.server]})`,
|
||||
value: `${player.server}/${player.clan_id}`,
|
||||
} satisfies ApplicationCommandOptionChoiceData<string>),
|
||||
)
|
||||
: [],
|
||||
);
|
||||
|
||||
console.log(`Clan autocomplete for ${focusedOption.value}`);
|
||||
}
|
||||
@@ -3,33 +3,23 @@ import {
|
||||
AutocompleteInteraction,
|
||||
CacheType,
|
||||
} from 'discord.js';
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
import { AccountList } from '../../types/players.js';
|
||||
import getWargamingResponse from '../blitz/getWargamingResponse.js';
|
||||
import { args } from '../process/args.js';
|
||||
import { BLITZ_SERVERS } from '../../constants/servers.js';
|
||||
import listAccountsPanServer from '../blitz/listAccountsPanServer.js';
|
||||
|
||||
export default async function usernameAutocomplete(
|
||||
interaction: AutocompleteInteraction<CacheType>,
|
||||
) {
|
||||
const focusedOption = interaction.options.getFocused(true);
|
||||
if (focusedOption.name !== 'username') return;
|
||||
const server = interaction.options.getString('server') as BlitzServer;
|
||||
|
||||
const players = await getWargamingResponse<AccountList>(
|
||||
`https://api.wotblitz.${
|
||||
server ?? 'com'
|
||||
}/wotb/account/list/?application_id=${
|
||||
args['wargaming-application-id']
|
||||
}&search=${focusedOption.value}&limit=10`,
|
||||
);
|
||||
const players = await listAccountsPanServer(focusedOption.value);
|
||||
|
||||
await interaction.respond(
|
||||
players
|
||||
? players.map(
|
||||
(player) =>
|
||||
({
|
||||
name: player.nickname,
|
||||
value: `${player.account_id}`,
|
||||
name: `${player.nickname} (${BLITZ_SERVERS[player.server]})`,
|
||||
value: `${player.server}/${player.account_id}`,
|
||||
} satisfies ApplicationCommandOptionChoiceData<string>),
|
||||
)
|
||||
: [],
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
import errorEmbed from '../interaction/errorEmbed.js';
|
||||
import listAccountsPanServer from './listAccountsPanServer.js';
|
||||
|
||||
export const serverAndIdPattern = /(com|eu|asia)\/[0-9]+/;
|
||||
|
||||
export default async function getBlitzAccount(
|
||||
interaction: ChatInputCommandInteraction<CacheType>,
|
||||
username: string,
|
||||
): Promise<{ server: BlitzServer; id: number } | null> {
|
||||
if (serverAndIdPattern.test(username)) {
|
||||
const [server, accountId] = username.split('/');
|
||||
return { server: server as BlitzServer, id: Number(accountId) };
|
||||
} else {
|
||||
const accounts = await listAccountsPanServer(username);
|
||||
|
||||
if (accounts[0]) {
|
||||
return { server: accounts[0].server, id: accounts[0].account_id };
|
||||
} else {
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
errorEmbed(
|
||||
'Could not find user',
|
||||
`I couldn't find user \`${username}\`. Try selecting a username from the search result.`,
|
||||
),
|
||||
],
|
||||
});
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
import errorEmbed from '../interaction/errorEmbed.js';
|
||||
import { serverAndIdPattern } from './getBlitzAccount.js';
|
||||
import listClansPanServer from './listClansPanServer.js';
|
||||
|
||||
export default async function getBlitzClan(
|
||||
interaction: ChatInputCommandInteraction<CacheType>,
|
||||
clan: string,
|
||||
): Promise<{ server: BlitzServer; id: number } | null> {
|
||||
if (serverAndIdPattern.test(clan)) {
|
||||
const [server, accountId] = clan.split('/');
|
||||
return { server: server as BlitzServer, id: Number(accountId) };
|
||||
} else {
|
||||
const accounts = await listClansPanServer(clan);
|
||||
|
||||
if (accounts[0]) {
|
||||
return { server: accounts[0].server, id: accounts[0].clan_id };
|
||||
} else {
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
errorEmbed(
|
||||
'Could not find user',
|
||||
`I couldn't find clan \`${clan}\`. Try selecting a username from the search result.`,
|
||||
),
|
||||
],
|
||||
});
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
import {
|
||||
CacheType,
|
||||
ChatInputCommandInteraction,
|
||||
EmbedBuilder,
|
||||
} from 'discord.js';
|
||||
import markdownEscape from 'markdown-escape';
|
||||
import { NEGATIVE_COLOR } from '../../constants/colors.js';
|
||||
import { BLITZ_SERVERS, BlitzServer } from '../../constants/servers.js';
|
||||
import { ClanList } from '../../types/clanList.js';
|
||||
import { args } from '../process/args.js';
|
||||
import getWargamingResponse from './getWargamingResponse.js';
|
||||
|
||||
export default async function getClan(
|
||||
interaction: ChatInputCommandInteraction<CacheType>,
|
||||
name: string,
|
||||
server: BlitzServer,
|
||||
) {
|
||||
const serverName = BLITZ_SERVERS[server];
|
||||
|
||||
const clans = await getWargamingResponse<ClanList>(
|
||||
`https://api.wotblitz.${server}/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${name}`,
|
||||
);
|
||||
|
||||
if (
|
||||
clans.length > 0 &&
|
||||
(clans[0].name.toLowerCase() === name.toLowerCase() ||
|
||||
clans[0].tag.toLowerCase() === name.toLowerCase())
|
||||
) {
|
||||
return clans[0];
|
||||
} else {
|
||||
// no exact match
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setColor(NEGATIVE_COLOR)
|
||||
.setTitle(`Clan not found`)
|
||||
.setDescription(
|
||||
`I couldn't find "${markdownEscape(
|
||||
name,
|
||||
)}" in the ${serverName} server. I found ${
|
||||
clans ? (clans.length < 100 ? clans.length : 'over 100') : 0
|
||||
} similarly spelled clan${clans?.length !== 1 ? 's' : ''}. ${
|
||||
clans && clans.length > 0
|
||||
? `Did you mean "${markdownEscape(
|
||||
clans[0].name,
|
||||
)}" or "${markdownEscape(clans[0].tag)}"? `
|
||||
: ''
|
||||
}Re-run the command, don't make typos.`,
|
||||
),
|
||||
],
|
||||
});
|
||||
|
||||
console.log(`Clan not found for ${name} in ${serverName} server.`);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
import { Account, AccountList } from '../../types/accountList.js';
|
||||
import { args } from '../process/args.js';
|
||||
import getWargamingResponse from './getWargamingResponse.js';
|
||||
|
||||
export type AccountListWithServer = (Account & {
|
||||
server: 'com' | 'eu' | 'asia';
|
||||
})[];
|
||||
|
||||
export default function listAccountsPanServer(search: string, limit = 9) {
|
||||
return new Promise<AccountListWithServer>(async (resolve) => {
|
||||
const trimmed = search.trim();
|
||||
if (trimmed.length < 3 && trimmed.length > 100) resolve([]);
|
||||
|
||||
const normalizedLimit = Math.round(limit / 3);
|
||||
const accountList: AccountListWithServer = [];
|
||||
let done = 0;
|
||||
|
||||
function afterResponse() {
|
||||
done++;
|
||||
|
||||
if (done === 3) {
|
||||
resolve(accountList);
|
||||
}
|
||||
}
|
||||
|
||||
getWargamingResponse<AccountList>(
|
||||
`https://api.wotblitz.com/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
|
||||
).then((value) => {
|
||||
if (value)
|
||||
accountList.push(
|
||||
...value.map((account) => ({
|
||||
...account,
|
||||
server: 'com' as BlitzServer,
|
||||
})),
|
||||
);
|
||||
afterResponse();
|
||||
});
|
||||
getWargamingResponse<AccountList>(
|
||||
`https://api.wotblitz.eu/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
|
||||
).then((value) => {
|
||||
if (value)
|
||||
accountList.push(
|
||||
...value.map((account) => ({
|
||||
...account,
|
||||
server: 'eu' as BlitzServer,
|
||||
})),
|
||||
);
|
||||
afterResponse();
|
||||
});
|
||||
getWargamingResponse<AccountList>(
|
||||
`https://api.wotblitz.asia/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
|
||||
).then((value) => {
|
||||
if (value)
|
||||
accountList.push(
|
||||
...value.map((account) => ({
|
||||
...account,
|
||||
server: 'asia' as BlitzServer,
|
||||
})),
|
||||
);
|
||||
afterResponse();
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
import { Clan, ClanList } from '../../types/clanList.js';
|
||||
import { args } from '../process/args.js';
|
||||
import getWargamingResponse from './getWargamingResponse.js';
|
||||
|
||||
export type ClanListWithServer = (Clan & {
|
||||
server: 'com' | 'eu' | 'asia';
|
||||
})[];
|
||||
|
||||
export default function listClansPanServer(search: string, limit = 9) {
|
||||
return new Promise<ClanListWithServer>(async (resolve) => {
|
||||
const trimmed = search.trim();
|
||||
if (trimmed.length < 3 && trimmed.length > 100) resolve([]);
|
||||
|
||||
const normalizedLimit = Math.round(limit / 3);
|
||||
const accountList: ClanListWithServer = [];
|
||||
let done = 0;
|
||||
|
||||
function afterResponse() {
|
||||
done++;
|
||||
|
||||
if (done === 3) {
|
||||
resolve(accountList);
|
||||
}
|
||||
}
|
||||
|
||||
getWargamingResponse<ClanList>(
|
||||
`https://api.wotblitz.com/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
|
||||
).then((value) => {
|
||||
if (value)
|
||||
accountList.push(
|
||||
...value.map((account) => ({
|
||||
...account,
|
||||
server: 'com' as BlitzServer,
|
||||
})),
|
||||
);
|
||||
afterResponse();
|
||||
});
|
||||
getWargamingResponse<ClanList>(
|
||||
`https://api.wotblitz.eu/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
|
||||
).then((value) => {
|
||||
if (value)
|
||||
accountList.push(
|
||||
...value.map((account) => ({
|
||||
...account,
|
||||
server: 'eu' as BlitzServer,
|
||||
})),
|
||||
);
|
||||
afterResponse();
|
||||
});
|
||||
getWargamingResponse<ClanList>(
|
||||
`https://api.wotblitz.asia/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
|
||||
).then((value) => {
|
||||
if (value)
|
||||
accountList.push(
|
||||
...value.map((account) => ({
|
||||
...account,
|
||||
server: 'asia' as BlitzServer,
|
||||
})),
|
||||
);
|
||||
afterResponse();
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
||||
import { BLITZ_SERVERS, BlitzServer } from '../../constants/servers.js';
|
||||
import { AccountInfo } from '../../types/accountInfo.js';
|
||||
import { Account, AccountList } from '../../types/players.js';
|
||||
import errorEmbed from '../interaction/errorEmbed.js';
|
||||
import { args } from '../process/args.js';
|
||||
import getWargamingResponse from './getWargamingResponse.js';
|
||||
|
||||
export default async function validateUsername(
|
||||
interaction: ChatInputCommandInteraction<CacheType>,
|
||||
username: string,
|
||||
server: BlitzServer,
|
||||
) {
|
||||
const serverName = BLITZ_SERVERS[server];
|
||||
|
||||
if (Number.isNaN(Number(username))) {
|
||||
// string like "tresabhi"
|
||||
const accountList = await getWargamingResponse<AccountList>(
|
||||
`https://api.wotblitz.${server}/wotb/account/list/?application_id=${args['wargaming-application-id']}&limit=1&search=${username}`,
|
||||
);
|
||||
|
||||
if (
|
||||
accountList?.[0]?.nickname.toLowerCase().trim() ===
|
||||
username.toLowerCase().trim()
|
||||
) {
|
||||
return accountList[0];
|
||||
}
|
||||
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
errorEmbed(
|
||||
'Could not find user',
|
||||
`I couldn't find user "${username}" in the ${serverName} server. Did you select a username from the search result?`,
|
||||
),
|
||||
],
|
||||
});
|
||||
|
||||
return null;
|
||||
} else {
|
||||
// number like 1041988373
|
||||
const accountInfo = await getWargamingResponse<AccountInfo>(
|
||||
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${args['wargaming-application-id']}&account_id=${username}`,
|
||||
);
|
||||
|
||||
if (accountInfo) {
|
||||
const account = accountInfo[username as unknown as number];
|
||||
|
||||
return {
|
||||
account_id: account.account_id,
|
||||
nickname: account.nickname,
|
||||
} satisfies Account;
|
||||
}
|
||||
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
errorEmbed(
|
||||
'Could not find user',
|
||||
`I couldn't find the user by id \`${username}\` in the ${serverName} server. Did you select a username from the search result?`,
|
||||
),
|
||||
],
|
||||
});
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { SlashCommandStringOption } from 'discord.js';
|
||||
|
||||
export default function addClanOption(option: SlashCommandStringOption) {
|
||||
return option
|
||||
.setName('clan')
|
||||
.setDescription('The clan name or tag you are checking')
|
||||
.setAutocomplete(true)
|
||||
.setRequired(true);
|
||||
}
|
||||
Reference in new issue
Block a user