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)
128 lines • 5.58 kB
JavaScript
;
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 node_ipc_1 = __importDefault(require("node-ipc"));
const DiscordTrigger_node_options_1 = require("./DiscordTrigger.node.options");
const helpers_1 = require("./bot/helpers");
const nodeDescription = {
displayName: 'Discord Trigger',
name: 'discordTrigger',
icon: 'file:discord.svg',
group: ['trigger', 'discord'],
version: 1,
description: 'Trigger based on Discord events',
eventTriggerDescription: '',
mockManualExecution: true,
activationMessage: 'Your workflow will now trigger executions on the event you have defined.',
defaults: {
name: 'Discord Trigger',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'discordApi',
required: true,
},
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: DiscordTrigger_node_options_1.options,
};
class DiscordTrigger {
constructor() {
this.description = nodeDescription;
this.methods = {
loadOptions: {
async getChannels() {
return await (0, helpers_1.getChannels)(this).catch((e) => e);
},
async getRoles() {
return await (0, helpers_1.getRoles)(this).catch((e) => e);
},
},
};
}
async webhook() {
const req = this.getRequestObject();
return {
workflowData: [this.helpers.returnJsonArray(req.body)],
};
}
async trigger() {
const activationMode = this.getActivationMode();
if (activationMode !== 'manual') {
let baseUrl = '';
const credentials = (await this.getCredentials('discordApi').catch((e) => e));
await (0, helpers_1.connection)(credentials).catch((e) => e);
try {
const regex = /^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^\/\n?]+)/gim;
let match;
while ((match = regex.exec(credentials.baseUrl)) != null) {
baseUrl = match[0];
}
}
catch (e) {
console.log(e);
}
node_ipc_1.default.connectTo('bot', () => {
const { webhookId } = this.getNode();
const parameters = {};
Object.keys(this.getNode().parameters).forEach((key) => {
parameters[key] = this.getNodeParameter(key, '');
});
node_ipc_1.default.of.bot.emit('trigger', Object.assign(Object.assign({}, parameters), { baseUrl,
webhookId, active: this.getWorkflow().active, credentials }));
});
}
return;
}
async execute() {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
const executionId = this.getExecutionId();
const input = this.getInputData();
const credentials = (await this.getCredentials('discordApi'));
const placeholderId = (_a = input[0].json) === null || _a === void 0 ? void 0 : _a.placeholderId;
const channelId = (_b = input[0].json) === null || _b === void 0 ? void 0 : _b.channelId;
const userId = (_c = input[0].json) === null || _c === void 0 ? void 0 : _c.userId;
const userName = (_d = input[0].json) === null || _d === void 0 ? void 0 : _d.userName;
const userTag = (_e = input[0].json) === null || _e === void 0 ? void 0 : _e.userTag;
const messageId = (_f = input[0].json) === null || _f === void 0 ? void 0 : _f.messageId;
const content = (_g = input[0].json) === null || _g === void 0 ? void 0 : _g.content;
const presence = (_h = input[0].json) === null || _h === void 0 ? void 0 : _h.presence;
const addedRoles = (_j = input[0].json) === null || _j === void 0 ? void 0 : _j.addedRoles;
const removedRoles = (_k = input[0].json) === null || _k === void 0 ? void 0 : _k.removedRoles;
const interactionMessageId = (_l = input[0].json) === null || _l === void 0 ? void 0 : _l.interactionMessageId;
const interactionValues = (_m = input[0].json) === null || _m === void 0 ? void 0 : _m.interactionValues;
const userRoles = (_o = input[0].json) === null || _o === void 0 ? void 0 : _o.userRoles;
const attachments = (_p = input[0].json) === null || _p === void 0 ? void 0 : _p.attachments;
await (0, helpers_1.execution)(executionId, placeholderId, channelId, credentials.apiKey, credentials.baseUrl, userId).catch((e) => e);
const returnData = [];
returnData.push({
json: Object.assign({ content,
channelId,
userId,
userName,
userTag,
messageId,
presence,
addedRoles,
removedRoles,
interactionMessageId,
interactionValues,
userRoles }, ((attachments === null || attachments === void 0 ? void 0 : attachments.length) ? { attachments } : {})),
});
return this.prepareOutputData(returnData);
}
}
exports.DiscordTrigger = DiscordTrigger;
//# sourceMappingURL=DiscordTrigger.node.js.map