SquadJS/squad-server/plugins/discord-base-plugin.js

46 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-10-13 06:59:07 -05:00
import BasePlugin from './base-plugin.js';
import { COPYRIGHT_MESSAGE } from '../utils/constants.js';
2020-10-13 06:59:07 -05:00
export default class DiscordBasePlugin extends BasePlugin {
static get optionsSpecification() {
return {
discordClient: {
required: true,
description: 'Discord connector name.',
connector: 'discord',
default: 'discord'
}
};
}
2020-12-03 08:16:07 -06:00
async prepareToMount() {
try {
this.channel = await this.options.discordClient.channels.fetch(this.options.channelID);
} catch (error) {
this.channel = null;
2023-01-29 05:22:51 -06:00
this.verbose(
1,
`Could not fetch Discord channel with channelID "${this.options.channelID}". Error: ${error.message}`
);
this.verbose(2, `${error.stack}`);
}
2020-10-13 06:59:07 -05:00
}
2020-12-03 08:16:07 -06:00
async sendDiscordMessage(message) {
if (!this.channel) {
2023-01-29 05:22:51 -06:00
this.verbose(1, `Could not send Discord Message. Channel not initialized.`);
return;
}
2020-12-06 15:23:05 -06:00
if (typeof message === 'object' && 'embed' in message)
2021-01-07 14:52:55 -06:00
message.embed.footer = message.embed.footer || { text: COPYRIGHT_MESSAGE };
2023-05-20 20:18:16 -05:00
2023-02-27 11:09:16 -06:00
try {
await this.channel.send(message);
} catch (error) {
2023-05-20 20:18:16 -05:00
this.verbose(1, 'discordjs cache error caught!');
2023-02-27 11:09:16 -06:00
}
2020-10-13 06:59:07 -05:00
}
}