如何获取 id 以创建语音通道

时间:2021-01-18 13:03:40

标签: javascript discord discord.js bots

我正在尝试获取 ID 以在 discord js v12 中创建语音频道。

我得到一个未定义的数组。

server.channels.create('helicopter', {
                                        type: "VOICE",
                                        parent: id_par
}).then( result => { console.log(result.id); voiceID.push(result.id)})

我也试过:

const newChannel = server.channels.create('helicopter', {
    type: "VOICE",
    parent: id_par
});
let x = await newChannel.id;

这里x也是未定义

这里还有:

server.channels.create('sky', {
                                    type: "VOICE",
                                    parent: id_par
        });
id = server.channels.cache.find(channel => channel.name === 'sky' && channel.type === "voice");
voiceID.push(id);

1 个答案:

答案 0 :(得分:2)

GuildChannelManager#create() 返回一个 Promise,这意味着我们可以在其上使用 .then() 函数并在机器人完成此方法后直接获取通道对象。获取通道对象后,我们可以简单地获取其 ID 并将其推送到我们想要的数组中。

最终代码

guild.channels.create('name here', {
  type: 'voice',
  parent: id_par
}).then(channel => voiceID.push(channel.id))