0
votes

I'm trying to create a discord bot with discord.js.

Basically, I would like to create a category and get the id and then create another channel that takes the category as parent but I can't get the id.

With console.log(tempo1.id) I get undefined.

If someone has an idea I am a taker, I have looked and tried all day but nothing conclusive.

Thanks in advance for your help =))

Code:

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)

} })

Console Output

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]
    },
    }
  }
}
1

1 Answers

1
votes

guild.create method returns a Promise. To operate with data from Promise it should be resolved, as Promise is about asyncronous functions. So here are 2 options: create another channel in .then block

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

or use async|await constructions,e.g.

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