From 8569079af9a86a267dc5309789e143fbf33991c3 Mon Sep 17 00:00:00 2001 From: tresabhi Date: Tue, 2 May 2023 23:01:54 +0000 Subject: [PATCH] feat: searchplayers --- .vscode/settings.json | 1 + src/behaviors/error.ts | 4 +- src/commands/eligible.ts | 23 +++--------- src/commands/inactive.ts | 15 ++------ src/commands/searchplayers.ts | 54 +++++++++++++++++++++++++++ src/commands/stats.ts | 23 +++--------- src/commands/tanks.ts | 27 ++++---------- src/commands/verify.ts | 23 +++--------- src/utilities/addIGNOption.ts | 8 ++++ src/utilities/addServerChoices.ts | 14 +++++++ src/utilities/getWargamingResponse.ts | 4 +- src/utilities/wargamingError.ts | 5 ++- 12 files changed, 110 insertions(+), 91 deletions(-) create mode 100644 src/commands/searchplayers.ts create mode 100644 src/utilities/addIGNOption.ts create mode 100644 src/utilities/addServerChoices.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index f115c42b5..e5c80b4cb 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,6 +4,7 @@ "accountinfo", "kranvagn", "ownedtanks", + "searchplayers", "SKLLD", "Smri", "strvk", diff --git a/src/behaviors/error.ts b/src/behaviors/error.ts index f48a0757a..5e8f9624e 100644 --- a/src/behaviors/error.ts +++ b/src/behaviors/error.ts @@ -7,7 +7,7 @@ import isDev from '../utilities/isDev.js'; const PROCESS_ERROR_EVENTS = ['uncaughtException']; const CLIENT_ERROR_EVENTS = ['error']; -function handleError(error: Error, client: Client) { +export function handleError(error: Error, client: Client) { console.error(error); ( @@ -17,7 +17,7 @@ function handleError(error: Error, client: Client) { ).send({ embeds: [ new EmbedBuilder() - .setTitle(`${client.user?.username} ran into a catastrophic error`) + .setTitle(`${client.user?.username} ran into an error`) .setColor(NEGATIVE_COLOR) .setDescription( `\`\`\`${error.name}\n${error.message}\n${error.stack}\n${error.cause}\`\`\``, diff --git a/src/commands/eligible.ts b/src/commands/eligible.ts index eb7c0ec74..a5f9eb10d 100644 --- a/src/commands/eligible.ts +++ b/src/commands/eligible.ts @@ -1,7 +1,9 @@ import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; import { CommandRegistry } from '../behaviors/interactionCreate.js'; import { NEGATIVE_COLOR, POSITIVE_COLOR } from '../constants/colors.js'; -import { BLITZ_SERVERS, BlitzServer } from '../constants/servers.js'; +import { BlitzServer } from '../constants/servers.js'; +import addIGNOption from '../utilities/addIGNOption.js'; +import addServerChoices from '../utilities/addServerChoices.js'; import blitzLinks from '../utilities/blitzLinks.js'; import getBlitzAccount from '../utilities/getBlitzAccount.js'; import getBlitzStarsAccount from '../utilities/getBlitzStarsAccount.js'; @@ -30,23 +32,8 @@ export default { ) .setRequired(true), ) - .addStringOption((option) => - option - .setName('server') - .setDescription('The Blitz server you are in') - .addChoices( - { name: BLITZ_SERVERS.com, value: 'com' }, - { name: BLITZ_SERVERS.eu, value: 'eu' }, - { name: BLITZ_SERVERS.asia, value: 'asia' }, - ) - .setRequired(true), - ) - .addStringOption((option) => - option - .setName('name') - .setDescription('The username you use in Blitz') - .setRequired(true), - ), + .addStringOption(addServerChoices) + .addStringOption(addIGNOption), execute(interaction) { const name = interaction.options.getString('name')!; diff --git a/src/commands/inactive.ts b/src/commands/inactive.ts index e86faa3f7..0a1b25bbc 100644 --- a/src/commands/inactive.ts +++ b/src/commands/inactive.ts @@ -1,9 +1,10 @@ import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; import { CommandRegistry } from '../behaviors/interactionCreate.js'; import { NEGATIVE_COLOR, POSITIVE_COLOR } from '../constants/colors.js'; -import { BLITZ_SERVERS, BlitzServer } from '../constants/servers.js'; +import { BlitzServer } from '../constants/servers.js'; import { ClanInfo } from '../types/clanInfo.js'; import { PlayerPersonalData } from '../types/playerPersonalData.js'; +import addServerChoices from '../utilities/addServerChoices.js'; import { args } from '../utilities/args.js'; import getClan from '../utilities/getClan.js'; import getWargamingResponse from '../utilities/getWargamingResponse.js'; @@ -18,17 +19,7 @@ export default { command: new SlashCommandBuilder() .setName('inactive') .setDescription('Lists all inactive players') - .addStringOption((option) => - option - .setName('server') - .setDescription('The Blitz server you are in') - .setRequired(true) - .addChoices( - { name: BLITZ_SERVERS.com, value: 'com' }, - { name: BLITZ_SERVERS.eu, value: 'eu' }, - { name: BLITZ_SERVERS.asia, value: 'asia' }, - ), - ) + .addStringOption(addServerChoices) .addStringOption((option) => option .setName('name') diff --git a/src/commands/searchplayers.ts b/src/commands/searchplayers.ts new file mode 100644 index 000000000..901e54504 --- /dev/null +++ b/src/commands/searchplayers.ts @@ -0,0 +1,54 @@ +import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; +import { CommandRegistry } from '../behaviors/interactionCreate.js'; +import { SKILLED_COLOR } from '../constants/colors.js'; +import { Players } from '../types/players.js'; +import addIGNOption from '../utilities/addIGNOption.js'; +import addServerChoices from '../utilities/addServerChoices.js'; +import { args } from '../utilities/args.js'; +import getWargamingResponse from '../utilities/getWargamingResponse.js'; + +export default { + inDevelopment: false, + inProduction: true, + inPublic: true, + + command: new SlashCommandBuilder() + .setName('searchplayers') + .setDescription('Search players in a Blitz server') + .addStringOption(addServerChoices) + .addStringOption(addIGNOption) + .addIntegerOption((option) => + option + .setName('limit') + .setDescription('The size of the search result (default: 25)') + .setMinValue(1) + .setMaxValue(100), + ), + + execute(interaction) { + const server = interaction.options.getString('server')!; + const name = interaction.options.getString('name')!; + const limit = interaction.options.getInteger('limit') ?? 25; + + getWargamingResponse( + `https://api.wotblitz.${server}/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${name}&limit=${limit}`, + interaction, + (players) => { + interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setColor(SKILLED_COLOR) + .setTitle(`Player search results for "${name}"`) + .setDescription( + `\`\`\`${ + players.length === 0 + ? 'No players found.' + : players.map((player) => player.nickname).join('\n') + }\`\`\``, + ), + ], + }); + }, + ); + }, +} satisfies CommandRegistry; diff --git a/src/commands/stats.ts b/src/commands/stats.ts index 1fe37c73f..4120fff80 100644 --- a/src/commands/stats.ts +++ b/src/commands/stats.ts @@ -1,7 +1,9 @@ import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; import { CommandRegistry } from '../behaviors/interactionCreate.js'; import { SKILLED_COLOR } from '../constants/colors.js'; -import { BLITZ_SERVERS, BlitzServer } from '../constants/servers.js'; +import { BlitzServer } from '../constants/servers.js'; +import addIGNOption from '../utilities/addIGNOption.js'; +import addServerChoices from '../utilities/addServerChoices.js'; import blitzLinks from '../utilities/blitzLinks.js'; import getBlitzAccount from '../utilities/getBlitzAccount.js'; import getBlitzStarsAccount from '../utilities/getBlitzStarsAccount.js'; @@ -26,23 +28,8 @@ export default { ) .setRequired(true), ) - .addStringOption((option) => - option - .setName('server') - .setDescription('The Blitz server you are in') - .addChoices( - { name: BLITZ_SERVERS.com, value: 'com' }, - { name: BLITZ_SERVERS.eu, value: 'eu' }, - { name: BLITZ_SERVERS.asia, value: 'asia' }, - ) - .setRequired(true), - ) - .addStringOption((option) => - option - .setName('name') - .setDescription('The username you use in Blitz') - .setRequired(true), - ), + .addStringOption(addServerChoices) + .addStringOption(addIGNOption), execute(interaction) { const name = interaction.options.getString('name')!; diff --git a/src/commands/tanks.ts b/src/commands/tanks.ts index 81bf15057..e44ac3498 100644 --- a/src/commands/tanks.ts +++ b/src/commands/tanks.ts @@ -1,8 +1,10 @@ import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; import { CommandRegistry } from '../behaviors/interactionCreate.js'; import { SKILLED_COLOR } from '../constants/colors.js'; -import { BLITZ_SERVERS, BlitzServer } from '../constants/servers.js'; +import { BlitzServer } from '../constants/servers.js'; import { TanksStats } from '../types/tanksStats.js'; +import addIGNOption from '../utilities/addIGNOption.js'; +import addServerChoices from '../utilities/addServerChoices.js'; import { args } from '../utilities/args.js'; import getBlitzAccount from '../utilities/getBlitzAccount.js'; import getWargamingResponse from '../utilities/getWargamingResponse.js'; @@ -37,24 +39,9 @@ export default { command: new SlashCommandBuilder() .setName('tanks') .setDescription("Shows a player's owned tanks") - .addStringOption((option) => - option - .setName('server') - .setDescription('The Blitz server you are in') - .setRequired(true) - .addChoices( - { name: BLITZ_SERVERS.com, value: 'com' }, - { name: BLITZ_SERVERS.eu, value: 'eu' }, - { name: BLITZ_SERVERS.asia, value: 'asia' }, - ), - ) - .addStringOption((option) => - option - .setName('name') - .setDescription('The username you use in Blitz') - .setRequired(true), - ) - .addNumberOption((option) => + .addStringOption(addServerChoices) + .addStringOption(addIGNOption) + .addIntegerOption((option) => option .setName('tier') .setDescription('The tier you want to see (default: all tiers)') @@ -66,7 +53,7 @@ export default { execute(interaction) { const server = interaction.options.getString('server') as BlitzServer; const name = interaction.options.getString('name')!; - const tier = Math.round(interaction.options.getNumber('tier')!); + const tier = interaction.options.getInteger('tier')!; getBlitzAccount(interaction, name, server, async (account) => { getWargamingResponse( diff --git a/src/commands/verify.ts b/src/commands/verify.ts index a9b58add8..4fad9688c 100644 --- a/src/commands/verify.ts +++ b/src/commands/verify.ts @@ -2,8 +2,10 @@ import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; import discord from '../../discord.json' assert { type: 'json' }; import { CommandRegistry } from '../behaviors/interactionCreate.js'; import { NEGATIVE_COLOR, POSITIVE_COLOR } from '../constants/colors.js'; -import { BLITZ_SERVERS, BlitzServer } from '../constants/servers.js'; +import { BlitzServer } from '../constants/servers.js'; import { PlayerClanData } from '../types/playerClanData.js'; +import addIGNOption from '../utilities/addIGNOption.js'; +import addServerChoices from '../utilities/addServerChoices.js'; import { args } from '../utilities/args.js'; import getBlitzAccount from '../utilities/getBlitzAccount.js'; import getWargamingResponse from '../utilities/getWargamingResponse.js'; @@ -16,23 +18,8 @@ export default { command: new SlashCommandBuilder() .setName('verify') .setDescription("Set's the user's username to their in-game name") - .addStringOption((option) => - option - .setName('server') - .setDescription('The Blitz server you are in') - .setRequired(true) - .addChoices( - { name: BLITZ_SERVERS.com, value: 'com' }, - { name: BLITZ_SERVERS.eu, value: 'eu' }, - { name: BLITZ_SERVERS.asia, value: 'asia' }, - ), - ) - .addStringOption((option) => - option - .setName('name') - .setDescription('The username you use in Blitz') - .setRequired(true), - ), + .addStringOption(addServerChoices) + .addStringOption(addIGNOption), execute(interaction) { const name = interaction.options.getString('name')!; diff --git a/src/utilities/addIGNOption.ts b/src/utilities/addIGNOption.ts new file mode 100644 index 000000000..99937265f --- /dev/null +++ b/src/utilities/addIGNOption.ts @@ -0,0 +1,8 @@ +import { SlashCommandStringOption } from 'discord.js'; + +export default function addIGNOption(option: SlashCommandStringOption) { + return option + .setName('name') + .setDescription('The username you use in Blitz') + .setRequired(true); +} diff --git a/src/utilities/addServerChoices.ts b/src/utilities/addServerChoices.ts new file mode 100644 index 000000000..ee94f20ac --- /dev/null +++ b/src/utilities/addServerChoices.ts @@ -0,0 +1,14 @@ +import { SlashCommandStringOption } from 'discord.js'; +import { BLITZ_SERVERS } from '../constants/servers.js'; + +export default function addServerChoices(option: SlashCommandStringOption) { + return option + .setName('server') + .setDescription('The Blitz server you are in') + .addChoices( + { name: BLITZ_SERVERS.com, value: 'com' }, + { name: BLITZ_SERVERS.eu, value: 'eu' }, + { name: BLITZ_SERVERS.asia, value: 'asia' }, + ) + .setRequired(true); +} diff --git a/src/utilities/getWargamingResponse.ts b/src/utilities/getWargamingResponse.ts index a2f700bb6..eb90d07a9 100644 --- a/src/utilities/getWargamingResponse.ts +++ b/src/utilities/getWargamingResponse.ts @@ -16,8 +16,8 @@ export default async function getWargamingResponse( if (response.status === 'ok') { callback(response.data); } else { - wargamingError(interaction); + wargamingError(interaction, new Error(`Status ${response.status}`)); } }) - .catch(() => wargamingError(interaction)); + .catch((error) => wargamingError(interaction, error)); } diff --git a/src/utilities/wargamingError.ts b/src/utilities/wargamingError.ts index 656574d64..396cebd60 100644 --- a/src/utilities/wargamingError.ts +++ b/src/utilities/wargamingError.ts @@ -3,11 +3,13 @@ import { ChatInputCommandInteraction, EmbedBuilder, } from 'discord.js'; +import { handleError } from '../behaviors/error.js'; import { NEGATIVE_COLOR } from '../constants/colors.js'; import { client } from '../index.js'; export default async function wargamingError( interaction: ChatInputCommandInteraction, + error: Error, ) { await interaction.editReply({ embeds: [ @@ -18,5 +20,6 @@ export default async function wargamingError( ], }); - console.log('Wargaming returned an error.'); + console.log(`Wargaming returned an error`); + handleError(error, client); }