feat: inactive command

This commit is contained in:
TresAbhi committed 2023-04-23 23:23:55 +00:00
1 parent e5922fd646
commit 6cd135f7d4
5 files changed
+209 -10

No files matched your search

+18 -8
View File
@@ -12,8 +12,14 @@ import getBlitzStarsAccount from '../utilities/getBlitzStarsAccount.js';
import poweredByBlitzStars from '../utilities/poweredByBlitzStars.js';
export const CLANS = {
sklld: 'Skilled',
smri: 'Samurai',
sklld: {
name: 'Skilled',
id: 71559,
},
smri: {
name: 'Samurai',
id: 91244,
},
};
export async function execute(
@@ -90,15 +96,17 @@ export async function execute(
new EmbedBuilder()
.setTitle(
`${data.nickname} is ${eligible ? '' : 'not '}eligible for ${
CLANS[clan]
CLANS[clan].name
}`,
)
.setColor(eligible ? POSITIVE_COLOR : NEGATIVE_COLOR)
.setDescription(
`${
eligible
? `${data.nickname} meets all the requirements set for ${CLANS[clan]}.`
: `${data.nickname}'s shortcomings for ${CLANS[clan]} (${
? `${data.nickname} meets all the requirements set for ${CLANS[clan].name}.`
: `${data.nickname}'s shortcomings for ${
CLANS[clan].name
} (${
clan === 'sklld' ? '30 days stats' : 'career stats'
}):\n\n${issues.join('\n')}`
}\n\n${blitzLinks(data)}`,
@@ -107,7 +115,9 @@ export async function execute(
],
});
console.log(`Showing ${data.nickname}'s eligibility for ${CLANS[clan]}`);
console.log(
`Showing ${data.nickname}'s eligibility for ${CLANS[clan].name}`,
);
});
});
}
@@ -120,8 +130,8 @@ export const data = new SlashCommandBuilder()
.setName('clan')
.setDescription('The Skilled clan you are checking')
.addChoices(
{ name: CLANS.sklld, value: 'sklld' },
{ name: CLANS.smri, value: 'smri' },
{ name: CLANS.sklld.name, value: 'sklld' },
{ name: CLANS.smri.name, value: 'smri' },
)
.setRequired(true),
)
+96
View File
@@ -0,0 +1,96 @@
import {
CacheType,
ChatInputCommandInteraction,
EmbedBuilder,
SlashCommandBuilder,
} from 'discord.js';
import fetch from 'node-fetch';
import { NEGATIVE_COLOR, POSITIVE_COLOR } from '../constants/colors.js';
import { AccountInfo } from '../types/accountInfo.js';
import { ClanInfo } from '../types/clanInfo.js';
import { CLANS } from './eligible.js';
const DEFAULT_THRESHOLD = 7;
export async function execute(
interaction: ChatInputCommandInteraction<CacheType>,
) {
const threshold =
interaction.options.getNumber('threshold')! ?? DEFAULT_THRESHOLD;
const time = new Date().getTime() / 1000;
const clanName = interaction.options.getString('clan') as keyof typeof CLANS;
const clanData = (await (
await fetch(
`https://api.wotblitz.com/wotb/clans/info/?application_id=${process.env.WARGAMING_APPLICATION_ID}&clan_id=${CLANS[clanName].id}`,
)
).json()) as ClanInfo;
const clan = clanData.data[CLANS[clanName].id];
const memberIds = clan.members_ids;
const accountInfo = (await (
await fetch(
`https://api.wotblitz.com/wotb/account/info/?application_id=${
process.env.WARGAMING_APPLICATION_ID
}&account_id=${memberIds.join(',')}`,
)
).json()) as AccountInfo;
const inactiveInfo = memberIds
.map((memberId) => {
const member = accountInfo.data[memberId];
const inactiveDays = (time - member.last_battle_time) / 60 / 60 / 24;
return [member.nickname, inactiveDays] as [string, number];
})
.filter(([, inactiveDays]) => inactiveDays >= threshold)
.sort((a, b) => b[1] - a[1])
.map(([nickname, inactiveDays]) => {
const days = Math.floor(inactiveDays);
return `**${nickname}**: ${days} day${days === 1 ? '' : 's'}`;
});
const hasInactiveMembers = inactiveInfo.length > 0;
await interaction.reply({
embeds: [
new EmbedBuilder()
.setColor(hasInactiveMembers ? NEGATIVE_COLOR : POSITIVE_COLOR)
.setTitle(
hasInactiveMembers
? `${CLANS[clanName].name}'s inactive members`
: `No inactive members in ${CLANS[clanName].name}`,
)
.setDescription(
hasInactiveMembers
? `Players that exceed the threshold of ${threshold} days:\n\n${inactiveInfo.join(
'\n',
)}`
: `No players exceed the threshold of ${threshold} days.`,
),
],
});
console.log(
`Displaying inactive members in ${CLANS[clanName].name} for ${threshold} days`,
);
}
export const data = new SlashCommandBuilder()
.setName('inactive')
.setDescription('Lists all inactive players')
.addStringOption((option) =>
option
.setName('clan')
.setDescription('The Skilled clan you are checking')
.addChoices(
{ name: CLANS.sklld.name, value: 'sklld' },
{ name: CLANS.smri.name, value: 'smri' },
)
.setRequired(true),
)
.addNumberOption((option) =>
option
.setName('threshold')
.setDescription(
`The number of days inactive (default: ${DEFAULT_THRESHOLD})`,
)
.setMinValue(0),
);