How to create a channel then find the ID

Viewed 244

I am creating a channel with message.guild.channels.create. How do I go about then finding the message id of that channel and sending a message in the newly created channel?

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'],
  }]
})
2 Answers

guild.channels.create returns a promise that resolves to a GuildChannel. It means you can await the result and that's the newly created channel:

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;

Make sure that the parent function is an async fucntion.

You can use bot.channels.cache.find() to get the id after creating the channel, like so:

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

Hope this helped!

Related