UNPKG

n8n-nodes-discord

Version:

nodes to trigger workflows from Discord or send interactive messages. Uses the components API which allows to create dialogs (e.g. attach buttons and wait for the user to click on them)

151 lines 8.45 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const discord_js_1 = require("discord.js"); const helpers_1 = require("../helpers"); const state_1 = __importDefault(require("../state")); async function default_1(ipc, client) { ipc.server.on('send:prompt', async (nodeParameters, socket) => { try { if (state_1.default.ready) { const executionMatching = state_1.default.executionMatching[nodeParameters.executionId]; let channelId = ''; if (nodeParameters.triggerPlaceholder || nodeParameters.triggerChannel) channelId = executionMatching === null || executionMatching === void 0 ? void 0 : executionMatching.channelId; else channelId = nodeParameters.channelId; client.channels .fetch(channelId) .then(async (channel) => { var _a, _b; if (!channel || !channel.isTextBased()) return; (0, helpers_1.addLog)(`send:prompt to ${channelId}`, client); const promptProcessing = async (message) => { state_1.default.promptData[message.id] = nodeParameters; await (0, helpers_1.pollingPromptData)(message, nodeParameters.content, nodeParameters.timeout, client).catch((e) => (0, helpers_1.addLog)(`${e}`, client)); ipc.server.emit(socket, 'send:prompt', state_1.default.promptData[message.id]); delete state_1.default.promptData[message.id]; if (nodeParameters.placeholder) { const message = await channel .send({ content: nodeParameters.placeholder }) .catch((e) => e); await (0, helpers_1.execution)(nodeParameters.executionId, message.id, channel.id, nodeParameters.apiKey, nodeParameters.baseUrl).catch((e) => e); (0, helpers_1.placeholderLoading)(message, message.id, nodeParameters.placeholder); } }; let row; if (nodeParameters.buttons) { const buttons = []; ((_a = nodeParameters.buttons.button) !== null && _a !== void 0 ? _a : []).forEach((button) => { buttons.push(new discord_js_1.ButtonBuilder() .setCustomId(button.value) .setLabel(button.label) .setStyle(button.style)); }); row = new discord_js_1.ActionRowBuilder().addComponents(buttons); } else { const options = []; ((_b = nodeParameters.select.select) !== null && _b !== void 0 ? _b : []).forEach((select) => { options.push(Object.assign(Object.assign({ label: select.label }, (select.description ? { description: select.description } : {})), { value: select.value })); }); const select = new discord_js_1.SelectMenuBuilder() .setCustomId('select') .setPlaceholder('...') .setMinValues(nodeParameters.persistent ? nodeParameters.minSelect : 1) .setMaxValues(nodeParameters.persistent ? nodeParameters.maxSelect : 1) .addOptions(options); row = new discord_js_1.ActionRowBuilder().addComponents(select); } let mentions = ''; if (nodeParameters.mentionRoles) { nodeParameters.mentionRoles.forEach((role) => { mentions += ` <@&${role}>`; }); } let content = ''; if (nodeParameters.content) content += nodeParameters.content; if (mentions) content += mentions; const sendObject = { content: content + (nodeParameters.timeout ? ` (${nodeParameters.timeout}s)` : ''), components: [row], }; if (nodeParameters.triggerPlaceholder && (executionMatching === null || executionMatching === void 0 ? void 0 : executionMatching.placeholderId)) { const realPlaceholderId = state_1.default.placeholderMatching[executionMatching.placeholderId]; if (realPlaceholderId) { const message = await channel.messages .fetch(realPlaceholderId) .catch((e) => { (0, helpers_1.addLog)(`${e}`, client); }); delete state_1.default.placeholderMatching[executionMatching.placeholderId]; if (message && message.edit) { let t = 0; const retry = async () => { if (state_1.default.placeholderWaiting[executionMatching.placeholderId] && t < 10) { t++; setTimeout(() => retry(), 300); } else { await message.edit(sendObject).catch((e) => { (0, helpers_1.addLog)(`${e}`, client); }); promptProcessing(message); } }; retry(); return; } } } if (executionMatching === null || executionMatching === void 0 ? void 0 : executionMatching.placeholderId) delete state_1.default.placeholderMatching[executionMatching.placeholderId]; let message; if (nodeParameters.updateMessageId) { const messageToEdit = await channel.messages .fetch(nodeParameters.updateMessageId) .catch((e) => { (0, helpers_1.addLog)(`${e}`, client); }); if (messageToEdit && messageToEdit.edit) { message = await messageToEdit .edit(sendObject) .catch((e) => { (0, helpers_1.addLog)(`${e}`, client); }); } } else { message = await channel.send(sendObject).catch((e) => { (0, helpers_1.addLog)(`${e}`, client); }); } if (message && message.id && !nodeParameters.persistent) { promptProcessing(message); } else if (message && message.id && nodeParameters.persistent) { ipc.server.emit(socket, 'send:prompt', { channelId: channel.id, messageId: message.id, }); } }) .catch((e) => { (0, helpers_1.addLog)(`${e}`, client); ipc.server.emit(socket, 'send:prompt', false); }); } } catch (e) { (0, helpers_1.addLog)(`${e}`, client); ipc.server.emit(socket, 'send:prompt', false); } }); } exports.default = default_1; //# sourceMappingURL=sendPrompt.ipc.js.map