UNPKG

@castery/caster-discord

Version:

🤖⛓️ The Discord platform for caster

138 lines (119 loc) 2.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _caster = require("@castery/caster"); var _constants = require("../util/constants"); const { SUPPORTED_CONTEXT_TYPES, SUPPORTED_ATTACHMENT_TYPES } = _caster.contextProps; const enumTypesMessage = { text: 'channel' }; /** * Incoming vk context * * @public */ class DiscordMessageContext extends _caster.MessageContext { /** * Constructor * * @param {Caster} caster * @param {Message} message * @param {number} id */ constructor(caster, { id, message, $text = null }) { super(caster); this.platform = { id, name: _constants.PLATFORM_NAME }; const { type } = message.channel; this.from = { id: message.channel.id, type: type in enumTypesMessage ? enumTypesMessage[type] : type }; this.sender = { id: message.author.id, type: 'user' }; this.text = message.content; this.$text = $text; this.raw = message; } /** * Returns supported context types * * @return {Object} */ get [SUPPORTED_CONTEXT_TYPES]() { return _constants.supportedContextTypes; } /** * Returns supported attachment types * * @return {Object} */ get [SUPPORTED_ATTACHMENT_TYPES]() { return _constants.supportedAttachmentTypes; } /** * Sends a message to the current dialog * * @param {mixed} text * @param {Object} options * * @return {Promise<mixed>} */ send(text, options = {}) { if (typeof text === 'object') { options = text; } else { options.text = text; } this.to = this.from; this.text = options.text; const message = new DiscordMessageContext(this.caster, { id: this.platform.id, message: this.raw }); message.to = this.from; message.state = Object.assign({}, this.state); message.text = options.text; if ('attachments' in options) { if (!Array.isArray(options.attachments)) { message.attachments = [options.attachments]; } else { message.attachments = options.attachments; } } return this.caster.dispatchOutcoming(message); } /** * Responds to a message with a mention * * @param {mixed} text * @param {Object} options * * @return {Promise<mixed>} */ reply(text, options = {}) { if (typeof text === 'object') { options = text; } else { options.text = text; } options.text = `<@${this.sender.id}>, ${options.text}`; return this.send(options); } } exports.default = DiscordMessageContext;