From 7e7d4f8771c1e6eac2a091effee5cedb4f049bc4 Mon Sep 17 00:00:00 2001 From: tresabhi Date: Wed, 19 Apr 2023 01:26:35 +0000 Subject: [PATCH] feat: better verify messages --- src/commands/verify.ts | 86 ++++++++++++++---------------------- src/utilities/getBlitzURL.ts | 3 ++ 2 files changed, 35 insertions(+), 54 deletions(-) create mode 100644 src/utilities/getBlitzURL.ts diff --git a/src/commands/verify.ts b/src/commands/verify.ts index 8fa3d233c..29d7778a6 100644 --- a/src/commands/verify.ts +++ b/src/commands/verify.ts @@ -4,73 +4,51 @@ import { SlashCommandBuilder, } from 'discord.js'; import config from '../../config.json' assert { type: 'json' }; +import getBlitzURL from '../utilities/getBlitzURL.js'; export async function execute(interaction: CommandInteraction) { - const response = (await fetch( - `https://api.wotblitz.${ - //@ts-ignore - interaction.options.getString('server') === 'na' - ? 'com' - : //@ts-ignore - interaction.options.getString('server') - }/wotb/account/list/?application_id=${ - config.wargaming_application_id - //@ts-ignore - }&search=${interaction.options.getString('ign')}`, + //@ts-ignore + const ign = interaction.options.getString('ign'); + //@ts-ignore + const server = interaction.options.getString('server'); + const blitzURL = getBlitzURL(server); + const players = (await fetch( + `${blitzURL}account/list/?application_id=${config.wargaming_application_id}&search=${ign}`, ).then((response) => response.json())) as { data: { nickname: string; account_id: number }[]; }; - if (response.data.length === 1) { - //@ts-ignore - if (response.data[0].nickname === interaction.options.getString('ign')) { - interaction.reply({ - embeds: [ - new EmbedBuilder() - .setColor('#82ff29') - .setTitle(`${interaction.user.username} is verified`) - .setDescription( - `The user is now verified as ${ - //@ts-ignore - interaction.options.getString('ign') - }`, - ), - ], - }); - } else { - interaction.reply({ - embeds: [ - new EmbedBuilder() - .setColor('#ff4747') - .setTitle(`Account not found`) - .setDescription( - //@ts-ignore - `No exact matches were found for "${interaction.options.getString( - 'ign', - )}" in the ${ - //@ts-ignore - interaction.options.getString('server') - } server. Are you sure you have no typos? Capitalization matters.`, - ), - ], - }); - } + if (players.data[0].nickname === ign) { + // good match + const clan = (await fetch( + `${blitzURL}clans/accountinfo/?application_id=${config.wargaming_application_id}&account_id=${players.data[0].account_id}&extra=clan`, + ).then((response) => response.json())) as { + data: { [key: number]: { clan: { tag: string } } }; + }; + + interaction.reply({ + embeds: [ + new EmbedBuilder() + .setColor('#82ff29') + .setTitle(`${interaction.user.username} is verified`) + .setDescription( + `The user is now verified as ${ign} [${ + clan.data[players.data[0].account_id].clan.tag + }]`, + ), + ], + }); } else { + // no exact match interaction.reply({ embeds: [ new EmbedBuilder() .setColor('#ff4747') .setTitle(`Account not found`) .setDescription( - //@ts-ignore - `Are you sure your username is "${interaction.options.getString( - 'ign', - )}" in the ${ - //@ts-ignore - interaction.options.getString('server') - } server? I found ${ - response.data.length < 100 ? response.data.length : 'over 100' - } accounts. Try re-running the command.`, + `Are you sure your username is exactly "${ign}" in the ${server} server? I found ${ + players.data.length < 100 ? players.data.length : 'over 100' + } account(s). Try re-running the command. There might be a typo and capitalization matters.`, ), ], }); diff --git a/src/utilities/getBlitzURL.ts b/src/utilities/getBlitzURL.ts new file mode 100644 index 000000000..91db3a46a --- /dev/null +++ b/src/utilities/getBlitzURL.ts @@ -0,0 +1,3 @@ +export default function getBlitzURL(server: string) { + return `https://api.wotblitz.${server === 'na' ? 'com' : server}/wotb/`; +}