1

我正在使用 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')
})();

非常感谢任何帮助。提前致谢。

4

1 回答 1

1

好的,我相信问题在于您正在等待您的 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");
于 2022-02-17T19:37:56.210 回答