discord.js DiscordApi: missing permissions - discord.js

im trying to make a change nickname command
if(isValidCommand(message, "changenick")){
try {
if (mention == null){return message.reply("changenick who? dumb dumb")}
nickname = message.content.slice(8 + mention);
let member = message.mentions.members.first();
member = await member.setNickname(nickname);
}
catch (e) {console.error(e);
return message.channel.send("something went wrong!");}
}
but i get the error DiscordAPIerror: missing permissions even when trying it in my own server

I would suggest the following:
Make sure the bot itself has the MANAGE_NICKNAMES permission.
A user can only change the nickname of a person below them in the role hierarchy, this applies to bots as well.

Related

Check if a user with a specific role reacted the latest bot message | Discord.js

i need to make my bot check if someone with admin role or a specific role reacted the bot's latest message
i made a suggestion command for my bot and i want to the bot check if anyone with the #Admin role reacted the latest bot message of #suggestions channel, then when a user that has the #Admin role react the suggestion, make the bot send me a DM saying something like: Accepted your suggestion!
Here is something that may help:
client.on('messageReactionAdd', async (reaction, user) {
if(reaction.message.channel.id !== 'suggestion channel id') return;
let channel = reaction.message.channel;
let msg = await channel.messages.fetch({limit: 1});
if(!msg || msg.id !== reaction.message.id) return;
if(reaction.message.guild.member(user).roles.cache.some(r => r.id === 'admin role id')) {
user.send('Your suggestion was accepted.')
//You may have said this wrong, but if you want the person who suggested it to be DMd
//You will need to somehow save their name (or id which can never change), let’s say you put it in the footer of an embed for the suggestion
let userID = reaction.message.embeds[0].footer;
msg.guild.members.cache.find(m => m.user.id === userID).send('Accepted your suggestion!')
}
})
I’d like to know if this doesn’t work because I didn’t get to test it. It may have some errors, but hopefully not

add role to a mentioned user in discord.js v12

code:
if(command === "rusr"){
console.log((chalk.yellow)`You ran a command: rusr`)
const id = args[0]
const role = args.slice(1).join(" ")
let user = message.guild.member(message.mentions.users.first()) || message.guild.members.get(id)
user.roles.add(role)
}
error:
UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes.
this is strange cuz i have the EXACT same code but with nickname which works just fine. anyway to fix this?
The error is in const role = args.slice(1).join(" "). That's not the role, thats the name of the role. Instead make it something like
const role = message.guild.roles.cache.find(r => r.name == args.slice(1).join(" "));
So that finds the role with the name specified.
It'll work without, but its good practice to also add if(!role) return message.channel.send('Invalid role!'); after the line where you define role. This means if someone enters the name of a role that doesn't exist, it will stop and tell the user instead of breaking the code.

Discord.Js add role

I want to do a mute command, but when I do the command, the console output is (node:67916) UnhandledPromiseRejectionWarning: TypeError: msg.guild.roles.get is not a function
Any ideas how i can fix that?
As the error message says, msg.guild.roles.get is not a function. Its quite hard to answer a question without seeing the code, you should always share your code, but the correct way to find and add a role is the following:
const role = message.guild.roles.cache.find(role => role.name === 'Muted');
const member = message.mentions.members.first();
if(!member) member = message.author;
member.roles.add(role);
Since the update to V12, it is important that when trying to get roles, members or guilds you must include the .cache bit.

Issue adding roles to Discord members programmatically

I've got an issue when trying to add a role to a member programmatically. member.roles.add is throwing the following error:
(node:9420) UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes.
I'd tried to pass in the id and name of the role to the add method but this did not work either. What am I doing wrong here? Any pointers in the right direction would be much appreciated.
Below is the functionality to apply the role itself (within its context) for better understanding of my issue:
bot.on('message', msg=>{
if(msg.content.match(/[`!##$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/)) {
if((msg.content.match((new RegExp(/[`!##$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/, "g")) || []).length) >= 30) {
msg.channel.send(`${"<#" + msg.author.id + ">"}. Your message has been deleted for containing too many special characters.`);
const guild = bot.guilds.cache.get(GUILD_ID_HERE);
let mute_role = msg.guild.roles.cache.find(role => role.name === 'spammer');
var member = guild.members.cache.get(msg.author.id);
member.roles.add(mute_role);
msg.delete();
}
}
...

Trying to implement Patreon-only commands/functions on my discord bot, how would I accomplish this?

My discord bot gives the role of 'Patreon' to my patreon supporters. This role is given on my main discord bot server. So right now I'm trying to write some commands that would be only available to users who have the role 'Patreon' in the BOTS discord server, how can I accomplish this?
Like is there a way I can be like -
message.member.has('Patreon Role').in('My Discord Server)?
Let's go over the tasks you need to accomplish this.
Get the "home guild" with your users and corresponding Patreon role.
See Client.guilds and Map.get().
Find the user in the guild.
See Guild.member().
Check whether or not the user has the Patreon role.
See GuildMember.roles and Collection.find().
You can define a function to help you out with this, export it and require it where you need it (or define it within relevant scope), and then call it to check if a user is one of your Patreon supporters.
Here's what this function would look like...
// Assuming 'client' is the instance of your Discord Client.
function isSupporter(user) {
const homeGuild = client.guilds.get('idHere');
if (!homeGuild) return console.error('Couldn\'t find the bots guild!');
const member = homeGuild.member(user);
if (!member) return false;
const role = member.roles.find(role => role.name === 'Patreon');
if (!role) return false;
return true;
}
Then, as an example, using this function in a command...
// Assuming 'message' is a Message.
if (!isSupporter(message.author)) {
return message.channel.send(':x: This command is restricted to Patreon supporters.')
.catch(console.error);
}
message.member.roles.find('name', 'Patreon Role');//this returns either undefined or a role
What that does is it searches the users collection to see if the have "Patreon Role"
If the message is on the same server, otherwise you could do
client.guild.find('name','My Discord Server').member(message.author).roles.find('name', 'Patreon Role'); //this also returns undefined or a role
Clearly that second option is long, but what is basically does is searches the servers the bot is in for a server called 'My Discord Server' then it finds the GuildMember form of the message.author user resolvable, then it searches their roles for the role 'Patreon Role'
There is a chance it will crash though if they aren't on the server(the documentation doesn't say if it returns and error or undefined for some reason) so if it does crash you could instead do
client.guild.find('name','My Discord Server').members.find('id', message.author.id).roles.find('name', 'Patreon Role'); //this also returns undefined or a role
You can read more here: https://discord.js.org/#/docs/main/stable/class/User
and here
https://discord.js.org/#/docs/main/stable/class/Client
and here
https://discord.js.org/#/docs/main/stable/class/Guild
To try and give a full example, assuming this is in your message event
if (message.member.roles.find(r => r.name === 'Patreon') == undefined &&
commandIsExclusive || message.guild.id !== 'this-id-for-BOTS-server') {
// Don't allow them in here
}
Essentially, to run a command they must be a supporter, in a specific server and if it is exclusive and the other criteria aren't met, they are denied

Resources