feat: username suggestions

This commit is contained in:
TresAbhi committed 2023-05-14 17:13:42 +00:00
1 parent 613527e78f
commit 692f4d2b80
13 files changed
+167 -102

No files matched your search

-54
View File
@@ -1,54 +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 { Players } from '../../types/players.js';
import { args } from '../process/args.js';
import getWargamingResponse from './getWargamingResponse.js';
export default async function getBlitzAccount(
interaction: ChatInputCommandInteraction<CacheType>,
name: string,
server: BlitzServer,
) {
const serverName = BLITZ_SERVERS[server];
const players = await getWargamingResponse<Players>(
`https://api.wotblitz.${server}/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${name}`,
);
if (
players.length > 0 &&
players[0].nickname.toLowerCase() === name.toLowerCase()
) {
return players[0];
} else {
// no exact match
await interaction.editReply({
embeds: [
new EmbedBuilder()
.setColor(NEGATIVE_COLOR)
.setTitle(`Account not found`)
.setDescription(
`I couldn't find "${markdownEscape(
name,
)}" in the ${serverName} server. I found ${
players ? (players.length < 100 ? players.length : 'over 100') : 0
} similarly spelled account${players?.length !== 1 ? 's' : ''}. ${
players && players.length > 0
? `Did you mean "${markdownEscape(players[0].nickname)}"? `
: ''
}Use the \`/verify\` command to fix any issues or re-run the command and don't make typos.`,
),
],
});
console.log(`Account not found for ${name} in ${serverName} server.`);
return null;
}
}
+65
View File
@@ -0,0 +1,65 @@
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;
}
}