Isaiah Easton
Isaiah Easton

Reputation: 47

Retrieve ID of channel after creation

I am trying to simultaneously create a category and a channel, setting the parent of the channel to said category. When I search through the channel cache, though, it returns undefined for the category I am searching for.

    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);

Console is printing "TypeError: Cannot read property 'id' of undefined". I will also need to somehow delete both the channel and the category at a later time.

Upvotes: 1

Views: 52

Answers (1)

coagmano
coagmano

Reputation: 5671

Wait for both creations to complete first using 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);

Upvotes: 1

Related Questions