Skip to content

Commit

Permalink
The quick and easy Discord.js upgrade (#350)
Browse files Browse the repository at this point in the history
* discord.js upgrade

* discord.js upgrade

* rename for discord.js message creation event

* fix generate message return

* fix generate message return

* parse embed color strings to integers

* add message event to discord client which mirrors messageCreate

* fix converting string to color

* fix passing other variables through an embed

---------

Co-authored-by: Marek <[email protected]>
  • Loading branch information
steelskillet and werewolfboy13 authored Aug 6, 2024
1 parent a419e53 commit 5e06b9a
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 50 deletions.
14 changes: 12 additions & 2 deletions squad-server/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

import Discord from 'discord.js';
import {Client, Events, GatewayIntentBits} from 'discord.js';
import sequelize from 'sequelize';
import AwnAPI from './utils/awn-api.js';

Expand Down Expand Up @@ -103,8 +103,18 @@ export default class SquadServerFactory {
Logger.verbose('SquadServerFactory', 1, `Starting ${type} connector ${connectorName}...`);

if (type === 'discord') {
const connector = new Discord.Client();
const connector = new Client({intents:[
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
]});
connector.once(Events.ClientReady, readyClient => {console.log(`Ready! Logged in as ${readyClient.user.tag}`);});
await connector.login(connectorConfig);
// setup compatability with older plugins for message create event.
connector.on('messageCreate', message=>{
connector.emit('message', message);
});
return connector;
}

Expand Down
2 changes: 1 addition & 1 deletion squad-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"axios": "^0.21.1",
"core": "1.0.0",
"didyoumean": "^1.2.1",
"discord.js": "^12.3.1",
"discord.js": "^14.14.1",
"gamedig": "^2.0.20",
"graphql": "^15.4.0",
"graphql-request": "^3.4.0",
Expand Down
4 changes: 2 additions & 2 deletions squad-server/plugins/discord-base-message-updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ export default class DiscordBaseMessageUpdater extends BasePlugin {
}

async mount() {
this.options.discordClient.on('message', this.onDiscordMessage);
this.options.discordClient.on('messageCreate', this.onDiscordMessage);
}

async unmount() {
this.options.discordClient.removeEventListener('message', this.onDiscordMessage);
this.options.discordClient.removeEventListener('messageCreate', this.onDiscordMessage);
}

async generateMessage() {
Expand Down
6 changes: 5 additions & 1 deletion squad-server/plugins/discord-base-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ export default class DiscordBasePlugin extends BasePlugin {
return;
}

if (typeof message === 'object' && 'embed' in message)
if (typeof message === 'object' && 'embed' in message) {
message.embed.footer = message.embed.footer || { text: COPYRIGHT_MESSAGE };
if (typeof message.embed.color === 'string')
message.embed.color = parseInt(message.embed.color,16);
message = {...message, embeds:[message.embed]};
}

await this.channel.send(message);
}
Expand Down
4 changes: 2 additions & 2 deletions squad-server/plugins/discord-placeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ export default class DiscordPlaceholder extends BasePlugin {
}

async mount() {
this.options.discordClient.on('message', this.onMessage);
this.options.discordClient.on('messageCreate', this.onMessage);
}

async unmount() {
this.options.discordClient.removeEventListener('message', this.onMessage);
this.options.discordClient.removeEventListener('messageCreate', this.onMessage);
}

async onMessage(message) {
Expand Down
4 changes: 2 additions & 2 deletions squad-server/plugins/discord-rcon.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ export default class DiscordRcon extends BasePlugin {
}

async mount() {
this.options.discordClient.on('message', this.onMessage);
this.options.discordClient.on('messageCreate', this.onMessage);
}

async unmount() {
this.options.discordClient.removeEventListener('message', this.onMessage);
this.options.discordClient.removeEventListener('messageCreate', this.onMessage);
}

async onMessage(message) {
Expand Down
71 changes: 33 additions & 38 deletions squad-server/plugins/discord-server-status.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Discord from 'discord.js';
import tinygradient from 'tinygradient';

import { COPYRIGHT_MESSAGE } from '../utils/constants.js';
Expand Down Expand Up @@ -55,10 +54,6 @@ export default class DiscordServerStatus extends DiscordBaseMessageUpdater {
}

async generateMessage() {
const embed = new Discord.MessageEmbed();

// Set embed title.
embed.setTitle(this.server.serverName);

// Set player embed field.
let players = '';
Expand All @@ -70,41 +65,14 @@ export default class DiscordServerStatus extends DiscordBaseMessageUpdater {
players += ` / ${this.server.publicSlots}`;
if (this.server.reserveSlots > 0) players += ` (+${this.server.reserveSlots})`;

embed.addField('Players', players);

// 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://squad-data.nyc3.cdn.digitaloceanspaces.com/main/${this.server.currentLayer.layerid}.jpg`
: undefined
);

// Set timestamp.
embed.setTimestamp(new Date());

// Set footer.
embed.setFooter(COPYRIGHT_MESSAGE);
const layerName = this.server.currentLayer ? this.server.currentLayer.name : (await this.server.rcon.getCurrentMap()).layer;

// Clamp the ratio between 0 and 1 to avoid tinygradient errors.
const ratio = this.server.a2sPlayerCount / (this.server.publicSlots + this.server.reserveSlots);
const clampedRatio = Math.min(1, Math.max(0, ratio));

// Set gradient embed color.
embed.setColor(
const color =
parseInt(
tinygradient([
{ color: '#ff0000', pos: 0 },
Expand All @@ -114,10 +82,37 @@ export default class DiscordServerStatus extends DiscordBaseMessageUpdater {
.rgbAt(clampedRatio)
.toHex(),
16
)
);
);

const embedobj = {
title: this.server.serverName,
fields: [
{
name: 'Players',
value: players
},
{
name: 'Current Layer',
value: `\`\`\`${layerName || 'Unknown'}\`\`\``,
inline: true
},
{
name: 'Next Layer',
value: `\`\`\`${
this.server.nextLayer?.name || (this.server.nextLayerToBeVoted ? 'To be voted' : 'Unknown')
}\`\`\``,
inline: true
}
],
color: color,
footer: {text:COPYRIGHT_MESSAGE},
timestamp: new Date(),
image: {
url: (this.server.currentLayer ? `https://squad-data.nyc3.cdn.digitaloceanspaces.com/main/${this.server.currentLayer.layerid}.jpg` : undefined)
},
}

return embed;
return { embeds: [embedobj] };
}

async updateStatus() {
Expand All @@ -127,7 +122,7 @@ export default class DiscordServerStatus extends DiscordBaseMessageUpdater {
`(${this.server.a2sPlayerCount}/${this.server.publicSlots}) ${
this.server.currentLayer?.name || 'Unknown'
}`,
{ type: 'WATCHING' }
{ type: 4 }
);
}
}
4 changes: 2 additions & 2 deletions squad-server/plugins/discord-subsystem-restarter.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ export default class DiscordSubsystemRestarter extends BasePlugin {
}

async mount() {
this.options.discordClient.on('message', this.onMessage);
this.options.discordClient.on('messageCreate', this.onMessage);
}

async unmount() {
this.options.discordClient.removeEventListener('message', this.onMessage);
this.options.discordClient.removeEventListener('messageCreate', this.onMessage);
}

async onMessage(message) {
Expand Down

0 comments on commit 5e06b9a

Please sign in to comment.