feat: descrete wargaming response

This commit is contained in:
TresAbhi committed 2023-04-27 00:21:39 +00:00
1 parent fd1dc40918
commit d4f676091d
14 files changed
+211 -297

No files matched your search

+31 -34
View File
@@ -3,10 +3,10 @@ import {
ChatInputCommandInteraction,
EmbedBuilder,
} from 'discord.js';
import fetch from 'node-fetch';
import { NEGATIVE_COLOR } from '../constants/colors.js';
import { BLITZ_SERVERS, BlitzServer } from '../constants/servers.js';
import { Clan, ClanList } from '../types/clanList.js';
import getWargamingResponse from './getWargamingResponse.js';
export default async function getClan(
interaction: ChatInputCommandInteraction<CacheType>,
@@ -16,39 +16,36 @@ export default async function getClan(
) {
const serverName = BLITZ_SERVERS[server];
const clans = (await fetch(
getWargamingResponse<ClanList>(
`https://api.wotblitz.${server}/wotb/clans/list/?application_id=${process.env.WARGAMING_APPLICATION_ID}&search=${name}`,
).then((response) => response.json())) as ClanList;
interaction,
async (clans) => {
if (
clans.length > 0 &&
(clans[0].name === name || clans[0].tag === name.toUpperCase())
) {
callback(clans[0]);
} else {
// no exact match
await interaction.reply({
embeds: [
new EmbedBuilder()
.setColor(NEGATIVE_COLOR)
.setTitle(`Clan not found`)
.setDescription(
`I couldn't find "${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 "${clans[0].name}" or "${clans[0].tag}"? `
: ''
}Re-run the command, don't make typos, and capitalize correctly.`,
),
],
});
if (
clans.status === 'ok' &&
clans.data.length > 0 &&
(clans.data[0].name === name || clans.data[0].tag === name.toUpperCase())
) {
callback(clans.data[0]);
} else {
// no exact match
await interaction.reply({
embeds: [
new EmbedBuilder()
.setColor(NEGATIVE_COLOR)
.setTitle(`Clan not found`)
.setDescription(
`I couldn't find "${name}" in the ${serverName} server. I found ${
clans?.data
? clans.data.length < 100
? clans.data.length
: 'over 100'
: 0
} similarly spelled clan${clans?.data?.length !== 1 ? 's' : ''}. ${
clans?.data && clans.data.length > 0
? `Did you mean "${clans.data[0].name}" or "${clans.data[0].tag}"? `
: ''
}Re-run the command, don't make typos, and capitalize correctly.`,
),
],
});
console.log(`Clan not found for ${name} in ${serverName} server.`);
}
console.log(`Clan not found for ${name} in ${serverName} server.`);
}
},
);
}
+20
View File
@@ -0,0 +1,20 @@
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
import { WargamingResponse } from '../types/wargamingResponse.js';
import wargamingError from './wargamingError.js';
export default function getWargamingResponse<Data extends object>(
url: string,
interaction: ChatInputCommandInteraction<CacheType>,
callback: (data: Data) => void,
) {
fetch(url)
.then((response) => response.json())
.then((response: WargamingResponse<Data>) => {
if (response.status === 'ok') {
callback(response.data);
} else {
wargamingError(interaction);
}
})
.catch(() => wargamingError(interaction));
}