@castery/caster-discord
Version:
🤖⛓️ The Discord platform for caster
240 lines (187 loc) • 4.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _debug = _interopRequireDefault(require("debug"));
var _discord = require("discord.js");
var _caster = require("@castery/caster");
var _message = _interopRequireDefault(require("./contexts/message"));
var _constants = require("./util/constants");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const debug = (0, _debug.default)('caster-discord');
/**
* Platform for integration with social network VK
*
* @public
*/
class DiscordPlatform extends _caster.Platform {
/**
* Constructor
*
* @param {Object} options
*/
constructor(options = {}) {
super();
Object.assign(this.options, _constants.defaultOptions);
this.discord = new _discord.Client();
this.casters = new Set();
if (Object.keys(options).length > 0) {
this.setOptions(options);
}
this.setReplacePrefix();
this.addDefaultEvents();
}
/**
* @inheritdoc
*/
setOptions(options) {
super.setOptions(options);
if ('adapter' in options) {
const {
adapter
} = this.options;
if ('token' in options) {
this.discord.login(adapter.token);
}
}
return this;
}
/**
* @inheritdoc
*/
getOptionsSchema() {
return _constants.defaultOptionsSchema;
}
/**
* @inheritdoc
*/
getAdapter() {
return this.discord;
}
/**
* Returns the platform id
*
* @return {string}
*/
getId() {
return this.options.id;
}
/**
* Returns the platform name
*
* @return {string}
*/
getPlatformName() {
return _constants.PLATFORM_NAME;
}
/**
* @inheritdoc
*/
async start() {
await this.discord.login(this.options.adapter.token);
if (this.options.id === null) {
this.setOptions({
id: this.discord.user.id
});
}
}
/**
* @inheritdoc
*/
async stop() {
await this.discord.destroy();
}
/**
* @inheritdoc
*/
async subscribe(caster) {
this.casters.add(caster);
if (!this.isStarted()) {
await this.start();
}
caster.outcoming.addPlatform(this, async (context, next) => {
if (context.getPlatformName() !== _constants.PLATFORM_NAME) {
return await next();
}
if (context.getPlatformId() !== this.options.id) {
return await next();
}
if (_constants.supportedContextTypes[context.type] !== true) {
throw new _caster.UnsupportedContextTypeError({
type: context.type
});
}
const channel = this.discord.channels.get(context.to.id);
if ('attachments' in context) {
for (const _ref of context.attachments) {
const {
type
} = _ref;
if (_constants.supportedAttachmentTypes[type] !== true) {
throw new _caster.UnsupportedAttachmentTypeError({
type
});
}
}
await Promise.all(context.attachments.map(({
source
}) => channel.send('', {
file: source
})));
}
if (context.text) {
await channel.send(context.text);
}
});
}
/**
* @inheritdoc
*/
async unsubscribe(caster) {
this.casters.delete(caster);
caster.outcoming.removePlatform(this);
if (this.casters.size === 0 && this.isStarted()) {
await this.stop();
}
}
/**
* Add default events discord
*/
addDefaultEvents() {
// eslint-disable-next-line no-console
this.discord.on('error', console.error);
this.discord.on('message', message => {
/* Ignore other bots and self */
if (message.author.bot) {
return;
}
let $text = message.content;
if ($text !== null) {
if (!this.hasPrefix.test($text)) {
return;
}
$text = $text.replace(this.replacePrefix, '');
}
for (const caster of this.casters) {
caster.dispatchIncoming(new _message.default(caster, {
id: this.options.id,
message,
$text
}));
}
});
}
/**
* Sets replace prefix
*/
setReplacePrefix() {
let {
prefix
} = this.options;
prefix = String.raw`^(?:${prefix.join('|')})`;
this.hasPrefix = new RegExp(String.raw`${prefix}.+`, 'i');
this.replacePrefix = new RegExp(String.raw`${prefix}?[, ]*`, 'i');
}
}
exports.default = DiscordPlatform;