我正在使用DiscordJ和distube为我的不和谐服务器创建一个机器人。我在使用斜杠命令。问题是,在播放一首歌之后,我无法执行任何命令(因此我甚至不能执行/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')
})();
任何帮助都是非常感谢的。提前谢谢。
发布于 2022-02-17 19:37:56
好的,我相信问题在于您正在等待您的command.execute()。我相信,在幕后,这是在创造一个承诺,解决一旦你的不和谐机器人的音乐播放完毕。
虽然异步使用这些函数是正确的,但是当您像这样调用它时,它实际上会阻止所有其他类似的异步函数(斜杠命令)发生,直到这个函数解析为止。如果这能解决问题请告诉我。
module.exports = {
name: "interactionCreate",
async execute(interaction, client) {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
command.execute(interaction); //removed await
} catch (error) {
console.error(error);
await interaction.reply({
content: "There was an error while executing this command.",
ephemeral: true
})
}
}
}索引编辑
await client.handleEvents(eventFiles, "./src/events");
await client.handleCommands(commandsFolders, "./src/commands");https://stackoverflow.com/questions/71164119
复制相似问题