我正在使用 DiscordJs 和 distube 为我的 discord 服务器创建一个机器人。我正在使用斜杠命令。问题是我在播放歌曲后无法执行任何命令(因此我什至无法执行 /stop 或播放另一首歌曲)。这是我的代码:
const {SlashCommandBuilder} = require("@discordjs/builders");
module.exports = {
data: new SlashCommandBuilder()
.setName("play")
.setDescription("Play a song.")
.addStringOption(option => option.setName("song").setDescription("The song link or name.").setRequired(true)),
async execute(interaction) {
interaction.reply({content: 'Music started.', ephemeral: true});
const {member, channel, client} = interaction;
await client.distube.play(member.voice.channel, interaction.options.get("song").value, {textChannel: channel, member: member});
}
}
我的命令处理程序:
const fs = require("fs");
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const clientId = '12345678';
const guildId = '12345678';
module.exports = (client) => {
client.handleCommands = async (commandsFolder, path) => {
client.commandArray = [];
for (const folder of commandsFolder) {
const commandFiles = fs.readdirSync(`${path}/${folder}`).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`../commands/${folder}/${file}`);
await client.commands.set(command.data.name, command);
client.commandArray.push(command.data.toJSON());
}
}
const rest = new REST({ version: '9' }).setToken("sometextthatidontwanttoshow");
await (async () => {
try {
console.log('Started refreshing application commands.');
await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{body: client.commandArray},
);
console.log('Successfully reloaded application commands.');
} catch (error) {
console.error(error);
}
})();
}
}
这是“创建”命令的函数:
module.exports = {
name: "interactionCreate",
async execute(interaction, client) {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({
content: "There was an error while executing this command.",
ephemeral: true
})
}
}
}
这是我的索引文件
const {Client, Intents, Collection} = require("discord.js");
const fs = require("fs");
const client = new Client({intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES]});
client.commands = new Collection();
const {DisTube} = require("distube");
client.distube = new DisTube(client, {
emitNewSongOnly: true,
leaveOnFinish: false,
emitAddSongWhenCreatingQueue: false
});
module.exports = client;
const functions = fs.readdirSync("./src/functions").filter(file => file.endsWith(".js"));
const eventFiles = fs.readdirSync("./src/events").filter(file => file.endsWith(".js"));
const commandsFolders = fs.readdirSync("./src/commands");
(async () => {
for (const file of functions) {
require(`./src/functions/${file}`)(client);
}
client.handleEvents(eventFiles, "./src/events");
client.handleCommands(commandsFolders, "./src/commands");
await client.login('mybeautifultoken')
})();
非常感谢任何帮助。提前致谢。