1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-11-17 00:10:58 -05:00

Cache improvements

This commit is contained in:
AnxietyisReal 2023-09-03 14:15:02 +10:00
parent c0aa61d001
commit 83b8abba33
2 changed files with 14 additions and 12 deletions

View File

@ -20,7 +20,7 @@ export default {
if (interaction.channelId === '468835769092669461' && !client.isStaff(interaction.member) && ['status', 'players'].includes(interaction.options.getSubcommand())) return interaction.reply('Please use <#739084625862852715> for `/mp status/players` commands to prevent clutter in this channel.').then(()=>setTimeout(()=>interaction.deleteReply(), 6000));
const database = await client.MPServer._content.findById(interaction.guildId);
const endpoint = await fetch(database[serverSelector].ip+'/feed/dedicated-server-stats.json?code='+database[serverSelector].code, {signal: AbortSignal.timeout(7500),headers:{'User-Agent':`Daggerbot - MPdata/undici`}}).then(r=>r.json() as Promise<FSData>);
const endpoint = await fetch(database[serverSelector].ip+'/feed/dedicated-server-stats.json?code='+database[serverSelector].code, {signal: AbortSignal.timeout(7500),headers:{'User-Agent':'Daggerbot - MPdata/undici'}}).then(r=>r.json() as Promise<FSData>);
const embed = new client.embed();
({
players: async()=>{
@ -243,7 +243,7 @@ export default {
.setName('server')
.setDescription('The server to update')
.setRequired(true)
.setChoices(serverChoices[0]))
.setChoices(serverChoices[0], serverChoices[1]))
.addStringOption(x=>x
.setName('address')
.setDescription('The URL to the dedicated-server-stats.json file')

View File

@ -3,7 +3,7 @@ interface TServer {
code: string
}
import Discord from 'discord.js';
import TClient from '../client';
import TClient from '../client.js';
import FormatPlayer from '../helpers/FormatPlayer.js';
import LogPrefix from '../helpers/LogPrefix.js';
import {writeFileSync, readFileSync} from 'node:fs';
@ -18,20 +18,22 @@ export default async(client:TClient, Channel:string, Message:string, Server:TSer
const genericEmbed = new client.embed();
const HITALL = async()=>{
let sessionInit = {signal: AbortSignal.timeout(8200),headers:{'User-Agent':`Daggerbot - HITALL/undici`}};
let sessionInit = {signal: AbortSignal.timeout(8200),headers:{'User-Agent':'Daggerbot - HITALL/undici'}};
try {
const hitDSS = await fetch(Server.ip+'/feed/dedicated-server-stats.json?code='+Server.code, sessionInit).then(r=>r.json() as Promise<FSData>);
const hitCSG = await fetch(Server.ip+'/feed/dedicated-server-savegame.html?code='+Server.code+'&file=careerSavegame', sessionInit).then(async r=>(client.xjs.xml2js(await r.text(), {compact: true}) as any).careerSavegame as FSCareerSavegame);
if (!hitDSS ?? !hitCSG){
if (hitDSS && !hitDSS.slots) return new Error(`${LogPrefix('MPLoop')} DSS failed with unknown slots table for ${client.MPServerCache[ServerName].name}`);
if (hitDSS && !hitCSG) return msg.edit({content: 'No savegame found or autosave has ran.', embeds: [genericEmbed.setColor(client.config.embedColorOrange).setImage(noContentImage)]});
if (hitDSS && !hitDSS.slots) return console.log(`${LogPrefix('MPLoop')} DSS failed with unknown slots table for ${client.MPServerCache[ServerName].name}`);
if (hitDSS && !hitCSG.slotSystem) return msg.edit({content: 'No savegame found or autosave has ran.', embeds: [genericEmbed.setColor(client.config.embedColorOrange).setImage(noContentImage)]});
else return msg.edit({embeds: [serverErrorEmbed]});
}
// Truncate unnecessary parts of the name for the serverLog embed
client.MPServerCache[ServerName].name = hitDSS.server.name === 'Official Daggerwin Game Server' ? 'Daggerwin' : hitDSS.server.name === 'undefined' ? 'undefined' : client.MPServerCache[ServerName].name;
//Second server name is unknown, will come back and update this later.
// Truncate unnecessary parts of the name for the MPServerCache
// This is a mess, but it works.
for (const filter of ['Official Daggerwin Game Server', 'Daggerwin Multifarm']) {
if (hitDSS.server.name.includes(filter)) client.MPServerCache[ServerName].name = ['Daggerwin', 'DagMF'][['Official Daggerwin Game Server', 'Daggerwin Multifarm'].indexOf(filter)];
}
//Timescale formatting
function formatTimescale(number:number,digits:number,icon:string){
@ -86,10 +88,10 @@ export default async(client:TClient, Channel:string, Message:string, Server:TSer
msg.edit({content:'This embed updates every 35 seconds.',embeds:[serverDetails, playersEmbed]});
}
} catch(err) {
msg.edit({content: null, embeds: [new client.embed().setColor(client.config.embedColorRed).setTitle('Host did not respond back in time')]});
console.log(client.logTime(), `Failed to make a request for ${ServerName}`)
if (err.message === 'The operation was aborted due to timeout') return msg.edit({content: 'Connection timed out.', embeds: [serverErrorEmbed]});
msg.edit({content: null, embeds: [serverErrorEmbed]});
console.log(client.logTime(), `Failed to make a request for ${ServerName}:`, err.message)
}
}
HITALL();
// Hit dem servers in the head every 35 seconds.
}