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)
110 lines • 6.5 kB
JavaScript
;
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 uid_1 = require("uid");
const helpers_1 = require("../helpers");
const state_1 = __importDefault(require("../state"));
async function default_1(client) {
client.on('interactionCreate', (interaction) => {
var _a, _b, _c, _d;
try {
if (!interaction.isButton() && !interaction.isSelectMenu())
return;
const userRoles = ((_a = interaction.member) === null || _a === void 0 ? void 0 : _a.roles).cache.map((role) => role.id);
Object.keys(state_1.default.channels).forEach((key) => {
const channel = state_1.default.channels[key];
channel.forEach(async (trigger) => {
if (trigger.type === 'interaction' &&
trigger.interactionMessageId === interaction.message.id) {
if (trigger.roleIds.length) {
const hasRole = trigger.roleIds.some((role) => userRoles === null || userRoles === void 0 ? void 0 : userRoles.includes(role));
if (!hasRole) {
interaction.reply({ content: `You are not allowed to do this`, ephemeral: true });
return;
}
}
(0, helpers_1.addLog)(`triggerWorkflow ${trigger.webhookId}`, client);
const placeholderMatchingId = trigger.placeholder ? (0, uid_1.uid)() : '';
const interactionValues = interaction.isButton()
? [interaction.customId]
: interaction.values;
const isEnabled = await (0, helpers_1.triggerWorkflow)(trigger.webhookId, null, placeholderMatchingId, state_1.default.baseUrl, interaction.user, interaction.channelId, undefined, undefined, undefined, interaction.message.id, interactionValues, userRoles).catch((e) => e);
const labels = [];
interaction.message.components.forEach((component) => {
component.components.forEach((element) => {
if (element.type === discord_js_1.ComponentType.Button &&
element.customId === interaction.customId) {
if (element.label)
labels.push(element.label);
}
else if (element.type === discord_js_1.ComponentType.SelectMenu) {
element.options.forEach((option) => {
if (interaction.values.includes(option.value))
labels.push(option.label);
});
}
});
});
interaction.deferUpdate();
if (isEnabled && trigger.placeholder) {
const channel = client.channels.cache.get(key);
const placeholder = await channel
.send(trigger.placeholder)
.catch((e) => (0, helpers_1.addLog)(`${e}`, client));
if (placeholder)
(0, helpers_1.placeholderLoading)(placeholder, placeholderMatchingId, trigger.placeholder);
}
}
});
});
const promptData = state_1.default.promptData[interaction.message.id];
if (!promptData) {
return;
}
if (promptData.restrictToRoles) {
const hasRole = promptData.mentionRoles.some((role) => userRoles === null || userRoles === void 0 ? void 0 : userRoles.includes(role));
if (!hasRole) {
interaction.reply({ content: `You are not allowed to do this`, ephemeral: true });
return;
}
}
const triggeringUserId = (_b = state_1.default.executionMatching[promptData.executionId]) === null || _b === void 0 ? void 0 : _b.userId;
if (promptData.restrictToTriggeringUser &&
triggeringUserId &&
interaction.user.id !== triggeringUserId) {
interaction.reply({ content: `You are not allowed to do this`, ephemeral: true });
return;
}
if (promptData && !promptData.value) {
const bt = interaction.isButton()
? (_c = promptData.buttons) === null || _c === void 0 ? void 0 : _c.button.find((b) => b.value === interaction.customId)
: (_d = promptData.select) === null || _d === void 0 ? void 0 : _d.select.find((b) => b.value === interaction.values[0]);
(0, helpers_1.addLog)(`User interact: ${bt.label}`, client);
promptData.value = interaction.isButton() ? interaction.customId : interaction.values[0];
promptData.userId = interaction.user.id;
promptData.userName = interaction.user.username;
promptData.userTag = interaction.user.tag;
promptData.channelId = interaction.message.channelId;
promptData.messageId = interaction.message.id;
interaction.update({ components: [] }).catch((e) => e);
const channel = client.channels.cache.get(interaction.message.channelId);
channel.send(`<@${interaction.user.id}>: ` + bt.label);
setTimeout(async () => {
const message = await channel.messages
.fetch(interaction.message.id)
.catch((e) => e);
if (message)
message.edit({ content: promptData.content, components: [] }).catch((e) => e);
}, 1000);
}
}
catch (e) {
(0, helpers_1.addLog)(`${e}`, client);
}
});
}
exports.default = default_1;
//# sourceMappingURL=interactionCreateUI.event.js.map