我正在尝试制作一个discord.js
机器人,制作后需要找到#counting频道。
我尝试了以下代码:
if (command === 'init') {
try {
msg.guild.channels.create('counting');
var countChannel = msg.guild.channels.cache.find(
(channel) => channel.name === 'counting'
);
msg.channel.send(`Created <#${countChannel}>`);
} catch (err) {
msg.channel.send(`Error: **${err}**`);
}
}
我不知道为什么返回undefined
。
任何修复程序?
答案 0 :(得分:0)
GuildChannelManager.create()
返回一个Promise
,并将创建的通道作为参数传递。
if (command === 'init') {
msg.guild.channels
.create('counting')
.then((countChannel) => {
msg.channel.send(`Created <#${countChannel.id}>`);
})
.catch((err) => msg.channel.send(`Error: **${err}**`));
}
由于出现错误的原因,该脚本运行得太快了,并试图在其创建完成之前就找到频道。