【问题标题】:Get ID after discord.js channel creation创建 discord.js 频道后获取 ID
【发布时间】:2020-07-26 13:58:18
【问题描述】:

我正在尝试使用 discord.js 创建一个不和谐机器人。

基本上,我想创建一个类别并获取 id,然后创建另一个以该类别为父的频道,但我无法获得 id

使用console.log(tempo1.id) 我得到undefined

如果有人认为我是一个接受者,我整天都在寻找和尝试,但没有任何结论。

提前感谢您的帮助 =))

代码:

bot.on('message', message => { if (message.content === ${prefix}vocal) {

const tempo1 = guild.channels.create('Channel Tempo', {
  type: 'category'
}).then(console.log)

//guild.channels.create('Text Channel Tempo', { type: 'text' })

console.log(`Hello from ${tempo1}!`)

console.log(tempo1.id)

} })

控制台输出

Ready!
Hello from [object Promise]!
undefined
CategoryChannel {
  type: 'category',
  deleted: false,
  id: '699377593845022741',    
  name: 'Channel Tempo',
  rawPosition: 9,
  parentID: null,
  permissionOverwrites: Collection [Map] {},
  guild: Guild {
    members: GuildMemberManager {
      cacheType: [Function: Collection],    
      cache: [Collection [Map]],
      guild: [Circular]
    },
    channels: GuildChannelManager {
      cacheType: [Function: Collection],    
      cache: [Collection [Map]],
      guild: [Circular]
    },
    roles: RoleManager {
      cacheType: [Function: Collection],
      cache: [Collection [Map]],
      guild: [Circular]
    },
    presences: PresenceManager {
      cacheType: [Function: Collection],
      cache: [Collection [Map]]
    },
    voiceStates: VoiceStateManager {
      cacheType: [Function: Collection],
      cache: [Collection [Map]],
      guild: [Circular]
    },
    }
  }
}

【问题讨论】:

    标签: javascript


    【解决方案1】:

    guild.create 方法返回一个Promise。要处理来自 Promise 的数据,它应该被解析,因为 Promise 是关于异步函数的。所以这里有 2 个选项:在 .then 中创建另一个频道

        const tempo1 = guild.channels.create('Channel Tempo', { type: 'category' }).then(result => {
    console.log('Here is channel id', result.id)
    //create another channel here
    })
    

    或使用 async|await 结构,例如

    async function createChannel() {  
      const tempo1= await guild.channels.create('Channel Tempo', { type: 'category' })
      console.log(result)  
      console.log(tempo1.id)
      return tempo1
      }  
      createChannel()
    

    【讨论】:

      猜你喜欢
      • 2021-05-04
      • 1970-01-01
      • 2020-08-12
      • 2021-10-12
      • 2021-09-19
      • 2021-02-06
      • 2023-03-27
      • 2021-04-04
      • 1970-01-01
      相关资源
      最近更新 更多