【问题标题】:Retrieve ID of channel after creation创建后获取频道 ID
【发布时间】:2020-10-15 22:06:53
【问题描述】:

我正在尝试同时创建一个类别和一个频道,将频道的父级设置为所述类别。但是,当我搜索通道缓存时,它会返回我正在搜索的类别的 undefined。

    message.guild.channels.create(`Category ${f.number}`, {
        type: 'category',
        permissionOverwrites: [
            {
                //everyone
                id: '762492106156539945',
                deny: ['VIEW_CHANNEL']
            },
            {
                //verified
                id: '763048301552992346',
                allow: ['VIEW_CHANNEL']
            }
        ]
    });
    message.guild.channels.create(`cat-${f.number}-chat`, {
        type: 'text'
    }).then(channel => {
        var category = message.guild.channels.cache.get("name", `Category ${f.number}`);
        channel.setParent(category.id);
      }).catch(console.error);

控制台正在打印“TypeError: Cannot read property 'id' of undefined”。 稍后我还需要以某种方式删除频道和类别。

【问题讨论】:

    标签: javascript discord.js


    【解决方案1】:

    首先使用Promise.all 等待两个创建完成

    const categoryPromise = message.guild.channels.create(`Category ${f.number}`, {
        type: 'category',
        permissionOverwrites: [
            {
                //everyone
                id: '762492106156539945',
                deny: ['VIEW_CHANNEL']
            },
            {
                //verified
                id: '763048301552992346',
                allow: ['VIEW_CHANNEL']
            }
        ]
    });
    const channelPromise = message.guild.channels.create(`cat-${f.number}-chat`, {
        type: 'text'
    })
    
    Promise.all([categoryPromise, channelPromise])
      .then(([category, channel]) => {
        channel.setParent(category);
      }).catch(console.error);
    

    【讨论】:

    • 编辑为类别使用promise repsonse,无需查看缓存,您可以将整个CategoryChannel对象传递给setParent
    • 我还需要存储频道,以便以后删除它。是否可以通过与上述相同的方法来完成?
    猜你喜欢
    • 2020-08-12
    • 2020-07-26
    • 2021-05-04
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 2018-01-06
    • 2021-09-19
    相关资源
    最近更新 更多