This commit is contained in:
Evert Prants 2021-09-03 20:22:33 +03:00
parent 5a70931c82
commit 215215c536
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
5 changed files with 741 additions and 507 deletions

View File

@ -2,8 +2,8 @@
"main": "plugin.js",
"name": "discord",
"description": "Discord Service for Squeebot 3",
"version": "1.2.0",
"version": "1.2.1",
"tags": ["service", "discord"],
"dependencies": ["control?"],
"npmDependencies": ["discord.js@^12.5.1"]
"npmDependencies": ["discord.js@^13.1.0"]
}

View File

@ -7,7 +7,7 @@ import {
import util from 'util';
import Discord from 'discord.js';
import Discord, { Intents } from 'discord.js';
import { logger } from '@squeebot/core/lib/core';
import { thousandsSeparator, timeSince, toHHMMSS } from '@squeebot/core/lib/common';
@ -31,12 +31,12 @@ class DiscordFormatter extends MarkdownFormatter {
let elemValue = elem[1];
const elemParams = elem[2];
if (!elemValue || elemType !== 'field') {
if (!elemValue || (elemType !== 'field' && elemType !== 'bold')) {
continue;
}
// Special types
if (elemParams && elemParams.type) {
if (elemParams?.type) {
switch (elemParams.type) {
case 'time':
elemValue = new Date(elemValue).toString();
@ -53,12 +53,15 @@ class DiscordFormatter extends MarkdownFormatter {
}
}
if (elemParams.type === 'description') {
embed.setDescription(elemValue);
if (elemParams?.type === 'description') {
embed.setDescription(embed.description
? embed.description += `\n${elemValue}`
: elemValue
);
continue;
}
if (elemParams.type === 'title') {
if (elemParams?.type === 'title') {
embed.setTitle(elemValue);
if (elemParams.color) {
embed.setColor(elemParams.color.toUpperCase());
@ -66,7 +69,7 @@ class DiscordFormatter extends MarkdownFormatter {
continue;
}
if (elemParams && elemParams.label) {
if (elemParams?.label) {
let label = elemParams.label;
// If the label param is an array, choose the last element
@ -78,7 +81,10 @@ class DiscordFormatter extends MarkdownFormatter {
embed.addField(label, elemValue, true);
} else {
embed.setDescription(embed.description += elemValue);
embed.setDescription(embed.description
? embed.description += `\n${elemValue}`
: elemValue
);
}
}
@ -138,7 +144,19 @@ class DiscordProtocol extends Protocol {
public format: Formatter = new DiscordFormatter();
public type = 'DiscordProtocol';
private client = new Discord.Client();
private client = new Discord.Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_PRESENCES,
Intents.FLAGS.GUILD_VOICE_STATES,
Intents.FLAGS.GUILD_BANS,
Intents.FLAGS.DIRECT_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGE_REACTIONS,
Intents.FLAGS.DIRECT_MESSAGE_TYPING,
],
});
private eventsAttached = false;
@ -151,8 +169,17 @@ class DiscordProtocol extends Protocol {
}
this.emit('running');
});
this.attachEvents();
try {
this.client.login(this.config.token);
} catch (e: any) {
this.client.on('error', (e: Error) => {
this.emit('error', e.message);
});
this.client.on('disconnect', () => this.stop(true));
}
}
public stop(force = false): void {
@ -176,10 +203,16 @@ class DiscordProtocol extends Protocol {
if (this.eventsAttached) {
return;
}
this.eventsAttached = true;
this.client.on('error', (e) => this.emit('error', e));
this.client.on('error', (e: Error) => {
this.emit('error', e.message);
});
this.client.on('disconnect', () => this.stop(true));
this.client.on('message', (message) => {
this.client.on('messageCreate', (message) => {
if (this.me && this.me.id && message.author.id === this.me.id) {
return;
}
@ -231,7 +264,7 @@ class DiscordProtocol extends Protocol {
if (Array.isArray(data[0])) {
try {
response = this.format.compose(data[0]);
} catch (e) {
} catch (e: any) {
logger.error('[%s] Failed to compose message:', this.fullName, e.message);
return;
}
@ -241,7 +274,14 @@ class DiscordProtocol extends Protocol {
response = data[0];
}
msg.data.channel.send(response).catch((e: Error) => {
const toDiscord: { embeds?: object[]; content?: string } = {};
if (typeof response === 'object') {
toDiscord.embeds = [ response ];
} else {
toDiscord.content = response;
}
msg.data.channel.send(toDiscord).catch((e: Error) => {
logger.error(e);
});
}

1052
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -11,8 +11,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"@squeebot/core": "file:../core",
"discord.js": "^12.5.1",
"typescript": "^4.1.2"
"@squeebot/core": "^3.3.1",
"discord.js": "^13.1.0",
"typescript": "^4.4.2"
}
}

View File

@ -3,7 +3,7 @@
"plugins": [
{
"name": "discord",
"version": "1.2.0"
"version": "1.2.1"
}
],
"typescript": true