【问题标题】:Discord.js doesn't create temporary channelsDiscord.js 不会创建临时频道
【发布时间】:2020-06-23 10:47:53
【问题描述】:

我正在尝试制作一个机器人来创建临时语音通道

代码:

var temporary = []

client.on('voiceStateUpdate', (oldMember, newMember) => {
    const mainCatagory = '677192265491415041';
    const mainChannel = '677875869351542803';
    if (newMember.voiceChannelID == mainChannel) {
        newMember.guild.createChannel(`${newMember.user.username} 5vs5`, { type: 'voice', parent: mainCatagory })
            .then(async channel => {

                temporary.push({ newID: channel.id, guild: channel.guild })
                // A new element has been added to temporary array!
                await newMember.setVoiceChannel(channel.id)
            })
    }
    if (temporary.length >= 0) for (let i = 0; i < temporary.length; i++) {
        // Finding...
        let ch = temporary[i].guild.channels.find(x => x.id == temporary[i].newID)
        // Channel Found!         
        ch.setUserLimit(5)
        if (ch.members.size <= 0) {
            ch.delete(1000)
            // Channel has been deleted!
            return temporary.splice(i, 1)
        }
    }
})

为什么它不起作用? 在我重新安装系统之前它工作正常 节点:13.10.1 赢:10

【问题讨论】:

  • 请详细说明does not work到底是什么。如果您遇到任何错误,请提供有关问题所在的更多信息以及到目前为止您为解决问题所做的尝试。

标签: javascript discord.js


【解决方案1】:

我知道您在使用异步代码时遇到了问题。您有以下行:

await newMember.setVoiceChannel(channel.id)

这并没有像您期望的那样得到等待,因为它属于 .then 的块内。 await 仅影响 .then 语句中的异步块中的代码,并且由于在该行之后没有发生任何事情,因此它在功能上没有做任何与不等待它时不同的事情。

如果可能的话,你应该尽量避免混合 .then 和 async/await (你可能有混合的原因,但你需要知道你在做什么)。在这种情况下,我建议将整个事件处理程序设置为异步并等待两者。

注意:以下假设您使用的是 discord.js v11,这与您之前的代码示例一致。如果您使用的是 v12,则应该使用 guild.channels.create()guild.channels.cache.find()newMember.voice.setChannel()。您说您刚刚安装了一个新的 node 实例和可能的 discord.js,因此您实际上可能现在使用的是 v12,这可能是您的问题的一部分。

client.on('voiceStateUpdate', async (oldMember, newMember) => {
    const mainCatagory = '677192265491415041';
    const mainChannel = '677875869351542803';
    if (newMember.voiceChannelID == mainChannel) {
        let channel = await newMember.guild.createChannel(`${newMember.user.username} 5vs5`, { type: 'voice', parent: mainCatagory })

        temporary.push({ newID: channel.id, guild: channel.guild })
        // A new element has been added to temporary array!
        await newMember.setVoiceChannel(channel.id)
    }
    // The rest of your code.
}

【讨论】:

    猜你喜欢
    • 2021-10-12
    • 2023-03-27
    • 2021-04-04
    • 1970-01-01
    • 2021-05-04
    • 2021-02-01
    • 2020-07-26
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多