Self Permission Check - javascript

How do I check if the bot (Self) has permissions to send messages and make it an if statement? Im using discord js and I've seen forums like member.roles.cache has, but none cover the bot itself, so how do I check that?

As Elitezen said, you can get GuildMember object using Guild#me but to check if your bot can send messages for example in guild where the command was executed you have to use this:
if(message.guild.me.permissions.has("SEND_MESSAGES")) { // check if bot has SEND_MESSAGES permission
// your code
}

You can use Guild#me to get the client's GuildMember object in that guild.
// guild = message.guild or any guild object
if (guild.me.roles.cache.has(...)) {
}
Update: in Discord.JS V14 you now have to use guild.members.me

Related

Member of channel in discord

I am developing discord bot via Discord.js and I need to get all the members that are in voice channel in this moment. How can I do that?
I tried
message.guild.channels.find(c => c.id === id here)
but it says that find is not a function.
Also, I tried
client.channels.get("name", "name of channel")
but it says that get is not a function.
DJS uses a cache, so the find function is client.channels.cache.find.

use react() from DM

I want to use .react() from DM way. But the reaction into the post is sended by bot. I need send a reaction by the user.
var guild = bot.guilds.get(GUILD_ID);
if(guild && guild.channels.get(ch_ID)){
guild.channels.get(ch_ID).fetchMessage(msg_ID).then(message => {
message.react('🤔')
})
}
As I shared in a comment...
A client cannot add a reaction as another user.
Unfortunately, that means that you can't accomplish what you're trying to do within the Discord API. You can read reactions, but the bot simply would never be able to force someone to add a reaction.

Checking if my bot has permissions to manage roles

So, the title briefly explains what I want.
When a member joins my Discord server, I want the bot to assign them the User role automatically. Before that happens, I want to verify if the bot has permission to manage roles and so far I have the following in my code.
/** Event will trigger when a user joins the server **/
bot.on("guildMemberAdd", function(member) {
if (!bot.user.hasPermission("MANAGE_ROLES")) {
var embed2 = new discord.RichEmbed()
.setTitle("Error")
.setDescription("The bot doesn't have the appropriate permissions to assign new members roles automatically.\nTo resolve this problem, go to **Server Settings** and then navigate to the **Roles** option. Next, click on **Bots**, and then click on the slider next to **Manage Roles** to activate it.")
.setColor("#3937a5")
bot.channels.get("466878539980079105").send(embed2);
} else {
member.addRole(member.guild.roles.find("name", "User"));
}
});
If the bot doesn't have the permissions, it will get the attention of staff by posting an alert to the #alert channel.
However, I attempt to join with a new Discord account, it reports in the console that hasPermission isn't a function, and then stops.
Would there be any suggestions on how to resolve this code?
On Discord.js we have users and members. Users is a user of Discord. Member is a member of a Discord Server. The user object doesn't have information about roles, nicknames, permissions. Members do, because they are related to that server.
So if you want to grab the bot as the member of the guild you need to use something like this:
<guild>.me
This will return the member object of the bot from the selected guild.
When a member is added to the guild you get the GuildMember object, and with that you also get the guild. So you can use this:
member.guild.me
And finally to check the bot has permissions:
if(member.guild.me.hasPermission("MANAGE_ROLES"))
Note:
hasPermission() and hasPermissions() will be deprecated in DiscordJS v13 and replaced with permissions.has()
e.g : member.permissions.has(Permissions.FLAGS.SEND_MESSAGES);
the error message is correct, user has no function called hasPermission. but the role property does.
https://discord.js.org/#/docs/main/stable/class/Role?scrollTo=hasPermission

Discord.js: Checking Bot Permissions

This is probably a nub question, but I can't seem to figure it out. I'm trying to make my bot check if it has a permission, and send a message if does not. I'm guessing it like this code to check it a member has a permission:
message.member.hasPermission("MUTE_MEMBERS")
Is it like that to get a bots permissions? Any help would be appreciated!
message.member gets the GuildMember object of the author who sent the message. Looks like you actually want to get the GuildMember object of the client instead. You can do this by doing <Client>.guild.me and then call .hasPermission(...) on this.
If you want to check if the bot has a permission you can do something like:
if(message.guild.me.hasPermission("MUTE_MEMBERS"))
console.log("I can mute members!!")
else
console.log("I CAN'T mute members!")
F.M.
message.guild.me.hasPermission("MUTE_MEMBERS")
In discord.js V13 you can check for permissions this way:
if (message.member.permissions.has("MUTE_MEMBERS")){
// ....
} else {
//....
}
if(!message.member.hasPermission("PERMISSION") return message.channel.send("You don't have permission to use this command")
Embeds:
const embed = new MessageEmbed()
.setTitle("No Permission")
.setDescription("You don't have permission to use this command")
if(!message.member.hasPermission("PERMISSION") return message.channel.send(embed)

Discord.js client.addMemberToRole not working

So, I'm programming a Discord bot, and one of the things I want it to do is to assign roles to members given certain conditions. After looking through the documentation, specifically here, I figured bot.addMemberToRole would be a good command to use. However, when I ran it, I got this error message:
TypeError: bot.addMemberToRole is not a function
I was understandably confused, as the documentation clearly says that this IS a function. I have tried doing bot.addMemberToRole(member, role);, addMemberToRole(member, role);, and several other iterations. This is my most recent attempt:
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.addMemberToRole(member, role, function(err){
if(err){
console.log(err);
}
});
I have also done just this:
bot.addMemberToRole(member, role);
Both gave the same TypeError as above.
I have no idea why it doesn't work. I followed the documentation exactly, the member and role variables I pass into it are the proper type, and other Discord.js commands work just fine in my bot. Any help would be appreciated.
You're using an old version of the docs so that function doesn't exist anymore. They should really get rid of those. You're looking for GuildMember.addRole(Role or String).
To add a member to a role, you need a GuildMember and a Role object (or the name of the role). Assuming you have the User object and the Guild object (your bot has a list of the guilds/servers it's joined and most events will have the guild they're associated with), you can get the GuildMember by using Guild.fetchMember(User). From there, you can add the role on the GuildMember using either the string or object based version of addRole.
Here's an example of how to do it upon receiving a message from a user which is very easy since the message has a GuildMember associated with it.
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.on('message', (message) => {
const guildMember = message.member;
guildMember.addRole('bot-added-role');
});

Categories