Skip to content

Instantly share code, notes, and snippets.

@iamnotacoder-djs
Created March 1, 2021 07:25
Show Gist options
  • Save iamnotacoder-djs/1583b60c394c788088f8f6760e35f5bc to your computer and use it in GitHub Desktop.
Save iamnotacoder-djs/1583b60c394c788088f8f6760e35f5bc to your computer and use it in GitHub Desktop.
Slash команды Discord.js
const Discord = require('discord.js'),
client = new Discord.Client(),
{ DiscordInteractions } = require("slash-commands");
const config = {
token: "секретный токен твоего бота",
publicKey : "публичный токен бота",
applicationId: "id приложения",
prefix: "",
guild_id: "id гильдии в каналах которой будут действовать слэши"
};
// Slash commands
client.interaction = new DiscordInteractions({
applicationId: config.applicationId,
authToken: config.token,
publicKey: config.publicKey,
});
client.login(config.token);
client.on('ready', () => {
checkIntegrations();
});
client.on('message', (message) => {
if (message.author.bot || message.channel.type != "text") return;
if (!message.content.startsWith(config.prefix)) return;
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift();
});
client.ws.on('INTERACTION_CREATE', async interaction => {
if (interaction.data.name == "ping") {
var param1 = "";
var answer = "pong!"
if (interaction.data.options == undefined) {
// опций нет
} else {
interaction.data.options.forEach((c) => {
if (c.name == "param1") {
param1 = c.value;
}
});
}
answer += " - " + param1; // та самая "магия"
client.api.interactions(interaction.id, interaction.token).callback.post({
data: {
type: 4,
data: {
embeds: [
{
color: 0x000000,
title: param1,
description: answer
}
]
}
}
});
}
});
function checkIntegrations() {
// удаление старых команд
client.interaction
.getApplicationCommands()
.then((d) => {
d.forEach((r) => {
client.interaction
.deleteApplicationCommand(r.id, config.guild_id)
.then(() => {
// do nothing
})
.catch(console.log);
})
})
.catch(console.log);
// регистрация новых
client.interaction
.createApplicationCommand({
name: "ping",
description: "Это ping любое описание",
options: [
{
name: "param1",
description: "description",
type: "3"
}
]
}, config.guild_id)
.then(console.log)
.catch(console.error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment