首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不和谐与不和谐--为什么我在演奏音乐之后什么都不能做?

不和谐与不和谐--为什么我在演奏音乐之后什么都不能做?
EN

Stack Overflow用户
提问于 2022-02-17 19:13:10
回答 1查看 427关注 0票数 1

我正在使用DiscordJ和distube为我的不和谐服务器创建一个机器人。我在使用斜杠命令。问题是,在播放一首歌之后,我无法执行任何命令(因此我甚至不能执行/stop或播放另一首歌曲)。这是我的密码:

代码语言:javascript
复制
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});
    }
}

我的指挥人员:

代码语言:javascript
复制
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);
            }
        })();
    }
}

这是“创建”命令的函数:

代码语言:javascript
复制
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
            })
        }
    }
}

这是我的索引文件

代码语言:javascript
复制
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')
})();

任何帮助都是非常感谢的。提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2022-02-17 19:37:56

好的,我相信问题在于您正在等待您的command.execute()。我相信,在幕后,这是在创造一个承诺,解决一旦你的不和谐机器人的音乐播放完毕。

虽然异步使用这些函数是正确的,但是当您像这样调用它时,它实际上会阻止所有其他类似的异步函数(斜杠命令)发生,直到这个函数解析为止。如果这能解决问题请告诉我。

代码语言:javascript
复制
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
            })
        }
    }
}

索引编辑

代码语言:javascript
复制
 await client.handleEvents(eventFiles, "./src/events");
 await client.handleCommands(commandsFolders, "./src/commands");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71164119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档