【问题标题】:How to create a channel then find the ID如何创建频道然后找到 ID
【发布时间】:2021-05-24 23:18:20
【问题描述】:

我正在使用message.guild.channels.create 创建一个频道。我该如何查找该频道的消息 ID 并在新创建的频道中发送消息?

message.guild.channels.create(`bug-priority${reportPriority}-${reportStart}`, {
  type: 'text',
  permissionOverwrites: [{
    id: message.author.id,
    allow: ['VIEW_CHANNEL', 'SEND_MESSAGES'],
  },
  {
    id: memberRole.id,
    deny: ['VIEW_CHANNEL', 'SEND_MESSAGES'],
  }]
})

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    创建频道后可以使用bot.channels.cache.find()获取id,如下:

    let channel = bot.channels.cache.find(channel => channel.name === "name here");
    let channelid = channel.id;
    

    希望这有帮助!

    【讨论】:

    • create() 是异步的。您确定在 Node.js 执行您的函数时该频道可用吗?
    • @ZsoltMeszaros 正确,我没想到
    【解决方案2】:

    guild.channels.create 返回一个解析为GuildChannel 的承诺。这意味着您可以等待结果,这就是新创建的频道:

    const channel = await message.guild.channels.create(`bug-priority${reportPriority}-${reportStart}`, {
      type: 'text',
      permissionOverwrites: [
        {
          id: message.author.id,
          allow: ['VIEW_CHANNEL', 'SEND_MESSAGES'],
        },
    
        {
          id: memberRole.id,
          deny: ['VIEW_CHANNEL', 'SEND_MESSAGES'],
        },
      ],
    });
    
    const { id } = channel;
    

    确保父函数是async 函数。

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 2021-10-12
      • 1970-01-01
      • 2020-07-26
      • 1970-01-01
      • 2021-05-04
      • 1970-01-01
      • 2022-01-09
      • 2021-02-06
      相关资源
      最近更新 更多