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

130 lines
3.6 KiB
JavaScript
Raw Normal View History

2021-03-09 14:31:33 -06:00
import Discord from 'discord.js';
2020-10-16 10:57:22 -05:00
import tinygradient from 'tinygradient';
2021-03-09 14:31:33 -06:00
2020-10-16 10:57:22 -05:00
import { COPYRIGHT_MESSAGE } from '../utils/constants.js';
2021-03-09 14:31:33 -06:00
import DiscordBaseMessageUpdater from './discord-base-message-updater.js';
export default class DiscordServerStatus extends DiscordBaseMessageUpdater {
2020-10-16 10:57:22 -05:00
static get description() {
2021-03-09 14:31:33 -06:00
return 'The <code>DiscordServerStatus</code> plugin can be used to get the server status in Discord.';
2020-10-16 10:57:22 -05:00
}
static get defaultEnabled() {
2021-03-09 14:31:33 -06:00
return true;
2020-10-16 10:57:22 -05:00
}
static get optionsSpecification() {
return {
2021-03-09 14:31:33 -06:00
...DiscordBaseMessageUpdater.optionsSpecification,
command: {
required: false,
description: 'Command name to get message.',
default: '!status'
2020-10-16 10:57:22 -05:00
},
updateInterval: {
required: false,
2021-03-09 14:31:33 -06:00
description: 'How frequently to update the time in Discord.',
2020-10-16 10:57:22 -05:00
default: 60 * 1000
},
2021-03-09 14:31:33 -06:00
setBotStatus: {
2020-10-16 10:57:22 -05:00
required: false,
2021-03-09 14:31:33 -06:00
description: "Whether to update the bot's status with server information.",
default: true
2020-10-16 10:57:22 -05:00
}
};
}
2020-12-03 08:16:07 -06:00
constructor(server, options, connectors) {
super(server, options, connectors);
2020-10-16 10:57:22 -05:00
2021-03-09 14:31:33 -06:00
this.updateMessages = this.updateMessages.bind(this);
this.updateStatus = this.updateStatus.bind(this);
2020-12-03 08:16:07 -06:00
}
2020-12-08 06:13:53 -06:00
async mount() {
2021-03-09 14:31:33 -06:00
await super.mount();
this.updateInterval = setInterval(this.updateMessages, this.options.updateInterval);
this.updateStatusInterval = setInterval(this.updateStatus, this.options.updateInterval);
2020-12-03 08:16:07 -06:00
}
2020-10-16 10:57:22 -05:00
2020-12-08 06:13:53 -06:00
async unmount() {
2021-03-09 14:31:33 -06:00
await super.unmount();
clearInterval(this.updateInterval);
clearInterval(this.updateStatusInterval);
2020-12-03 08:16:07 -06:00
}
2021-03-09 14:31:33 -06:00
async generateMessage() {
const embed = new Discord.MessageEmbed();
2020-12-03 08:16:07 -06:00
2021-03-09 14:31:33 -06:00
// Set embed title.
embed.setTitle(this.server.serverName);
2020-10-16 10:57:22 -05:00
2021-03-09 14:31:33 -06:00
// Set player embed field.
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
2021-03-09 14:31:33 -06:00
embed.addField('Players', players);
2020-10-16 10:57:22 -05:00
2021-03-09 14:31:33 -06:00
// Set layer embed fields.
embed.addField(
'Current Layer',
`\`\`\`${this.server.currentLayer?.name || 'Unknown'}\`\`\``,
true
);
embed.addField(
'Next Layer',
`\`\`\`${
this.server.nextLayer?.name || (this.server.nextLayerToBeVoted ? 'To be voted' : 'Unknown')
}\`\`\``,
true
);
// Set layer image.
embed.setImage(
this.server.currentLayer
? `https://raw.githubusercontent.com/Squad-Wiki-Editorial/squad-wiki-pipeline-map-data/master/completed_output/_Current%20Version/images/${this.server.currentLayer.classname}.jpg`
: undefined
);
// Set timestamp.
embed.setTimestamp(new Date());
// Set footer.
embed.setFooter(COPYRIGHT_MESSAGE);
// Set gradient embed color.
embed.setColor(
parseInt(
tinygradient([
{ color: '#ff0000', pos: 0 },
{ color: '#ffff00', pos: 0.5 },
{ color: '#00ff00', pos: 1 }
])
.rgbAt(this.server.a2sPlayerCount / (this.server.publicSlots + this.server.reserveSlots))
.toHex(),
16
)
);
return embed;
}
async updateStatus() {
if (!this.options.setBotStatus) return;
await this.options.discordClient.user.setActivity(
`(${this.server.a2sPlayerCount}/${this.server.publicSlots}) ${
this.server.currentLayer?.name || 'Unknown'
}`,
{ type: 'WATCHING' }
);
2020-10-16 10:57:22 -05:00
}
}