SquadJS/squad-server/plugins/discord-server-status.js

131 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-10-16 10:57:22 -05:00
import tinygradient from 'tinygradient';
import BasePlugin from './base-plugin.js';
import { COPYRIGHT_MESSAGE } from '../utils/constants.js';
export default class DiscordServerStatus extends BasePlugin {
static get description() {
return (
'The <code>DiscordServerStatus</code> plugin updates a message in Discord with current server information, ' +
'e.g. player count.'
);
}
static get defaultEnabled() {
return false;
}
static get optionsSpecification() {
return {
discordClient: {
required: true,
description: 'Discord connector name.',
connector: 'discord',
default: 'discord'
},
messageIDs: {
required: true,
description: 'ID of messages to update.',
default: [],
example: [{ channelID: '667741905228136459', messageID: '766688383043895387' }]
},
updateInterval: {
required: false,
description: 'How frequently to update the status in Discord.',
default: 60 * 1000
},
disableStatus: {
required: false,
description: 'Disable the bot status.',
default: false
}
};
}
2020-12-03 08:16:07 -06:00
constructor(server, options, connectors) {
super(server, options, connectors);
2020-10-16 10:57:22 -05:00
2020-12-06 15:23:05 -06:00
this.update = this.update.bind(this);
2020-12-03 08:16:07 -06:00
}
2020-12-08 06:13:53 -06:00
async mount() {
2020-12-03 08:16:07 -06:00
this.interval = setInterval(this.update, this.options.updateInterval);
}
2020-10-16 10:57:22 -05:00
2020-12-08 06:13:53 -06:00
async unmount() {
2020-12-03 08:16:07 -06:00
clearInterval(this.interval);
}
async update() {
for (const messageID of this.options.messageIDs) {
try {
const channel = await this.options.discordClient.channels.fetch(messageID.channelID);
const message = await channel.messages.fetch(messageID.messageID);
await message.edit(this.getEmbed());
} catch (err) {
console.log(err);
2020-10-16 10:57:22 -05:00
}
2020-12-03 08:16:07 -06:00
}
2020-10-16 10:57:22 -05:00
2020-12-03 08:16:07 -06:00
await this.options.discordClient.user.setActivity(
`(${this.server.a2sPlayerCount}/${this.server.publicSlots}) ${
2021-02-25 05:59:34 -06:00
this.server.layerHistory[0].layer.name || 'Unknown'
2020-12-03 08:16:07 -06:00
}`,
{ type: 'WATCHING' }
);
2020-10-16 10:57:22 -05:00
}
getEmbed() {
2020-10-16 10:57:22 -05:00
let players = '';
players += `${this.server.a2sPlayerCount}`;
if (this.server.publicQueue + this.server.reserveQueue > 0)
players += ` (+${this.server.publicQueue + this.server.reserveQueue})`;
2020-10-16 10:57:22 -05:00
players += ` / ${this.server.publicSlots}`;
if (this.server.reserveSlots > 0) players += ` (+${this.server.reserveSlots})`;
2020-10-16 10:57:22 -05:00
const fields = [
{
name: 'Players',
value: `\`\`\`${players}\`\`\``
},
{
name: 'Current Layer',
2021-02-03 11:33:01 -06:00
value: `\`\`\`${this.server.currentLayer.name || 'Unknown'}\`\`\``,
2020-10-16 10:57:22 -05:00
inline: true
},
{
name: 'Next Layer',
2021-02-03 11:33:01 -06:00
value: `\`\`\`${
this.server.nextLayer?.name ||
(this.server.nextLayerToBeVoted ? 'To be voted' : 'Unknown')
2021-02-03 11:33:01 -06:00
}\`\`\``,
2020-10-16 10:57:22 -05:00
inline: true
}
];
return {
content: '',
embed: {
title: this.server.serverName,
2020-10-16 10:57:22 -05:00
color: parseInt(
tinygradient([
{ color: '#ff0000', pos: 0 },
{ color: '#ffff00', pos: 0.5 },
{ color: '#00ff00', pos: 1 }
])
2021-01-07 14:52:55 -06:00
.rgbAt(
this.server.a2sPlayerCount / (this.server.publicSlots + this.server.reserveSlots)
)
2020-10-16 10:57:22 -05:00
.toHex(),
16
),
fields: fields,
timestamp: new Date().toISOString(),
footer: { text: COPYRIGHT_MESSAGE }
}
};
}
}