feat: added other regions

This commit is contained in:
TresAbhi committed 2023-07-18 00:23:59 +00:00
1 parent c173ece882
commit 62627c96a3
6 files changed
+19 -29

No files matched your search

+8 -18
View File
@@ -4,15 +4,10 @@ import { octokit } from '../bot';
import * as Leaderboard from '../components/Leaderboard';
import TitleBar from '../components/TitleBar';
import Wrapper from '../components/Wrapper';
import { REGION_NAMES } from '../constants/regions';
import getWargamingResponse from '../core/blitz/getWargamingResponse';
import { REGION_NAMES, Region } from '../constants/regions';
import regionToRegionSubdomain from '../core/blitz/regionToRegionSubdomain';
import addUsernameChoices from '../core/discord/addUsernameChoices';
import autocompleteUsername from '../core/discord/autocompleteUsername';
import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand';
import { secrets } from '../core/node/secrets';
import addRegionChoices from '../core/discord/addRegionChoices';
import { CommandRegistry } from '../events/interactionCreate';
import { AccountInfo } from '../types/accountInfo';
export interface RatingsPlayer {
spa_id: number;
@@ -109,6 +104,7 @@ const ratingsInfo = fetch(
function optionalParameters(option: SlashCommandSubcommandBuilder) {
return option
.addStringOption(addRegionChoices)
.addIntegerOption((option) =>
option
.setName('limit')
@@ -116,8 +112,7 @@ function optionalParameters(option: SlashCommandSubcommandBuilder) {
.setRequired(false)
.setMinValue(5)
.setMaxValue(30),
)
.addStringOption(addUsernameChoices);
);
}
const LEAGUE_INDEXES = ['diamond', 'platinum', 'gold', 'silver', 'bronze'];
@@ -179,22 +174,19 @@ export const ratingsCommand: CommandRegistry = {
const limit = interaction.options.getInteger('limit') ?? 10;
const subcommand = interaction.options.getSubcommand(true);
const leagueIndex = LEAGUE_INDEXES.indexOf(subcommand);
const player = await resolvePlayerFromCommand(interaction);
const regionSubdomain = regionToRegionSubdomain(player.region);
const region = interaction.options.getString('region') as Region;
const regionSubdomain = regionToRegionSubdomain(region);
const result = await fetch(
`https://${regionSubdomain}.wotblitz.com/en/api/rating-leaderboards/league/${leagueIndex}/top/`,
)
.then((response) => response.json() as Promise<LeagueTop>)
.then(({ result }) => result.slice(0, limit));
const { nickname } = await getWargamingResponse<AccountInfo>(
`https://api.wotblitz.${player.region}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${player.id}`,
).then((accounts) => accounts[player.id]);
const awaitedInfo = await ratingsInfo;
const leagueInfo = awaitedInfo.leagues[leagueIndex];
const midnightLeaderboard = await octokit.repos
.getContent({
...DATABASE_REPO,
path: `${player.region}/ratings/${awaitedInfo.current_season}/midnight.json`,
path: `${region}/ratings/${awaitedInfo.current_season}/midnight.json`,
})
.then(({ data }) => {
if (!Array.isArray(data) && data.type === 'file') {
@@ -252,7 +244,7 @@ export const ratingsCommand: CommandRegistry = {
: `https:${leagueInfo.big_icon}`
}
description={`Top ${limit} players • ${new Date().toDateString()} • ${
REGION_NAMES[player.region]
REGION_NAMES[region]
}`}
/>
@@ -267,6 +259,4 @@ export const ratingsCommand: CommandRegistry = {
return [];
},
autocomplete: autocompleteUsername,
};
+3 -3
View File
@@ -3,7 +3,7 @@ import markdownEscape from 'markdown-escape';
import { REGION_NAMES, Region } from '../constants/regions';
import getWargamingResponse from '../core/blitz/getWargamingResponse';
import addClanChoices from '../core/discord/addClanChoices';
import addServerChoices from '../core/discord/addServerChoices';
import addRegionChoices from '../core/discord/addRegionChoices';
import embedInfo from '../core/discord/embedInfo';
import { secrets } from '../core/node/secrets';
import { CommandRegistry } from '../events/interactionCreate';
@@ -17,7 +17,7 @@ export const searchClansCommand: CommandRegistry = {
command: new SlashCommandBuilder()
.setName('searchclans')
.setDescription('Search clans')
.addStringOption(addServerChoices)
.addStringOption(addRegionChoices)
.addStringOption((option) => addClanChoices(option).setAutocomplete(false))
.addIntegerOption((option) =>
option
@@ -28,7 +28,7 @@ export const searchClansCommand: CommandRegistry = {
),
async handler(interaction) {
const server = interaction.options.getString('server') as Region;
const server = interaction.options.getString('region') as Region;
const clan = interaction.options.getString('clan')!;
const limit = interaction.options.getInteger('limit') ?? 25;
const clanList = await getWargamingResponse<ClanList>(
+3 -3
View File
@@ -3,7 +3,7 @@ import markdownEscape from 'markdown-escape';
import { REGION_NAMES, Region } from '../constants/regions';
import getWargamingResponse from '../core/blitz/getWargamingResponse';
import { usernamePattern } from '../core/blitz/listPlayers';
import addServerChoices from '../core/discord/addServerChoices';
import addRegionChoices from '../core/discord/addRegionChoices';
import addUsernameChoices from '../core/discord/addUsernameChoices';
import embedInfo from '../core/discord/embedInfo';
import { secrets } from '../core/node/secrets';
@@ -18,7 +18,7 @@ export const searchPlayersCommand: CommandRegistry = {
command: new SlashCommandBuilder()
.setName('searchplayers')
.setDescription('Search players in a Blitz server')
.addStringOption(addServerChoices)
.addStringOption(addRegionChoices)
.addStringOption((option) =>
addUsernameChoices(option).setAutocomplete(false).setRequired(true),
)
@@ -31,7 +31,7 @@ export const searchPlayersCommand: CommandRegistry = {
),
async handler(interaction) {
const server = interaction.options.getString('server') as Region;
const server = interaction.options.getString('region') as Region;
const name = interaction.options.getString('username')!;
const limit = interaction.options.getInteger('limit') ?? 25;
const trimmedSearch = name.trim();
@@ -115,7 +115,7 @@ export function Item({
>
{reward && (
<img
style={{ width: 16, height: 16, objectFit: 'fill' }}
style={{ width: 16, height: 16, objectFit: 'cover' }}
src={normalizeImage(
reward.type === 'vehicle'
? reward.vehicle.image_url
@@ -1,10 +1,10 @@
import { SlashCommandStringOption } from 'discord.js';
import { REGION_NAMES } from '../../constants/regions';
export default function addServerChoices(option: SlashCommandStringOption) {
export default function addRegionChoices(option: SlashCommandStringOption) {
return option
.setName('server')
.setDescription('The Blitz server you are in')
.setName('region')
.setDescription('The Blitz region you are in')
.addChoices(
{ name: REGION_NAMES.com, value: 'com' },
{ name: REGION_NAMES.eu, value: 'eu' },
+1 -1
View File
@@ -6,6 +6,6 @@ export default function resolvePlayerFromURL(urlString: string) {
return {
id: parseInt(url.searchParams.get('id')!),
region: url.searchParams.get('server') as Region,
region: url.searchParams.get('region') as Region,
} satisfies ResolvedPlayer;
}