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)
171 lines • 10.3 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 helpers_1 = require("../helpers");
const state_1 = __importDefault(require("../state"));
async function default_1(ipc, client) {
ipc.server.on('send:message', 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.channelId;
else
channelId = nodeParameters.channelId;
client.channels
.fetch(channelId)
.then(async (channel) => {
var _a, _b, _c, _d, _e, _f, _g;
if (!channel || !channel.isTextBased())
return;
const embedFiles = [];
(0, helpers_1.addLog)(`send:message to ${channelId}`, client);
let embed;
if (nodeParameters.embed) {
embed = new discord_js_1.EmbedBuilder();
if (nodeParameters.title)
embed.setTitle(nodeParameters.title);
if (nodeParameters.url)
embed.setURL(nodeParameters.url);
if (nodeParameters.description)
embed.setDescription(nodeParameters.description);
if (nodeParameters.color)
embed.setColor(nodeParameters.color);
if (nodeParameters.timestamp)
embed.setTimestamp(Date.parse(nodeParameters.timestamp));
if (nodeParameters.footerText) {
let iconURL = nodeParameters.footerIconUrl;
if (iconURL && iconURL.match(/^data:/)) {
const buffer = Buffer.from(iconURL.split(',')[1], 'base64');
const reg = new RegExp(/data:image\/([a-z]+);base64/gi);
let mime = (_a = reg.exec(nodeParameters.footerIconUrl)) !== null && _a !== void 0 ? _a : [];
const file = new discord_js_1.AttachmentBuilder(buffer, { name: `footer.${mime[1]}` });
embedFiles.push(file);
iconURL = `attachment://footer.${mime[1]}`;
}
embed.setFooter(Object.assign({ text: nodeParameters.footerText }, (iconURL ? { iconURL } : {})));
}
if (nodeParameters.imageUrl) {
if (nodeParameters.imageUrl.match(/^data:/)) {
const buffer = Buffer.from(nodeParameters.imageUrl.split(',')[1], 'base64');
const reg = new RegExp(/data:image\/([a-z]+);base64/gi);
let mime = (_b = reg.exec(nodeParameters.imageUrl)) !== null && _b !== void 0 ? _b : [];
const file = new discord_js_1.AttachmentBuilder(buffer, { name: `image.${mime[1]}` });
embedFiles.push(file);
embed.setImage(`attachment://image.${mime[1]}`);
}
else
embed.setImage(nodeParameters.imageUrl);
}
if (nodeParameters.thumbnailUrl) {
if (nodeParameters.thumbnailUrl.match(/^data:/)) {
const buffer = Buffer.from(nodeParameters.thumbnailUrl.split(',')[1], 'base64');
const reg = new RegExp(/data:image\/([a-z]+);base64/gi);
let mime = (_c = reg.exec(nodeParameters.thumbnailUrl)) !== null && _c !== void 0 ? _c : [];
const file = new discord_js_1.AttachmentBuilder(buffer, { name: `thumbnail.${mime[1]}` });
embedFiles.push(file);
embed.setThumbnail(`attachment://thumbnail.${mime[1]}`);
}
else
embed.setThumbnail(nodeParameters.thumbnailUrl);
}
if (nodeParameters.authorName) {
let iconURL = nodeParameters.authorIconUrl;
if (iconURL && iconURL.match(/^data:/)) {
const buffer = Buffer.from(iconURL.split(',')[1], 'base64');
const reg = new RegExp(/data:image\/([a-z]+);base64/gi);
let mime = (_d = reg.exec(nodeParameters.authorIconUrl)) !== null && _d !== void 0 ? _d : [];
const file = new discord_js_1.AttachmentBuilder(buffer, { name: `author.${mime[1]}` });
embedFiles.push(file);
iconURL = `attachment://author.${mime[1]}`;
}
embed.setAuthor(Object.assign(Object.assign({ name: nodeParameters.authorName }, (iconURL ? { iconURL } : {})), (nodeParameters.authorUrl ? { url: nodeParameters.authorUrl } : {})));
}
if ((_e = nodeParameters.fields) === null || _e === void 0 ? void 0 : _e.field) {
nodeParameters.fields.field.forEach((field) => {
if (embed && field.name && field.value)
embed.addFields({
name: field.name,
value: field.value,
inline: field.inline,
});
else if (embed)
embed.addFields({ name: '\u200B', value: '\u200B' });
});
}
}
let mentions = '';
nodeParameters.mentionRoles.forEach((role) => {
mentions += ` <@&${role}>`;
});
let content = '';
if (nodeParameters.content)
content += nodeParameters.content;
if (mentions)
content += mentions;
let files = [];
if ((_f = nodeParameters.files) === null || _f === void 0 ? void 0 : _f.file) {
files = (_g = nodeParameters.files) === null || _g === void 0 ? void 0 : _g.file.map((file) => {
if (file.url.match(/^data:/)) {
return Buffer.from(file.url.split(',')[1], 'base64');
}
return file.url;
});
}
if (embedFiles.length)
files = files.concat(embedFiles);
const sendObject = Object.assign(Object.assign({ content: content !== null && content !== void 0 ? content : '' }, (embed ? { embeds: [embed] } : {})), (files.length ? { files } : {}));
if (nodeParameters.triggerPlaceholder && 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);
});
ipc.server.emit(socket, 'send:message', {
channelId,
messageId: message.id,
});
}
};
retry();
return;
}
}
}
const message = (await channel.send(sendObject).catch((e) => {
(0, helpers_1.addLog)(`${e}`, client);
}));
ipc.server.emit(socket, 'send:message', { channelId, messageId: message.id });
})
.catch((e) => {
(0, helpers_1.addLog)(`${e}`, client);
ipc.server.emit(socket, 'send:message', false);
});
}
}
catch (e) {
(0, helpers_1.addLog)(`${e}`, client);
ipc.server.emit(socket, 'send:message', false);
}
});
}
exports.default = default_1;
//# sourceMappingURL=sendMessage.ipc.js.map