feat: cleaned blitz

This commit is contained in:
TresAbhi committed 2023-06-27 02:13:57 +00:00
1 parent 9dc48c9125
commit c7821d03d2
15 files changed
+36 -68

No files matched your search

@@ -3,7 +3,7 @@ import { AllStats } from '../../types/accountInfo.js';
/**
* Formula from https://web.archive.org/web/20190105064817/http://wiki.wnefficiency.net/pages/WN8
*/
export default function getWN8(expected: AllStats, average: AllStats) {
export default function calculateWN8(expected: AllStats, average: AllStats) {
const avgDmg = average.damage_dealt / average.battles;
const avgSpot = average.spotted / average.battles;
const avgFrag = average.frags / average.battles;
+3 -7
View File
@@ -1,18 +1,14 @@
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
import { BlitzServer } from '../../constants/servers.js';
import { serverAndIdPattern } from '../discord/resolvePlayerFromCommand.js';
import errorWithCause from '../node/errorWithCause.js';
import listClansPanServer from './listClansPanServer.js';
import listClans from './listClans.js';
export default async function getBlitzClan(
interaction: ChatInputCommandInteraction<CacheType>,
clan: string,
) {
export default async function getBlitzClan(clan: string) {
if (serverAndIdPattern.test(clan)) {
const [server, accountId] = clan.split('/');
return { server: server as BlitzServer, id: Number(accountId) };
} else {
const accounts = await listClansPanServer(clan);
const accounts = await listClans(clan);
if (accounts[0]) {
return { server: accounts[0].server, id: accounts[0].clan_id };
+10 -1
View File
@@ -1,6 +1,15 @@
import { WargamingResponse } from '../../types/wargamingResponse.js';
import errorWithCause from '../node/errorWithCause.js';
export type WargamingResponse<Data extends object> =
| {
status: 'error';
error: { field: string; message: string; code: 402; value: null };
}
| {
status: 'ok';
data: Data;
};
export default async function getWargamingResponse<Data extends object>(
url: string,
) {
@@ -7,7 +7,7 @@ export type ClanListWithServer = (Clan & {
server: 'com' | 'eu' | 'asia';
})[];
export default async function listClansPanServer(search: string, limit = 9) {
export default async function listClans(search: string, limit = 9) {
const trimmed = search.trim();
if (trimmed.length < 3 && trimmed.length > 100) return [];
const normalizedLimit = Math.round(limit / 3);
@@ -10,7 +10,7 @@ export type AccountListWithServer = (Account & {
export const usernamePattern = /^[a-zA-Z0-9_]{3,24}$/;
export const usernamePatternWithoutPosition = /[a-zA-Z0-9_]{3,24}/;
export default async function listAccountsPanServer(search: string, limit = 9) {
export default async function listPlayers(search: string, limit = 9) {
const trimmedSearch = search.trim();
const normalizedLimit = Math.round(limit / 3);
const encodedSearch = encodeURIComponent(trimmedSearch);
-26
View File
@@ -1,26 +0,0 @@
import {
CacheType,
ChatInputCommandInteraction,
EmbedBuilder,
} from 'discord.js';
import { client } from '../../bot.js';
import { NEGATIVE_COLOR } from '../../constants/colors.js';
import { handleError } from '../../events/error.js';
export default async function wargamingError(
interaction: ChatInputCommandInteraction<CacheType>,
error: Error,
command: string,
) {
await interaction.editReply({
embeds: [
new EmbedBuilder()
.setColor(NEGATIVE_COLOR)
.setTitle(`${client.user?.username} ran into an issue`)
.setDescription('Wargaming returned an error. Please try again later.'),
],
});
console.log(`Wargaming returned an error`);
handleError(error, command);
}
+2 -2
View File
@@ -4,14 +4,14 @@ import {
CacheType,
} from 'discord.js';
import { BLITZ_SERVERS } from '../../constants/servers.js';
import listClansPanServer from '../blitz/listClansPanServer.js';
import listClans from '../blitz/listClans.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);
const players = await listClans(focusedOption.value);
await interaction.respond(
players
+3 -3
View File
@@ -1,8 +1,8 @@
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
import { BlitzServer } from '../../constants/servers.js';
import listAccountsPanServer, {
import listPlayers, {
usernamePatternWithoutPosition,
} from '../blitz/listAccountsPanServer.js';
} from '../blitz/listPlayers.js';
import errorWithCause from '../node/errorWithCause.js';
export const serverAndIdPattern = /(com|eu|asia)\/[0-9]+/;
@@ -35,7 +35,7 @@ export default async function resolvePlayerFromCommand(
id: Number(accountId),
} satisfies ResolvedPlayer;
} else {
const accounts = await listAccountsPanServer(username);
const accounts = await listPlayers(username);
if (accounts[0]) {
return {
+2 -2
View File
@@ -4,14 +4,14 @@ import {
CacheType,
} from 'discord.js';
import { BLITZ_SERVERS } from '../../constants/servers.js';
import listAccountsPanServer from '../blitz/listAccountsPanServer.js';
import listPlayers from '../blitz/listPlayers.js';
export default async function usernameAutocomplete(
interaction: AutocompleteInteraction<CacheType>,
) {
const focusedOption = interaction.options.getFocused(true);
if (focusedOption.name !== 'username') return;
const players = await listAccountsPanServer(focusedOption.value);
const players = await listPlayers(focusedOption.value);
try {
await interaction.respond(