n8n-nodes-discord-trigger
Version:
A node that triggers a workflow whenever a message from discord is sent.
156 lines • 7.16 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DiscordInteraction = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const DiscordInteraction_node_options_1 = require("./DiscordInteraction.node.options");
const node_ipc_1 = __importDefault(require("node-ipc"));
const helper_1 = require("../helper");
const configuredOutputs = (parameters) => {
const mode = parameters.type;
if (mode === 'confirm') {
return [
{ displayName: 'confirm', type: 'main' },
{ displayName: 'cancel', type: 'main' },
{ displayName: 'no response', type: 'main' },
];
}
else {
return [{ type: 'main' }];
}
};
class DiscordInteraction {
constructor() {
this.description = {
displayName: 'Discord Interaction',
name: 'discordInteraction',
group: ['discord'],
version: 1,
description: 'Sends messages, embeds and prompts to Discord',
defaults: {
name: 'Discord Interaction',
},
icon: 'file:discord-logo.svg',
inputs: ['main'],
outputs: `={{(${configuredOutputs})($parameter)}}`,
credentials: [
{
name: 'discordBotTriggerApi',
required: true,
},
],
properties: DiscordInteraction_node_options_1.options,
};
this.methods = {
loadOptions: {
async getChannels() {
const selectedGuilds = this.getNodeParameter('guildIds', []);
console.log("selectedGuilds", selectedGuilds);
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', []);
console.log("selectedGuilds", selectedGuilds);
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 getGuilds() {
return await (0, helper_1.getGuilds)(this).catch((e) => e);
},
},
};
}
async execute() {
const executionId = this.getExecutionId();
const credentials = (await this.getCredentials('discordBotTriggerApi').catch((e) => e));
await (0, helper_1.connection)(credentials).catch((e) => {
console.log(e);
if (this.getNodeParameter('type', 0) === 'confirm') {
const returnData = [[], [], []];
returnData[2] = this.getInputData();
return returnData;
}
else {
return this.prepareOutputData(this.getInputData());
}
});
if (this.getNodeParameter('type', 0) === 'confirm') {
const returnData = [[], [], []];
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;
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: nodeParameters, token: credentials.token });
});
});
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;
}
else {
const returnData = [];
const items = this.getInputData();
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
const nodeParameters = {};
Object.keys(this.getNode().parameters).forEach((key) => {
nodeParameters[key] = this.getNodeParameter(key, itemIndex, '');
});
nodeParameters.executionId = executionId;
if (nodeParameters.channelId || nodeParameters.executionId) {
const res = new Promise((resolve) => {
node_ipc_1.default.config.retry = 1500;
node_ipc_1.default.connectTo('bot', () => {
const type = `send:${nodeParameters.type}`;
node_ipc_1.default.of.bot.on(`callback:${type}`, (data) => {
resolve(data);
});
node_ipc_1.default.of.bot.emit(type, { token: credentials.token, nodeParameters: nodeParameters });
});
}).catch((e) => {
console.log(e);
return this.prepareOutputData(this.getInputData());
});
returnData.push({
json: {
value: res === null || res === void 0 ? void 0 : res.value,
channelId: res === null || res === void 0 ? void 0 : res.channelId,
userId: res === null || res === void 0 ? void 0 : res.userId,
userName: res === null || res === void 0 ? void 0 : res.userName,
userTag: res === null || res === void 0 ? void 0 : res.userTag,
messageId: res === null || res === void 0 ? void 0 : res.messageId,
action: res === null || res === void 0 ? void 0 : res.action,
},
});
}
}
return this.prepareOutputData(returnData);
}
}
}
exports.DiscordInteraction = DiscordInteraction;
//# sourceMappingURL=DiscordInteraction.node.js.map