1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-09-30 05:01:00 -04:00
Daggerbot-TS/src/commands/fsurl.ts

53 lines
2.8 KiB
TypeScript
Raw Normal View History

import Discord,{SlashCommandBuilder} from 'discord.js';
import { TClient } from 'src/client';
import MPDB from '../models/MPServer';
export default {
async run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){
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 newServerId = interaction.guildId
const address = interaction.options.getString('address')
if (!address) {
try {
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) {
console.log(`MPDB | Error: ${err}`)
2022-11-19 05:17:04 -05:00
interaction.reply('**Database error:**\nTry inserting an URL first.')
}
} else {
const verifyURL = address.match(/feed/)
2022-11-19 05:17:04 -05:00
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 {
console.log(`MPDB | URL for ${interaction.guild.name} has been updated by ${interaction.member.displayName} (${interaction.member.id})`)
const Url = await MPDB.create({
serverId: newServerId,
ip: newURL[0],
2022-11-19 05:17:04 -05:00
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}});
2022-11-19 05:17:04 -05:00
await MPDB.increment('timesUpdated', {where: {serverId: newServerId}});
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}>`)
}
}
}
},
data: new SlashCommandBuilder()
.setName('url')
2022-11-19 05:17:04 -05:00
.setDescription('View the URL for this server\'s FSMP server or update the URL')
.addStringOption((opt)=>opt
.setName('address')
2022-11-19 05:17:04 -05:00
.setDescription('Insert a \'dedicated-server-stats\' url'))
}