我如何添加斜杠命令 discord.js
How do i add slash commands discord.js
所以我关注了 youtube 上的 Codelyon's Code Your Own Discord Bot 2021 播放列表,我使用了他的高级命令处理程序并通过 modifying/adding 我自己的命令对其进行了一些更新,但后来我想更改他的常规高级命令处理程序使用 slash 命令,但我不知道如何实现它。我看过其他一些教程,但它们似乎不起作用。希望有人能帮助我。
main.js 文件的代码:
const Discord = require('discord.js');
const client = new Discord.Client({
intents: ['GUILDS', 'GUILD_MESSAGES']
});
const prefix = '!';
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('Bot is online!');
});
client.on('messageCreate', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (!client.commands.get(command)) return;
client.commands.get(command).execute(message, args, Discord, prefix, client);
})
client.login('xxx');
而here是我的文件格式
提前致谢!
所以这应该可以帮助您入门,然后使用本指南来制作命令
DiscordJS Guide - Slash Commands
您还需要确保您转到 Discord Dev 并确保您的机器人被邀请加入您的公会,并在 OAuth2 选项卡下检查这些并设置您需要的权限
const Discord = require('discord.js');
const client = new Discord.Client({
intents: ['GUILDS', 'GUILD_MESSAGES']
});
const prefix = '!'; //*
const fs = require('fs');
client.commands = new Discord.Collection(); //*
client.slashCommands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));//*
// create a new folder for your slash commands
const slashCommandFiles = fs.readdirSync('./slashCommands/').filter(file => file.endsWith('.js'));
for (const file of commandFiles) { //*
const command = require(`./commands/${file}`);//*
client.commands.set(command.name, command);//*
}//*
// delete anything using "commands" if you only plan on having slash commands otherwise keep both ( I marked them with a //* )
for (const file of slashCommandFiles) {
const slashCommand = require(`./slashCommands/${file}`);
client.slashCommands.set(slashCommand.name, slashCommand);
}
// other code
client.on('interactionCreate', async interaction => {
if (interaction.isCommand()) {
const command = client.slashCommands.get(interaction.commandName);
try {
command.execute(client, interaction);
} catch (error) {
console.error(error);
}
}
if (interaction.isButton()) {
}
if (interaction.isContextMenu()) {
}
});
所以我关注了 youtube 上的 Codelyon's Code Your Own Discord Bot 2021 播放列表,我使用了他的高级命令处理程序并通过 modifying/adding 我自己的命令对其进行了一些更新,但后来我想更改他的常规高级命令处理程序使用 slash 命令,但我不知道如何实现它。我看过其他一些教程,但它们似乎不起作用。希望有人能帮助我。
main.js 文件的代码:
const Discord = require('discord.js');
const client = new Discord.Client({
intents: ['GUILDS', 'GUILD_MESSAGES']
});
const prefix = '!';
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('Bot is online!');
});
client.on('messageCreate', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (!client.commands.get(command)) return;
client.commands.get(command).execute(message, args, Discord, prefix, client);
})
client.login('xxx');
而here是我的文件格式
提前致谢!
所以这应该可以帮助您入门,然后使用本指南来制作命令
DiscordJS Guide - Slash Commands
您还需要确保您转到 Discord Dev 并确保您的机器人被邀请加入您的公会,并在 OAuth2 选项卡下检查这些并设置您需要的权限
const Discord = require('discord.js');
const client = new Discord.Client({
intents: ['GUILDS', 'GUILD_MESSAGES']
});
const prefix = '!'; //*
const fs = require('fs');
client.commands = new Discord.Collection(); //*
client.slashCommands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));//*
// create a new folder for your slash commands
const slashCommandFiles = fs.readdirSync('./slashCommands/').filter(file => file.endsWith('.js'));
for (const file of commandFiles) { //*
const command = require(`./commands/${file}`);//*
client.commands.set(command.name, command);//*
}//*
// delete anything using "commands" if you only plan on having slash commands otherwise keep both ( I marked them with a //* )
for (const file of slashCommandFiles) {
const slashCommand = require(`./slashCommands/${file}`);
client.slashCommands.set(slashCommand.name, slashCommand);
}
// other code
client.on('interactionCreate', async interaction => {
if (interaction.isCommand()) {
const command = client.slashCommands.get(interaction.commandName);
try {
command.execute(client, interaction);
} catch (error) {
console.error(error);
}
}
if (interaction.isButton()) {
}
if (interaction.isContextMenu()) {
}
});