Ami Noo
Ami Noo

Reputation: 23

How to get permissions in discord js when we do direct message

I am trying to get a list of the permission that the user has in Discord. If it sends the message in the channel, it’s fine as we can use message.member.hasPermission, etc. But what if the message is DM? I want my users to send a DM to the bot and the bot be able to check and see if the user has certain permissions. I cannot find anything anywhere. I keep getting redirected to message.member, or message.guild which both are null when it’s in DM.

Upvotes: 2

Views: 1857

Answers (2)

MrMythical
MrMythical

Reputation: 9041

In DM no one has permissions. All you have is the permission to see messages and send messages which aren’t shown visually, or to bots. To make sure that it isn’t DM, just return if the channel type is dm, or guild is null.

if(!message.guild) return;
//before checking "perms"

If you want the permissions for a certain guild if the message is from DM, use this code

if(message.channel.type === 'dm') {
let guild = await client.guilds.fetch('THE ID OF THE GUILD YOU WANT TO SEE THE USER’S PERMS IN')
let member = await guild.members.fetch(message.author.id);
//you can now access member.permissions
}

Keep in mind await must be in an async callback.

You did not provide any code so I cannot give any more code than this.

Upvotes: 1

Elitezen
Elitezen

Reputation: 6710

You can fetch the member with the guild's context.

Fetching is recommended, as Guild#member() relies on cache and is also deprecated.

Member#hasPermission() will be deprecated as well, using MemberRoleManager#has() is recommended

The following example uses async/await, ensure you're inside an async function

// Inside async function
const guild = await client.guilds.fetch('guild-id');
const member = await guild.members.fetch(message.author.id);
const hasThisPermission = member.roles.cache.has('permission');

Upvotes: 1

Related Questions