1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-11-16 20:00:59 -05:00

Consolidate the function into mp.ts

This commit is contained in:
toast-ts 2024-05-16 09:22:34 +10:00
parent b47cb825d3
commit 46912aa4a4
2 changed files with 12 additions and 14 deletions

View File

@ -5,7 +5,6 @@ import Logger from '../helpers/Logger.js';
import CanvasBuilder from '../components/CanvasBuilder.js';
import RanIntoHumor from '../helpers/RanIntoHumor.js';
import MessageTool from '../helpers/MessageTool.js';
import PalletLibrary from '../helpers/PalletLibrary.js';
import {FSData} from 'src/interfaces';
import {requestServerData, mpModuleDisabled, refreshTimerSecs, playtimeStat, MPChannels} from '../modules/MPModule.js';
@ -23,6 +22,7 @@ async function fetchData(client:TClient, interaction:Discord.ChatInputCommandInt
const logPrefix = 'MPDB';
const MAP_POOL_HOOKMSG = '1141293129673232435';
const PALLET_FILTER = ['PALLETS', 'BIGBAGPALLETS'];
export default class MP {
static async autocomplete(client:TClient, interaction:Discord.AutocompleteInteraction<'cached'>) {
@ -103,7 +103,7 @@ export default class MP {
pallets: async()=>{
const DSS = await fetchData(client, interaction, choiceSelector) as FSData;
if (!DSS) return console.log('Endpoint failed - pallets');
const filter = DSS?.vehicles.filter(x=>['PALLETS', 'BIGBAGPALLETS'].includes(x.category));
const filter = DSS?.vehicles.filter(x=>PALLET_FILTER.includes(x.category));
const rules = {
one: 'single pallet',
two: 'pallets',
@ -112,10 +112,10 @@ export default class MP {
}[new Intl.PluralRules('en', {type: 'ordinal'}).select(filter.length)];
if (filter.length < 1) return interaction.editReply('No pallets found on the server.');
else {
const getLongestName = Object.entries(PalletLibrary(DSS)).map(([name, _])=>name.length).sort((a,b)=>b-a)[0];
const getLongestName = Object.entries(this.getPalletCounts(DSS)).map(([name, _])=>name.length).sort((a,b)=>b-a)[0];
await interaction.editReply(MessageTool.concatMessage(
`There are currently **${filter.length}** ${rules} on the server. Here\'s the breakdown:\`\`\`ansi`,
Object.entries(PalletLibrary(DSS)).map(([name, count])=>`${ansi.blue(name.padEnd(getLongestName+3))}${ansi.yellow(count.toString())}`).join('\n'),
Object.entries(this.getPalletCounts(DSS)).map(([name, count])=>`${ansi.blue(name.padEnd(getLongestName+3))}${ansi.yellow(count.toString())}`).join('\n'),
'```'
))
}
@ -230,6 +230,14 @@ export default class MP {
const numbersArr = ['1⃣','2⃣','3⃣','4⃣','5⃣','6⃣','7⃣','8⃣','9⃣','🔟'];
await Promise.all(numbersArr.slice(0, length).map(emote=>message.react(emote)));
}
private static getPalletCounts(data:FSData) {
const pallets = data.vehicles.filter(x=>PALLET_FILTER.includes(x.category));
const counts = pallets.reduce((acc, name)=>{
acc[name.name] = (acc[name.name] ?? 0) + 1;
return acc;
}, {} as {[key:string]:number});
return counts;
}
static data = new Discord.SlashCommandBuilder()
.setName('mp')
.setDescription('Get information from the FSMP server(s)')

View File

@ -1,10 +0,0 @@
import {FSData} from 'src/interfaces';
export default function(data:FSData) {
const pallets = data.vehicles.filter(x=>['PALLETS', 'BIGBAGPALLETS'].includes(x.category));
const counts = pallets.reduce((acc, name)=>{
acc[name.name] = (acc[name.name] ?? 0) + 1;
return acc;
}, {} as {[key:string]:number});
return counts;
}