UNPKG

n8n-nodes-discord-trigger

Version:

A node that triggers a workflow whenever a message from discord is sent.

202 lines 9.84 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DiscordTrigger = void 0; const n8n_workflow_1 = require("n8n-workflow"); const DiscordTrigger_node_options_1 = require("./DiscordTrigger.node.options"); const bot_1 = __importDefault(require("../bot")); const node_ipc_1 = __importDefault(require("node-ipc")); const helper_1 = require("../helper"); const settings_1 = __importDefault(require("../settings")); if (!process.send) (0, bot_1.default)(); class DiscordTrigger { constructor() { this.description = { displayName: 'Discord Trigger', name: 'discordTrigger', group: ['trigger', 'discord'], version: 1, description: 'Discord Trigger on message', defaults: { name: 'Discord Trigger', }, icon: 'file:discord-logo.svg', inputs: [], outputs: ['main'], credentials: [ { name: 'discordBotTriggerApi', required: true, }, ], properties: DiscordTrigger_node_options_1.options, }; this.methods = { loadOptions: { async getGuilds() { return await (0, helper_1.getGuilds)(this).catch((e) => e); }, async getChannels() { const selectedGuilds = this.getNodeParameter('guildIds', []); if (!selectedGuilds.length) { throw new n8n_workflow_1.NodeOperationError('Please select at least one server before choosing channels.'); } return await (0, helper_1.getChannels)(this, selectedGuilds).catch((e) => e); }, async getRoles() { const selectedGuilds = this.getNodeParameter('guildIds', []); if (!selectedGuilds.length) { throw new n8n_workflow_1.NodeOperationError('Please select at least one server before choosing channels.'); } return await (0, helper_1.getRoles)(this, selectedGuilds).catch((e) => e); }, }, }; } async trigger() { const credentials = (await this.getCredentials('discordBotTriggerApi').catch((e) => e)); if (!(credentials === null || credentials === void 0 ? void 0 : credentials.token)) { console.log("No token given."); return {}; } await (0, helper_1.connection)(credentials).catch((e) => e); node_ipc_1.default.connectTo('bot', () => { console.log('Connected to IPC server'); const parameters = {}; Object.keys(this.getNode().parameters).forEach((key) => { parameters[key] = this.getNodeParameter(key, ''); }); node_ipc_1.default.of.bot.emit('triggerNodeRegistered', { parameters, active: this.getWorkflow().active, credentials, token: credentials.token, nodeId: this.getNode().id, }); node_ipc_1.default.of.bot.on('messageCreate', ({ message, author, guild, nodeId, messageReference, attachments, referenceAuthor }) => { if (this.getNode().id === nodeId) { console.log("received messageCreate event", message.id); const messageCreateOptions = { id: message.id, content: message.content, guildId: guild === null || guild === void 0 ? void 0 : guild.id, channelId: message.channelId, authorId: author.id, authorName: author.username, timestamp: message.createdTimestamp, listenValue: this.getNodeParameter('value', ''), authorIsBot: author.bot || author.system, referenceId: null, referenceContent: null, referenceAuthorId: null, referenceAuthorName: null, referenceTimestamp: null, }; if (messageReference) { messageCreateOptions.referenceId = messageReference.id; messageCreateOptions.referenceContent = messageReference.content; messageCreateOptions.referenceAuthorId = referenceAuthor.id; messageCreateOptions.referenceAuthorName = referenceAuthor.username; messageCreateOptions.referenceTimestamp = messageReference.createdTimestamp; } if (attachments) { messageCreateOptions.attachments = attachments; } this.emit([ this.helpers.returnJsonArray(messageCreateOptions), ]); } }); node_ipc_1.default.of.bot.on('guildMemberAdd', ({ guildMember, guild, user, nodeId }) => { if (this.getNode().id === nodeId) { this.emit([ this.helpers.returnJsonArray(guildMember), ]); } }); node_ipc_1.default.of.bot.on('guildMemberRemove', ({ guildMember, guild, user, nodeId }) => { if (this.getNode().id === nodeId) { this.emit([ this.helpers.returnJsonArray(guildMember), ]); } }); node_ipc_1.default.of.bot.on('guildMemberUpdate', ({ oldMember, newMember, guild, nodeId }) => { if (this.getNode().id === nodeId) { const addPrefix = (obj, prefix) => Object.fromEntries(Object.entries(obj).map(([key, value]) => [`${prefix}${key.charAt(0).toUpperCase()}${key.slice(1)}`, value])); const mergedGuildMemberUpdateOptions = { ...addPrefix(oldMember, "old"), ...addPrefix(newMember, "new"), ...addPrefix(guild, "guild"), }; this.emit([ this.helpers.returnJsonArray(mergedGuildMemberUpdateOptions), ]); } }); node_ipc_1.default.of.bot.on('messageReactionAdd', ({ messageReaction, message, user, guild, nodeId }) => { if (this.getNode().id === nodeId) { this.emit([ this.helpers.returnJsonArray({ ...messageReaction, ...user, channelId: message.channelId, guildId: guild.id }), ]); } }); node_ipc_1.default.of.bot.on('messageReactionRemove', ({ messageReaction, message, user, guild, nodeId }) => { if (this.getNode().id === nodeId) { this.emit([ this.helpers.returnJsonArray({ ...messageReaction, ...user, channelId: message.channelId, guildId: guild.id }), ]); } }); node_ipc_1.default.of.bot.on('roleCreate', ({ role, guild, nodeId }) => { if (this.getNode().id === nodeId) { this.emit([ this.helpers.returnJsonArray(role), ]); } }); node_ipc_1.default.of.bot.on('roleDelete', ({ role, guild, nodeId }) => { if (this.getNode().id === nodeId) { this.emit([ this.helpers.returnJsonArray(role), ]); } }); node_ipc_1.default.of.bot.on('roleUpdate', ({ oldRole, newRole, guild, nodeId }) => { if (this.getNode().id === nodeId) { const addPrefix = (obj, prefix) => Object.fromEntries(Object.entries(obj).map(([key, value]) => [`${prefix}${key.charAt(0).toUpperCase()}${key.slice(1)}`, value])); const mergedRoleOptions = { ...addPrefix(oldRole, "old"), ...addPrefix(newRole, "new") }; this.emit([ this.helpers.returnJsonArray(mergedRoleOptions), ]); } }); }); node_ipc_1.default.of.bot.on('disconnect', () => { console.error('Disconnected from IPC server'); }); return { closeFunction: async () => { const credentials = (await this.getCredentials('discordBotTriggerApi').catch((e) => e)); const isActive = await (0, helper_1.checkWorkflowStatus)(credentials.baseUrl, credentials.apiKey, String(this.getWorkflow().id)); console.log("removing trigger node"); delete settings_1.default.triggerNodes[this.getNode().id]; node_ipc_1.default.connectTo('bot', () => { node_ipc_1.default.of.bot.emit('triggerNodeRemoved', { nodeId: this.getNode().id }); }); if (!isActive || this.getActivationMode() !== 'manual') { console.log('Workflow stopped. Disconnecting bot...'); node_ipc_1.default.disconnect('bot'); } }, }; } } exports.DiscordTrigger = DiscordTrigger; //# sourceMappingURL=DiscordTrigger.node.js.map