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)
66 lines • 3.92 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const uid_1 = require("uid");
const helpers_1 = require("../helpers");
const state_1 = __importDefault(require("../state"));
async function default_1(client) {
client.on('messageCreate', async (message) => {
var _a, _b, _c, _d;
try {
if (message.author.bot || message.author.system)
return;
const userRoles = (_a = message.member) === null || _a === void 0 ? void 0 : _a.roles.cache.map((role) => role.id);
const clientId = (_b = client.user) === null || _b === void 0 ? void 0 : _b.id;
const botMention = message.mentions.users.some((user) => user.id === clientId);
message.content = message.content.replace(/<@!?\d+>/g, '').trim();
if (state_1.default.channels[message.channelId] || state_1.default.channels.all) {
[...((_c = state_1.default.channels[message.channelId]) !== null && _c !== void 0 ? _c : []), ...((_d = state_1.default.channels.all) !== null && _d !== void 0 ? _d : [])].forEach(async (trigger) => {
var _a;
if (trigger.type === 'message') {
if (trigger.roleIds.length) {
const hasRole = trigger.roleIds.some((role) => userRoles === null || userRoles === void 0 ? void 0 : userRoles.includes(role));
if (!hasRole)
return;
}
if (trigger.botMention && !botMention)
return;
const escapedTriggerValue = ((_a = trigger.value) !== null && _a !== void 0 ? _a : '')
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
.replace(/-/g, '\\x2d');
let regStr = `^${escapedTriggerValue}$`;
if (trigger.pattern === 'start')
regStr = `^${escapedTriggerValue}`;
else if (trigger.pattern === 'end')
regStr = `${escapedTriggerValue}$`;
else if (trigger.pattern === 'contain')
regStr = `${escapedTriggerValue}`;
else if (trigger.pattern === 'regex')
regStr = `${trigger.value}`;
const reg = new RegExp(regStr, trigger.caseSensitive ? '' : 'i');
if (reg.test(message.content)) {
(0, helpers_1.addLog)(`triggerWorkflow ${trigger.webhookId}`, client);
const placeholderMatchingId = trigger.placeholder ? (0, uid_1.uid)() : '';
const isEnabled = await (0, helpers_1.triggerWorkflow)(trigger.webhookId, message, placeholderMatchingId, state_1.default.baseUrl).catch((e) => e);
if (isEnabled && trigger.placeholder) {
const channel = client.channels.cache.get(message.channelId);
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);
}
}
}
});
}
}
catch (e) {
(0, helpers_1.addLog)(`${e}`, client);
}
});
}
exports.default = default_1;
//# sourceMappingURL=messageCreate.event.js.map