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

Overhaul '/mp url' for new database.

This commit is contained in:
AnxietyisReal 2023-02-27 10:45:56 +11:00
parent 192f63b8c4
commit 370db5e953
4 changed files with 74 additions and 107 deletions

View File

@ -8,7 +8,7 @@ import bannedWords from './models/bannedWords';
import userLevels from './models/userLevels'; import userLevels from './models/userLevels';
import punishments from './models/punishments'; import punishments from './models/punishments';
import bonkCount from './models/bonkCount'; import bonkCount from './models/bonkCount';
import MPDB from './models/MPServer'; import MPServer from './models/MPServer';
import axios from 'axios'; import axios from 'axios';
import moment from 'moment'; import moment from 'moment';
import tokens from './tokens.json'; import tokens from './tokens.json';
@ -43,6 +43,7 @@ export default class TClient extends Client {
punishments: punishments; punishments: punishments;
bonkCount: bonkCount; bonkCount: bonkCount;
bannedWords: bannedWords; bannedWords: bannedWords;
MPServer: MPServer;
repeatedMessages: repeatedMessages; repeatedMessages: repeatedMessages;
statsGraph: number; statsGraph: number;
@ -81,12 +82,12 @@ export default class TClient extends Client {
this.bonkCount = new bonkCount(this); this.bonkCount = new bonkCount(this);
this.punishments = new punishments(this); this.punishments = new punishments(this);
this.bannedWords = new bannedWords(this); this.bannedWords = new bannedWords(this);
this.MPServer = new MPServer(this);
this.repeatedMessages = {}; this.repeatedMessages = {};
this.setMaxListeners(80); this.setMaxListeners(80);
this.statsGraph = -60; this.statsGraph = -60;
} }
async init(){ async init(){
MPDB.sync();
mongoose.set('strictQuery', true); mongoose.set('strictQuery', true);
await mongoose.connect(this.tokens.mongodb_uri, { await mongoose.connect(this.tokens.mongodb_uri, {
autoIndex: true, autoIndex: true,

View File

@ -1,30 +1,28 @@
import Discord,{EmbedBuilder, SlashCommandBuilder} from 'discord.js'; import Discord,{SlashCommandBuilder} from 'discord.js';
import MPDB from '../models/MPServer';
import TClient from 'src/client'; import TClient from 'src/client';
import path from 'node:path'; import path from 'node:path';
import canvas from 'canvas'; import canvas from 'canvas';
import fs from 'node:fs'; import fs from 'node:fs';
async function MPdata(client:TClient, interaction:Discord.ChatInputCommandInteraction, embed: EmbedBuilder) { async function MPdata(client:TClient, interaction:Discord.ChatInputCommandInteraction, embed: Discord.EmbedBuilder) {
let FSserver; let FSserver;
MPDB.sync(); if (!await client.MPServer._content.findOne({_id:interaction.guildId})) return interaction.reply('This server isn\'t linked to the bot.');
if (!await MPDB.findOne({where: {serverId: interaction.guildId}})) return interaction.reply('This server isn\'t linked.') const ServerURL = await client.MPServer._content.findById(interaction.guildId);
const ServerURL = await MPDB.findOne({where: { serverId: interaction.guildId }}); if (!ServerURL) return interaction.reply(`No FS server found, please notify <@&${client.config.mainServer.roles.mpmanager}> to add it.`)
if (!ServerURL) return interaction.reply(`No gameserver found, please contact <@&${client.config.mainServer.roles.mpmanager}> to add it.`) const MPURL = ServerURL.ip
const DBURL = ServerURL.ip const MPCode = ServerURL.code
const DBCode = ServerURL.code const verifyURL = MPURL.match(/http|https/)
const verifyURL = DBURL.match(/http/) const completedURL = MPURL+'/feed/dedicated-server-stats.json?code='+MPCode
const completedURL = DBURL + '/feed/dedicated-server-stats.json?code=' + DBCode if (!verifyURL) return interaction.reply(`The server IP for this server is currently invalid, please notify <@&${client.config.mainServer.roles.mpmanager}>`)
if (!verifyURL) return interaction.reply(`Invalid gameserver IP, please contact <@&${client.config.mainServer.roles.mpmanager}> to update it.`)
// Fetch dss // Fetch dss
try { // v I am aware timeout has decreased from 2800 to 2588 to fit within Discord's interaction timeouts (3s) -Toast try { // v I am aware timeout has decreased from 2800 to 2588 to fit within Discord's interaction timeouts (3s) -Toast
FSserver = await client.axios.get(completedURL, {timeout: 2588, headers: {'User-Agent': `Daggerbot - mp cmd/axios ${client.axios.VERSION}`}}) // Finally got around to fixing the command when it cannot ping the host. FSserver = await client.axios.get(completedURL, {timeout: 2588, headers: {'User-Agent': `Daggerbot - mp cmd/axios ${client.axios.VERSION}`}})
} catch(err) { } catch(err) {
// Blame Nawdic & RedRover92 // Blame Nawdic & RedRover92
embed.setTitle('Host is not responding.'); embed.setTitle('Host is not responding.');
embed.setColor(client.config.embedColorRed); embed.setColor(client.config.embedColorRed);
console.log(client.logTime, 'dag mp fail to fetch, host is not responding.'); console.log(client.logTime(), 'DagMP failed to fetch, host didn\'t respond in time.');
return interaction.reply('Server didn\'t respond in time.'); return interaction.reply('Server didn\'t respond in time.');
} }
return FSserver return FSserver
@ -33,9 +31,7 @@ async function MPdata(client:TClient, interaction:Discord.ChatInputCommandIntera
export default { export default {
async run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){ async run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){
if (interaction.channelId == '468835769092669461' && !client.isStaff(interaction.member) && ['status', 'players'].includes(interaction.options.getSubcommand())) { if (interaction.channelId == '468835769092669461' && !client.isStaff(interaction.member) && ['status', 'players'].includes(interaction.options.getSubcommand())) {
interaction.reply(`Please use <#739084625862852715> for \`/mp status/players\` commands to prevent clutter in this channel.`).then((msg)=>{ interaction.reply(`Please use <#739084625862852715> for \`/mp status/players\` commands to prevent clutter in this channel.`).then((msg)=>{setTimeout(()=>{interaction.deleteReply()}, 6000)});
setTimeout(()=>{interaction.deleteReply()}, 6000)
});
return; return;
} }
({ ({
@ -53,43 +49,38 @@ export default {
{name: 'In-game Time', value: `${('0' + Math.floor((FSserver0.data.server.dayTime/3600/1000))).slice(-2)}:${('0' + Math.floor((FSserver0.data.server.dayTime/60/1000)%60)).slice(-2)}`, inline: true} {name: 'In-game Time', value: `${('0' + Math.floor((FSserver0.data.server.dayTime/3600/1000))).slice(-2)}:${('0' + Math.floor((FSserver0.data.server.dayTime/60/1000)%60)).slice(-2)}`, inline: true}
) )
interaction.reply({embeds: [embed0]}) interaction.reply({embeds: [embed0]})
} else if (FSserver0.data.server.name.length == 0) { } else if (FSserver0.data.server.name.length == 0) interaction.reply('Server is currently offline.')
interaction.reply('Server is currently offline.')
}
} catch (err){ } catch (err){
console.log(err) console.log(err)
interaction.reply('FSserver0 Placeholder') interaction.reply('FSserver0 Error placeholder')
}; };
}, },
info: async()=>{ info: async()=>{
const embed2 = new client.embed().setColor(client.config.embedColor) const embed2 = new client.embed().setColor(client.config.embedColor)
const FSserver2 = await MPdata(client, interaction, embed2) const FSserver2 = await MPdata(client, interaction, embed2)
if (!FSserver2?.data) return console.log('FSserver2 failed - info') if (!FSserver2?.data) return console.log('FSserver2 failed - info')
const DBURL = MPDB.findOne({where: {serverId: interaction.guildId}}) const MPURL = await client.MPServer._content.findById(interaction.guildId);
embed2.setDescription([ embed2.setDescription([
`**Server name**: \`${FSserver2?.data.server.name.length == 0 ? '\u200b' : FSserver2?.data.server.name}\``, `**Server name**: \`${FSserver2?.data.server.name.length == 0 ? '\u200b' : FSserver2?.data.server.name}\``,
'**Password:** `mf4700`', '**Password:** `mf4700`',
'**Crossplay server**', '**Crossplay server**',
`**Map:** ${FSserver2.data.server.mapName.length == 0 ? 'Null Island' : FSserver2.data.server.mapName}`, `**Map:** ${FSserver2.data.server.mapName.length == 0 ? 'Null Island' : FSserver2.data.server.mapName}`,
`**Mods:** [Click here](${(await DBURL).ip}/mods.html) **|** [Direct Download](${(await DBURL).ip}/all_mods_download?onlyActive=true)`, `**Mods:** [Click here](${MPURL.ip}/mods.html) **|** [Direct Download](${MPURL.id}/all_mods_download?onlyActive=true)`,
'**Filters:** [Click here](https://discord.com/channels/468835415093411861/468835769092669461/926581585938120724)', '**Filters:** [Click here](https://discord.com/channels/468835415093411861/468835769092669461/926581585938120724)',
'Please see <#543494084363288637> for additional information.' 'Please see <#543494084363288637> for additional information.'
].join('\n')); ].join('\n'));
if (FSserver2?.data.server.name.length == 0){ if (FSserver2?.data.server.name.length == 0) embed2.setFooter({text: 'Server is currently offline.'})
embed2.setFooter({text: 'Server is currently offline.'})
}
interaction.reply({embeds: [embed2]}) interaction.reply({embeds: [embed2]})
}, },
url: async()=>{ url: async()=>{
if (!interaction.member.roles.cache.has(client.config.mainServer.roles.mpmanager) && !interaction.member.roles.cache.has(client.config.mainServer.roles.bottech) && !interaction.member.roles.cache.has(client.config.mainServer.roles.admin)) return client.youNeedRole(interaction, 'mpmanager'); if (!interaction.member.roles.cache.has(client.config.mainServer.roles.mpmanager) && !interaction.member.roles.cache.has(client.config.mainServer.roles.bottech) && !interaction.member.roles.cache.has(client.config.mainServer.roles.admin)) return client.youNeedRole(interaction, 'mpmanager');
MPDB.sync();
const address = interaction.options.getString('address'); const address = interaction.options.getString('address');
if (!address){ if (!address){
try { try {
const Url = await MPDB.findOne({where:{serverId: interaction.guildId}}) const Url = await client.MPServer._content.findById(interaction.guildId);
if (Url.ip && Url.code){return interaction.reply(`${Url.get('ip')}` + '/feed/dedicated-server-stats.json?code=' + `${Url.get('code')}`)} if (Url.ip && Url.code) return interaction.reply(`${Url.get('ip')}`+'/feed/dedicated-server-stats.json?code='+`${Url.get('code')}`)
} catch(err){ } catch(err){
console.log(`MPDB | ${err}`) console.log(`MPDB :: ${err}`)
interaction.reply('**Database error:**\nTry inserting an URL first.') interaction.reply('**Database error:**\nTry inserting an URL first.')
} }
}else{ }else{
@ -97,23 +88,13 @@ export default {
if (!verifyURL) return interaction.reply('The URL does not match `dedicated-server-stats.xml`') if (!verifyURL) return interaction.reply('The URL does not match `dedicated-server-stats.xml`')
const newURL = address.replace('xml','json').split('/feed/dedicated-server-stats.json?code=') const newURL = address.replace('xml','json').split('/feed/dedicated-server-stats.json?code=')
try{ try{
console.log(`MPDB | URL for ${interaction.guild.name} has been updated by ${interaction.member.displayName} (${interaction.member.id})`); console.log(`MPDB :: URL for ${interaction.guild.name} has been updated by ${interaction.member.displayName} (${interaction.member.id})`);
const Url = await MPDB.create({ await client.MPServer._content.create({_id: interaction.guildId, ip: newURL[0], code: newURL[1], timesUpdated: 0})
serverId: interaction.guildId, return interaction.reply('This server is now linked and URL has been added.');
ip: newURL[0],
code: newURL[1],
timesUpdated: 0
});
return interaction.reply(`Successfully set the URL to ${Url.ip}`)
} catch(err){ } catch(err){
if (err.name == 'SequelizeUniqueConstraintError'){ const affectedValues = await client.MPServer._content.findByIdAndUpdate({_id: interaction.guildId}, {ip: newURL[0], code: newURL[1]});
const AffectedRows = await MPDB.update({ip: newURL[0], code: newURL[1]},{where:{serverId: interaction.guildId}}); await client.MPServer._increment(interaction.guildId);
await MPDB.increment('timesUpdated',{where:{serverId: interaction.guildId}}); if (affectedValues) return interaction.reply('URL successfully updated.')
if (AffectedRows) return interaction.reply(`Successfully updated the URL to ${newURL[0]}`)
}else{
console.log(err)
interaction.reply(`\`MPDB\` has caught an error, notify <@&${client.config.mainServer.roles.bottech}>`)
}
} }
} }
}, },

View File

@ -3,7 +3,6 @@ import TClient from './client';
const client = new TClient; const client = new TClient;
client.init(); client.init();
import fs from 'node:fs'; import fs from 'node:fs';
import MPDB from './models/MPServer';
import {FSData, FSCareerSavegame} from './typings/interfaces'; import {FSData, FSCareerSavegame} from './typings/interfaces';
client.on('ready', async()=>{ client.on('ready', async()=>{
@ -44,13 +43,12 @@ setInterval(async()=>{
let error; let error;
// Connect to DB to retrieve the Gameserver info to fetch data. // Connect to DB to retrieve the Gameserver info to fetch data.
MPDB.sync(); const ServerURL = await client.MPServer._content.findById(client.config.mainServer.id);
const ServerURL = MPDB.findOne({where: {serverId: client.config.mainServer.id}}) const MPURL = ServerURL.ip;
const DBURL = (await ServerURL).ip const MPCode = ServerURL.code;
const DBCode = (await ServerURL).code const verifyURL = MPURL.match(/http|https/);
const verifyURL = DBURL.match(/http/); const completedURL_DSS = MPURL+'/feed/dedicated-server-stats.json?code='+MPCode;
const completedURL_DSS = DBURL + '/feed/dedicated-server-stats.json?code=' + DBCode const completedURL_CSG = MPURL+'/feed/dedicated-server-savegame.html?code='+MPCode+'&file=careerSavegame';
const completedURL_CSG = DBURL + '/feed/dedicated-server-savegame.html?code=' + DBCode + '&file=careerSavegame'
const FSdss = { const FSdss = {
data: {} as FSData, data: {} as FSData,
fetchResult: '' as string fetchResult: '' as string
@ -59,30 +57,27 @@ setInterval(async()=>{
data: {} as FSCareerSavegame, data: {} as FSCareerSavegame,
fetchResult: '' as string fetchResult: '' as string
}; };
if (!verifyURL) return msg.edit({content: 'Invalid gameserver IP, please update!', embeds: null}) if (!verifyURL) return msg.edit({content: '*Detected an invalid IP.*', embeds: null})
async function serverData(client:TClient, URL: string){ async function serverData(client:TClient, URL: string){
return await client.axios.get(URL, {timeout: 4000, maxContentLength: Infinity, headers: {'User-Agent': `Daggerbot/axios ${client.axios.VERSION}`}}).catch((error:Error)=>error.message) return await client.axios.get(URL, {timeout: 4000, maxContentLength: Infinity, headers: {'User-Agent': `Daggerbot/axios ${client.axios.VERSION}`}}).catch((error:Error)=>error.message)
} }
await Promise.all([serverData(client, completedURL_DSS), serverData(client, completedURL_CSG)]).then(function(results){ await Promise.all([serverData(client, completedURL_DSS), serverData(client, completedURL_CSG)]).then(function(results){
if (typeof results[0] == 'string'){ if (typeof results[0] == 'string'){
FSdss.fetchResult = `dag mp dss fail, ${results[0]}`; FSdss.fetchResult = `DagMP DSS failed, ${results[0]}`;
embed.addFields({name: 'DSS Status', value: results[0]}) embed.addFields({name: 'DSS Status', value: results[0]})
} else if (results[0].status != 200){ } else if (results[0].status != 200){
FSdss.fetchResult = `dag mp dss fail with ${results[0].status + ' ' + results[0].statusText}`; FSdss.fetchResult = `DagMP DSS failed with ${results[0].status + ' ' + results[0].statusText}`;
embed.addFields({name: 'DSS Status', value: results[0].status + ' ' + results[0].statusText}) embed.addFields({name: 'DSS Status', value: results[0].status + ' ' + results[0].statusText})
} else { } else FSdss.data = results[0].data as FSData
FSdss.data = results[0].data as FSData
}
if (typeof results[1] == 'string'){ if (typeof results[1] == 'string'){
FScsg.fetchResult = `dag mp csg fail, ${results[1]}`; FScsg.fetchResult = `DagMP CSG failed, ${results[1]}`;
embed.addFields({name: 'CSG Status', value: results[1]}) embed.addFields({name: 'CSG Status', value: results[1]})
} else if (results[1].status != 200){ } else if (results[1].status != 200){
if (results[1].status == 204){embed.setImage('https://http.cat/204')} if (results[1].status == 204) embed.setImage('https://http.cat/204');
FScsg.fetchResult = `dag mp csg fail with ${results[1].status + ' ' + results[1].statusText}`; FScsg.fetchResult = `DagMP CSG failed with ${results[1].status + ' ' + results[1].statusText}`;
embed.addFields({name: 'CSG Status', value: results[1].status + ' ' + results[1].statusText}) embed.addFields({name: 'CSG Status', value: results[1].status + ' ' + results[1].statusText})
} else { } else FScsg.data = client.xjs.xml2js(results[1].data,{compact:true,spaces:2}).careerSavegame as FSCareerSavegame;
FScsg.data = client.xjs.xml2js(results[1].data,{compact:true,spaces:2}).careerSavegame as FSCareerSavegame;
}
}).catch((error)=>console.log(error)) }).catch((error)=>console.log(error))
if (FSdss.fetchResult.length != 0){ if (FSdss.fetchResult.length != 0){
error = true; error = true;

View File

@ -1,35 +1,25 @@
import {Sequelize, DataTypes, Model, InferAttributes, InferCreationAttributes} from 'sequelize'; import TClient from 'src/client';
var db = new Sequelize('database', 'daggerbot', 'toastsus', { import mongoose from 'mongoose';
host: 'localhost',
dialect: 'sqlite', const Schema = mongoose.model('mpserver', new mongoose.Schema({
logging: false, _id: {type: String, required:true},
storage: 'src/database/MPDB.dat' ip: {type: String},
}) code: {type: String},
class MPDB extends Model<InferAttributes<MPDB>, InferCreationAttributes<MPDB>>{ timesUpdated: {type: Number, required: true}
declare serverId: string | null; }, {versionKey: false}));
declare ip: string | null;
declare code: string | null; export default class MPServer extends Schema {
declare timesUpdated: number | null; client: TClient;
_content: typeof Schema;
constructor(client:TClient){
super();
this.client = client;
this._content = Schema;
}
async _increment(serverId: string){
const server = await this._content.findById(serverId)
if (server) await this._content.findByIdAndUpdate(server, {timesUpdated: server.timesUpdated + 1})
else await this._content.create({_id:serverId, timesUpdated: 1})
//console.log(`[${serverId}] :: timesUpdated value incremented`)
} }
MPDB.init({
serverId: {
type: DataTypes.STRING,
unique: true
},
ip: {
type: DataTypes.STRING,
defaultValue: 'Missing IP',
allowNull: false
},
code: {
type: DataTypes.STRING,
defaultValue: 'Missing Code',
allowNull: false
},
timesUpdated: {
type: DataTypes.INTEGER,
defaultValue: 0,
allowNull: false
} }
}, { sequelize: db, modelName: 'urls', timestamps: false });
export default MPDB