From 96444a4c027030dd4322e490b422f164c472d88a Mon Sep 17 00:00:00 2001 From: AnxietyisReal <96593068+AnxietyisReal@users.noreply.github.com> Date: Sat, 19 Aug 2023 10:54:17 +1000 Subject: [PATCH] After few trial and errors, I finally got it!! --- src/MPLoop.ts | 72 +++++++++++++++++-------------------------------- src/config.json | 4 +-- src/index.ts | 7 +++-- 3 files changed, 29 insertions(+), 54 deletions(-) diff --git a/src/MPLoop.ts b/src/MPLoop.ts index 766934b..2a13408 100644 --- a/src/MPLoop.ts +++ b/src/MPLoop.ts @@ -1,68 +1,44 @@ +interface TServer { + ip: string + code: string +} import Discord from 'discord.js'; import TClient from './client'; import {writeFileSync, readFileSync} from 'node:fs'; import {FSPlayer, FSData, FSCareerSavegame} from './typings/interfaces'; -export default async(client:TClient, Channel:string, Message:string, ServerName:string)=>{ +export default async(client:TClient, Channel:string, Message:string, Server:TServer)=>{ if (!client.config.botSwitches.mpstats) return; - + let MPLoopPrefix = '[MPLoop] '; + let noContentImage = 'https://cdn.discordapp.com/attachments/1118960531135541318/1140906691236479036/68efx1.png'; const msg = await (client.channels.resolve(Channel) as Discord.TextChannel).messages.fetch(Message); - const database = await client.MPServer._content.findById(client.config.mainServer.id); - const servers = { - main: { - dss: database.mainServer.ip+'/feed/dedicated-server-stats.json?code='+database.mainServer.code, - csg: database.mainServer.ip+'/feed/dedicated-server-savegame.html?code='+database.mainServer.code+'&file=careerSavegame' - }, - second: { - dss: database.secondServer.ip+'/feed/dedicated-server-stats.json?code='+database.secondServer.code, - csg: database.secondServer.ip+'/feed/dedicated-server-savegame.html?code='+database.secondServer.code+'&file=careerSavegame' - } - }; + // Log bot uptime for the sake of debugging. (client.channels.resolve('1091300529696673792') as Discord.TextChannel).send(client.formatTime(client.uptime, 2, {longNames: true, commas: true})); const HITALL = async()=>{ - /* const hitDSS = await Promise.all([ - client.axios.get(servers.main.dss,{timeout:7500,maxContentLength:Infinity,headers:{'User-Agent':`Daggerbot - HITALL/axios ${client.axios.VERSION}`}}), - client.axios.get(servers.second.dss,{timeout:7500,maxContentLength:Infinity,headers:{'User-Agent':`Daggerbot - HITALL/axios ${client.axios.VERSION}`}}) - ]).catch(e=>{throw new Error('hitDSS failed to make a request', {cause: e.cause})}); */ - const hitCSG = await Promise.all([ - client.axios.get(servers.main.csg,{timeout:7500,maxContentLength:Infinity,headers:{'User-Agent':`Daggerbot - HITALL/axios ${client.axios.VERSION}`}}), - client.axios.get(servers.second.csg,{timeout:7500,maxContentLength:Infinity,headers:{'User-Agent':`Daggerbot - HITALL/axios ${client.axios.VERSION}`}}) - ]).catch(e=>{throw new Error('hitCSG failed to make a request', {cause: e.cause})}); + let sessionInit = {signal: AbortSignal.timeout(7500),headers:{'User-Agent':`Daggerbot - HITALL/fetch`}}; try { - const APIData = { - 'Daggerwin': { - //dss: hitDSS[0].data as FSData, - csg: (client.xjs.xml2js(hitCSG[0].data,{compact:true}) as any).careerSavegame as FSCareerSavegame - }, - 'SecondServer': { - //dss: hitDSS[1].data as FSData, - csg: (client.xjs.xml2js(hitCSG[1].data,{compact:true}) as any).careerSavegame as FSCareerSavegame - } - } as const; - console.log(APIData['Daggerwin'].csg) - console.log(APIData['SecondServer'].csg) - //console.log((APIData.Daggerwin.dss as FSData).server.name) - //console.log((APIData.Daggerwin.csg as FSCareerSavegame).statistics.money) + const hitDSS = await fetch(Server.ip+'/feed/dedicated-server-stats.json?code='+Server.code, sessionInit).then(r=>r.json() as Promise); + const hitCSG = await fetch(Server.ip+'/feed/dedicated-server-savegame.html?code='+Server.code+'&file=careerSavegame', sessionInit).then(async r=>{ + if (r.status === 204) return msg.edit({embeds: [new client.embed().setImage(noContentImage)]}); + else return (client.xjs.xml2js(await r.text(), {compact: true}) as any).careerSavegame as FSCareerSavegame + }).catch(()=>console.log(client.logTime(), `${MPLoopPrefix}CSG failed for ${Server.ip.replace(/^(https?)(\:\/\/)/, '')}`)); + + if (!hitDSS ?? !hitCSG){ + if (hitDSS && !hitDSS.slots) return new Error(`${MPLoopPrefix}DSS failed for unknown slots table`); + return msg.edit({embeds: [new client.embed().setColor(client.config.embedColorRed).setTitle('Host did not respond back in time')]}); + } + msg.edit({content: [ - ServerName, - (APIData[ServerName].csg as FSCareerSavegame).settings.savegameName._text - ].join('\n')}) + Server.ip.replace(/^(https?)(\:\/\/)/, ''), + hitDSS.server.name.length > 0 ? hitDSS.server.name : 'Offline' + ].join('\n'), embeds: []}) } catch(err) { - msg.edit({content: err.message}) - throw new Error('HITALL failed to make a promise request', {cause: err.cause}); + throw new Error(`${MPLoopPrefix}Failed to make a promise request.`, {cause: err.cause}) } } HITALL(); - - /* await Promise.all([ - client.axios.get(servers.main.dss,{timeout:7500,maxContentLength:Infinity,headers:{'User-Agent':`Daggerbot/axios ${client.axios.VERSION}`}}), - client.axios.get(servers.main.csg,{timeout:7500,maxContentLength:Infinity,headers:{'User-Agent':`Daggerbot/axios ${client.axios.VERSION}`}}), - client.axios.get(servers.second.dss,{timeout:7500,maxContentLength:Infinity,headers:{'User-Agent':`Daggerbot/axios ${client.axios.VERSION}`}}), - client.axios.get(servers.second.csg,{timeout:7500,maxContentLength:Infinity,headers:{'User-Agent':`Daggerbot/axios ${client.axios.VERSION}`}}) - ]).then(x=>x.map(x=>x.data)).catch(()=>{throw new Error('[MPLOOP] Failed to make a promise request.')}); - msg.edit({content: ServerName, embeds: []}) */ } /* export default async(client:TClient,Channel:string,Message:string,ServerName:string)=>{ diff --git a/src/config.json b/src/config.json index 943c242..2da93c0 100644 --- a/src/config.json +++ b/src/config.json @@ -12,11 +12,11 @@ "929807948748832798", "468835415093411861", "1058183358267543552", "549114074273677314" ], "MPStatsLocation": { - "main": { + "mainServer": { "channel": "543494084363288637", "message": "1023699243183112192" }, - "second": { + "secondServer": { "channel": "", "message": "" } diff --git a/src/index.ts b/src/index.ts index 790eb5b..a37b4a4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -68,10 +68,9 @@ if (client.config.botSwitches.music){ // YouTube Upload notification and Daggerwin MP loop setInterval(async()=>{ - console.log(client.logTime(), 'MPLoop Interval') - MPLoop(client, client.config.MPStatsLocation.main.channel, client.config.MPStatsLocation.main.message, 'Daggerwin') - MPLoop(client, client.config.MPStatsLocation.second.channel, client.config.MPStatsLocation.second.message, 'SecondServer') -}, 30000); + const serverlake = (await client.MPServer._content.findById(client.config.mainServer.id)); + for await (const [locName, locArea] of Object.entries(client.config.MPStatsLocation)) await MPLoop(client, locArea.channel, locArea.message, serverlake[locName]) +}, 10000); setInterval(async()=>{ client.YTLoop('UCQ8k8yTDLITldfWYKDs3xFg', 'Daggerwin', '528967918772551702'); // 528967918772551702 = #videos-and-streams client.YTLoop('UCguI73--UraJpso4NizXNzA', 'Machinery Restorer', '767444045520961567') // 767444045520961567 = #machinery-restorer