我正在尝试制作一个机器人来创建临时语音 channel 代码:
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
最佳答案
我发现您在异步代码方面遇到了问题。您有以下行:
等待 newMember.setVoiceChannel(channel.id)
这并没有像您期望的那样得到等待,因为它属于 .then
的 block 内。 wait 仅影响 .then
语句中该异步 block 中的代码,并且由于该行之后不会发生任何事情,因此它在功能上不会执行任何与不等待它时不同的操作。
如果可能的话,您应该尽量避免混合 .then 和 async/await (有一些原因您可能会混合使用,但您需要知道自己在做什么)。在这种情况下,我建议将整个事件处理程序设置为异步并等待两者。
注意:以下假设您使用的是discord.js v11,这与您之前的代码示例一致。如果您使用的是 v12,则应该使用 guild.channels.create()
、guild.channels.cache.find()
和 newMember.voice.setChannel ()
代替。您说您刚刚安装了一个新的节点实例和可能的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.
}
关于javascript - Discord.js 不会创建临时 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60636248/