UNPKG

n8n-nodes-discord-trigger

Version:

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

96 lines 4.05 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DiscordConfirm = void 0; const DiscordConfirm_node_options_1 = require("./DiscordConfirm.node.options"); const node_ipc_1 = __importDefault(require("node-ipc")); const helper_1 = require("../helper"); class DiscordConfirm { constructor() { this.description = { displayName: 'Discord Confirmation', name: 'discordConfirm', group: ['discord'], version: 1, description: 'Let \'s the user decide whether to continue the interaction.', defaults: { name: 'Discord Confirmation', }, icon: 'file:discord-logo.svg', inputs: ['main'], outputs: [ { displayName: 'confirm', type: 'main' }, { displayName: 'cancel', type: 'main' }, { displayName: 'no response', type: 'main' }, ], credentials: [ { name: 'discordBotTriggerApi', required: true, }, ], properties: DiscordConfirm_node_options_1.options, }; this.methods = { loadOptions: { async getChannels() { const selectedGuilds = this.getNodeParameter('guildIds', []); console.log("selectedGuilds", selectedGuilds); if (!selectedGuilds.length) { throw new Error('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', []); console.log("selectedGuilds", selectedGuilds); if (!selectedGuilds.length) { throw new Error('Please select at least one server before choosing channels.'); } return await (0, helper_1.getRoles)(this, selectedGuilds).catch((e) => e); }, async getGuilds() { return await (0, helper_1.getGuilds)(this).catch((e) => e); }, }, }; } async execute() { const executionId = this.getExecutionId(); const returnData = [[], [], []]; const credentials = (await this.getCredentials('discordBotTriggerApi').catch((e) => e)); await (0, helper_1.connection)(credentials).catch((e) => { console.log(e); returnData[2] = this.getInputData(); return returnData; }); const nodeParameters = {}; Object.keys(this.getNode().parameters).forEach((key) => { nodeParameters[key] = this.getNodeParameter(key, 0, ''); }); const response = await new Promise((resolve) => { node_ipc_1.default.config.retry = 1500; console.log("connecting to bot"); node_ipc_1.default.connectTo('bot', () => { const type = `send:confirmation`; node_ipc_1.default.of.bot.on(`callback:send:confirmation`, (data) => { console.log("user decided", data); resolve(data); }); node_ipc_1.default.of.bot.emit(type, nodeParameters); }); }); console.log(response); if (response.confirmed === null) returnData[2] = this.getInputData(); else if (response.confirmed === true) returnData[0] = this.getInputData(); else returnData[1] = this.getInputData(); return returnData; } } exports.DiscordConfirm = DiscordConfirm; //# sourceMappingURL=DiscordConfirm.node.js.map