Spooky Hmm
Spooky Hmm

Reputation: 25

How to fix TypeError [INVALID_TYPE]: Supplied parameter is not a User nor a Role

I'm running into a problem when I'm trying to create a role and a text channel. The role created will set permissions for the text channel.

if (id === '4') {
  message.guild.roles.create({
    name: `Owner`,
    color: `${color.white}`,
    permissions: ['ADD_REACTIONS'],
    hoist: true,
  });

  let owner = message.guild.roles.cache.find((role) => role.name === 'Owner');

  name = `${names.name}`,
    message.guild.channels
      .create(name, {
        type: 'GUILD_TEXT',
        permissionOverwrites: [
          {
            id: owner,
            allow: ['ADD_REACTIONS'],
            deny: ['VIEW_CHANNEL'],
          },
        ],
      })
      .catch((err) => console.log(err));
}

Although it creates the role, when it comes to the text channel, it cannot find the role made and throws an error:

TypeError [INVALID_TYPE]: Supplied parameter is not a User nor a Role.

I'm unsure how to fix this.

Upvotes: 1

Views: 997

Answers (2)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23161

roles.create() is an asynchronous method, which means it returns a promise and allows the rest of your code to run without blocking anything. It also means that when you try to find the newly created role on the next line, it won't be there as it's not even created, so it will be undefined. As undefined is not a valid Role or User, you receive a TypeError.

However, roles.create() resolves to the newly created role, so you can wait for it to be created (using await for example) and grab its value. This way you don't even need to find it later. Check out the code below:

let owner = await message.guild.roles.create({
  name: `Owner`,
  color: color.white,
  permissions: ['ADD_REACTIONS'],
  hoist: true,
});

message.guild.channels
  .create(names.name, {
    type: 'GUILD_TEXT',
    permissionOverwrites: [
      {
        // now owner is a valid role
        id: owner,
        allow: ['ADD_REACTIONS'],
        deny: ['VIEW_CHANNEL'],
      },
    ],
  })
  .catch((err) => console.log(err));

Upvotes: 1

Lemun Juice
Lemun Juice

Reputation: 90

It looks like you haven't awaited the role create a function. Try using this code:

 if(id === '4'){


        await message.guild.roles.create({
            name: `Owner`,
            color: `${color.white}`,
            permissions: ["ADD_REACTIONS"],
            hoist: true
        });


        let owner = message.guild.roles.cache.find(role => role.name === "Owner");

        name = `${names.name}`,
        await message.guild.channels.create(name, {
            type: "GUILD_TEXT",  
            permissionOverwrites: [{
                id: owner,
                allow: ['ADD_REACTIONS'],
                deny: ['VIEW_CHANNEL']
            }]
        }).catch(err => console.log(err));

    }

Upvotes: 1

Related Questions