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)
89 lines • 3.61 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerCommands = void 0;
const discord_js_1 = require("discord.js");
const rest_1 = require("@discordjs/rest");
const imports = ['clear', 'test', 'logs'];
const awaitingCommands = [];
imports.forEach((commandName) => {
const command = Promise.resolve().then(() => __importStar(require(`./commands/${commandName}`)));
awaitingCommands.push(command);
});
const registerCommands = async (token, clientId, triggerCommands) => {
console.log('registering commands');
const commands = await Promise.all(awaitingCommands).catch((e) => e);
const rest = new rest_1.REST({ version: '10' }).setToken(token);
const parsedCommands = commands.map((e) => {
return e.default.registerCommand().toJSON();
});
if (triggerCommands)
parsedCommands.push(...triggerCommands);
rest
.put(discord_js_1.Routes.applicationCommands(clientId), {
body: parsedCommands,
})
.catch(console.error);
return commands;
};
exports.registerCommands = registerCommands;
async function default_1(token, clientId, client) {
const commands = await (0, exports.registerCommands)(token, clientId);
client.on('interactionCreate', async (interaction) => {
var _a;
try {
if (!interaction.isChatInputCommand())
return;
if (!interaction.guildId) {
interaction.reply({ content: 'Commands work only inside channels' });
return;
}
const member = interaction.member;
if (!member.permissions.has('ADMINISTRATOR'))
return;
const { commandName, options } = interaction;
const i = imports.indexOf(commandName);
if (i === -1)
return;
const command = commands[i].default;
const reply = await command
.executeCommand((_a = options.get('input')) === null || _a === void 0 ? void 0 : _a.value, interaction)
.catch((e) => e);
const botReply = await interaction
.reply({ content: reply, fetchReply: true })
.catch((e) => e);
if (command.params.autoRemove || reply === 'Done!') {
setTimeout(async () => {
botReply.delete().catch((e) => console.log(e));
}, 2000);
}
}
catch (e) {
console.log(e);
}
});
}
exports.default = default_1;
//# sourceMappingURL=commands.js.map