From 3283013cafd7f3a3b5fb2f7fbb832ed2722d4dad Mon Sep 17 00:00:00 2001 From: toast-ts <96593068+toast-ts@users.noreply.github.com> Date: Sat, 19 Nov 2022 21:17:04 +1100 Subject: [PATCH] some mp improvements and other stuff --- src/commands/fsurl.ts | 19 +++++++------------ src/commands/mp.ts | 9 +++++---- src/events/messageCreate.ts | 6 +++--- src/index.ts | 6 ++++-- src/models/MPServer.ts | 7 +++++++ 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/commands/fsurl.ts b/src/commands/fsurl.ts index 72f6e8b..4532cf6 100644 --- a/src/commands/fsurl.ts +++ b/src/commands/fsurl.ts @@ -3,7 +3,6 @@ import { TClient } from 'src/client'; import MPDB from '../models/MPServer'; export default { async run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){ - if (interaction.guildId !== client.config.mainServer.id) return interaction.reply({content: 'This command doesn\'t work in this server.', ephemeral: true}); 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(); @@ -15,17 +14,12 @@ export default { const Url = await MPDB.findOne({where: {serverId: newServerId}}) if (Url.ip && Url.code){return interaction.reply(`${Url.get('ip')}` + '/feed/dedicated-server-stats.json?code=' + `${Url.get('code')}`)} } catch(err) { - if (err.name == 'SequelizeDatabaseError'){ - console.log('MPDB | File doesn\'t exist in database folder, generating one now.') - interaction.reply('`MPDB.dat` is being createdm run the command again.') - } else { console.log(`MPDB | Error: ${err}`) - interaction.reply('Database error:\nTry inserting an URL first.') - } + interaction.reply('**Database error:**\nTry inserting an URL first.') } } else { const verifyURL = address.match(/feed/) - if (!verifyURL) return interaction.reply('Invalid URL, try again.') + if (!verifyURL) return interaction.reply('That URL is not a valid `dedicated-server-stats.xml`') const convertURL = address const newURL = convertURL.replace('xml','json').split('/feed/dedicated-server-stats.json?code=') try { @@ -33,12 +27,14 @@ export default { const Url = await MPDB.create({ serverId: newServerId, ip: newURL[0], - code: newURL[1] + code: newURL[1], + timesUpdated: 0 }); return interaction.reply(`Successfully set the URL to ${Url.ip}`) } catch(err) { if (err.name == 'SequelizeUniqueConstraintError'){ const AffectedRows = await MPDB.update({ip: newURL[0], code: newURL[1]},{where:{serverId: newServerId}}); + await MPDB.increment('timesUpdated', {where: {serverId: newServerId}}); if (AffectedRows){return interaction.reply(`Successfully updated the URL to ${newURL[0]}`)} } else { console.log(err) @@ -50,9 +46,8 @@ export default { }, data: new SlashCommandBuilder() .setName('url') - .setDescription('Update the URL for FSMP functions') + .setDescription('View the URL for this server\'s FSMP server or update the URL') .addStringOption((opt)=>opt .setName('address') - .setDescription('Insert a \'dedicated-server-stats\' url') - .setRequired(false)) + .setDescription('Insert a \'dedicated-server-stats\' url')) } \ No newline at end of file diff --git a/src/commands/mp.ts b/src/commands/mp.ts index 3c59fff..94210eb 100644 --- a/src/commands/mp.ts +++ b/src/commands/mp.ts @@ -7,11 +7,12 @@ async function MPdata(client:TClient, interaction:Discord.ChatInputCommandIntera MPDB.sync(); const newServerId = interaction.guildId; const ServerURL = await MPDB.findOne({where: { serverId: newServerId }}); + if (!ServerURL) return interaction.reply(`No gameserver found, please contact <@&${client.config.mainServer.roles.mpmanager}> to add it.`) const DBURL = ServerURL.ip const DBCode = ServerURL.code - const verifyURL = DBURL.match(/http/) + const verifyURL = DBURL.match(/http/) const completedURL = DBURL + '/feed/dedicated-server-stats.json?code=' + DBCode - if (!verifyURL) return interaction.reply(`No gameserver found, please contact <@&${client.config.mainServer.roles.mpmanager}> to update it.`) + if (!verifyURL) return interaction.reply(`Invalid gameserver IP, please contact <@&${client.config.mainServer.roles.mpmanager}> to update it.`) // Fetch dss try { @@ -209,7 +210,7 @@ export default { const Image = new client.attachmentBuilder(img.toBuffer(),{name: 'FSStats.png'}) embed1.setImage('attachment://FSStats.png') const FSserver1 = await MPdata(client, interaction, embed1) - embed1.setTitle(FSserver1.data.server.name.length == 0 ? 'Offline' : FSserver1?.data.server.name) + embed1.setTitle(FSserver1?.data.server.name.length == 0 ? 'Offline' : FSserver1?.data.server.name) .setDescription(`${FSserver1?.data.slots.used}/${FSserver1?.data.slots.capacity}`) .setColor(FSserver1?.data.server.name.length == 0 ? client.config.embedColorRed : client.config.embedColor); FSserver1?.data.slots.players.filter(x=>x.isUsed).forEach(player=>{ @@ -222,7 +223,7 @@ export default { const FSserver2 = await MPdata(client, interaction, embed2) const DBURL = MPDB.findOne({where: {serverId: interaction.guildId}}) embed2.setDescription([ - `**Server name**: ${FSserver2?.data.server.name.length == 0 ? 'Unknown Server' : `\`${FSserver2.data.server.name}\``}`, + `**Server name**: \`Official Daggerwin Game Server\``, '**Password:** `mf4700`', '**Crossplay server**', `**Map:** ${FSserver2.data.server.mapName.length == 0 ? 'Null Island' : FSserver2.data.server.mapName}`, diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index 408f9a1..c707035 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -6,7 +6,7 @@ export default { if (message.author.bot) return; if (message.channel.type === ChannelType.DM) return; const msgarr = message.content.toLowerCase().split(' '); - let automodded: any; + let automodded: boolean; function onTimeout(){ delete client.repeatedMessages[message.author.id] @@ -19,7 +19,7 @@ export default { if (client.bannedWords._content.some((x)=>msgarr.includes(x)) && !message.member.roles.cache.has(client.config.mainServer.roles.dcmod) && message.guildId == client.config.mainServer.id && !Whitelist.includes(message.channelId) && client.config.botSwitches.automod){ automodded = true; message.delete().catch((err)=>{ - console.log('bannedWords automod; msg got possibly deleted by another bot or moderator.') + console.log('bannedWords automod; msg got possibly deleted by another bot.') }) message.channel.send('That word is banned here.').then((x)=>setTimeout(()=>x.delete(), 5000)); if (client.repeatedMessages[message.author.id]){ @@ -58,7 +58,7 @@ export default { if (message.content.toLowerCase().includes('discord.gg/') && !message.member.roles.cache.has(client.config.mainServer.roles.dcmod)) { automodded = true; message.delete().catch((err)=>{ - console.log('advertisement automod; msg got possibly deleted by another bot or moderator.') + console.log('advertisement automod; msg got possibly deleted by another bot.') }) } diff --git a/src/index.ts b/src/index.ts index e766773..dc99267 100644 --- a/src/index.ts +++ b/src/index.ts @@ -36,7 +36,7 @@ client.on('ready', async()=>{ // Handle errors process.on('unhandledRejection', async(error: Error)=>{ console.log(error); - (client.channels.resolve(client.config.mainServer.channels.errors) as Discord.TextChannel).send({embeds: [new client.embed().setColor('#420420').setTitle('Error caught!').setDescription(`**Error:** \`${error.message}\`\n\n**Stack:** \`${`${error.stack}`.slice(0, 2500)}\``)]}) + //(client.channels.resolve(client.config.mainServer.channels.errors) as Discord.TextChannel).send({embeds: [new client.embed().setColor('#420420').setTitle('Error caught!').setDescription(`**Error:** \`${error.message}\`\n\n**Stack:** \`${`${error.stack}`.slice(0, 2500)}\``)]}) }); process.on('uncaughtException', async(error: Error)=>{ console.log(error); @@ -62,9 +62,11 @@ setInterval(async()=>{ const newServerId = client.config.mainServer.id const ServerURL = MPDB.findOne({where: {serverId: newServerId}}) const DBURL = (await ServerURL).ip - const DBCode = (await ServerURL).code // vv todo: strip 'http://' from ServerURL + const DBCode = (await ServerURL).code + const verifyURL = DBURL.match(/http/); const completedURL_DSS = DBURL + '/feed/dedicated-server-stats.json?code=' + DBCode const completedURL_CSG = DBURL + '/feed/dedicated-server-savegame.html?code=' + DBCode + '&file=careerSavegame' + if (!verifyURL) return msg.edit({content: 'Invalid gameserver IP, please update!', embeds: null}) console.log(DBURL + '\n' + DBCode) try { Server = await client.axios.get(completedURL_DSS, {timeout: 4000}) diff --git a/src/models/MPServer.ts b/src/models/MPServer.ts index 0c80e66..711c7ac 100644 --- a/src/models/MPServer.ts +++ b/src/models/MPServer.ts @@ -9,6 +9,7 @@ class MPDB extends Model, InferCreationAttributes>{ declare serverId: string | null; declare ip: string | null; declare code: string | null; + declare timesUpdated: number | null; } MPDB.init({ serverId: { @@ -24,6 +25,12 @@ MPDB.init({ type: DataTypes.STRING, defaultValue: 'Missing Code', allowNull: false + }, + timesUpdated: { + type: DataTypes.INTEGER, + defaultValue: 0, + allowNull: false } }, { sequelize: db, modelName: 'urls', timestamps: false }); +MPDB.sync(); export default MPDB \ No newline at end of file