iwanttolearnts
iwanttolearnts

Reputation: 37

How to check for permissions from interaction in discord.js

I need help understanding how to write this in typescript. I tried this in javascript and it works.
(returns true or false whether the user who made the interaction has the administrator permission)

execute(client, interaction) {
    console.log(interaction.member?.permissions.has("ADMINISTRATOR"));
  }

My try in typescript:

execute(client: MyClient, interaction: CommandInteraction) {
    console.log(interaction.member?.permissions.has("ADMINISTRATOR"));
  },

Typescript has an error on .has:

Property 'has' does not exist on type 'string | Readonly<Permissions>'.
Property 'has' does not exist on type 'string'.

How can I fix this?

Upvotes: 2

Views: 5425

Answers (3)

Mohan Roy
Mohan Roy

Reputation: 1

//it is changed, use this:

if (interaction.memberPermissions.has('yourpermission'))
{
// your code`enter code here`
}

Upvotes: 0

MrMythical
MrMythical

Reputation: 9041

This is due to CommandInteraction.member being ?GuildMember | ?APIGuildMember. If you click the APIGuildMember link you will see that permissions is type string. I’m not really familiar with TS but I think this is a solution

execute(client: MyClient, interaction: CommandInteraction) {
   const member = interaction.member as GuildMember
console.log(member?.permissions.has("ADMINISTRATOR"));
  }

Upvotes: 0

Elitezen
Elitezen

Reputation: 6730

Try casting interaction.member as a GuildMember

// Namespace 
import { GuildMember } from "discord.js";

execute(client: MyClient, interaction: CommandInteraction) {
   const member = interaction.member as GuildMember;
   console.log(member.permissions.has("ADMINISTRATOR"));
},

Upvotes: 3

Related Questions