if (message.content.startsWith(prefix + "nickname ")) {
var nick = message.content.replace(prefix + "nickname ", "");
message.member.setNickname("nick");
message.channel.send('You have *changed* your **nickname** to "' + nick + '" **!**');
}
I am trying to make a command to change the authors nickname through text. Unfortunately it throws an error of "Privilege too low..." even though my bot has the Manage Nicknames permission, Administrators and is even at the top of the list of roles in server settings. Anyone have any ideas?
EDIT: According to the answer, the bot of the server is not allowed to modify the nickname of the server superior, despite having the highest role. I ran the code on an alternate account and it ran flawlessly. I just don't get the privilege of nicknaming myself (which can essentially be done via the /nick command that discord implemented)
Discord.js implements changing nicknames by getting the GuildMember from the message, and using the GuildMember#setNickname method. Here's a simple example of setting the nickname of the user who ran the message:
if (message.content.includes('changeName')) {
if (!message.guild.me.hasPermission('MANAGE_NICKNAMES')) return message.channel.send('I don/'t have permission to change your nickname!');
message.member.setNickname(message.content.replace('changeName ', ''));
}
Related
I'd like to auto set roles for new users when they join server X, problem is that this bot is on server Y as well, and server Y doesn't have the role.
client.on('guildMemberAdd', member => {
console.log('User ' + member.user.tag + ' has joined Steampunk.');
var role = member.guild.roles.find(x => x.name === "name");
member.addRole('247442955651121154');
})
I was hoping that I could do a simple check before applying the role, if user have joined server X, add the role, if else, do nothing. So far my attempts have failed.
Any input would be appreciated!
The original code is trying add a specific role you put in, and not the role you just looked up a line before that. There shouldn't be any issues with other servers the bot is in, because member.guild gives the guild of the member object, and since this member object is coming from a guildMemberAdd it means that member.guild will only return the guild that was just joined. The code in the edit has a lot of strange things going on, though.
The first issue I see is that in order to look through the channels in a guild, you have to use guild.channels.cache.find(), guild.channels.find() is old now and doesn't work in v12. Same goes for client.guild, guild.roles, etc. The part at the bottom of your edited code doesn't seem to make much sense at all. The code should get the roles of the newly joined guild (member.guild.roles.cache), look through them to find the role you want (you did this correctly with the .find()) and then apply the role to the member. Neither server nor role ever get used in your code, and guild.id is a property, not a method.
Let me know if you need help following these instructions, although it looks like you should be able to do it now. Always look at the discord.js docs first to check properties, methods, etc.
I ended up defining the guild in question by name, saved it as a const, and made a check for the specific guild.
If your bot is on several servers but you only want the greet/addRole on a specific one, all integrated into the message displayed on joining;
//Welcome message and autorole for specific server and channel.
client.on('guildMemberAdd', member => {
const = client.guilds.find(guild => guild.name === "Steampunk");
if (!guild) return;
const channel = member.guild.channels.find(ch => ch.name === "greetings");
if (!channel) return;
let botembed = new Discord.RichEmbed()
.setColor("#ff9900")
.setThumbnail(member.user.avatarURL)
.setDescription(`${member} Welcome to the Ark Steampunk Sponsored Mod Discord!\nPlease report all <#244843742568120321> and if u need any help view or ask your question in <#334492216292540417>. Have any ideas we got a section for that too <#244843792132341761>. Enjoy!:stuck_out_tongue: Appreciate all the support! <http://paypal.me/ispezz>`)
.setTimestamp()
.setFooter("Steampunk Bot", client.user.avatarURL);
channel.send(botembed);
console.log('User ' + member.user.tag + ' has joined ' + member.guild.name + '.');
var role = member.guild.roles.find(x => x.name === "name");
member.addRole('247442955651121154');
});
I am currently developing a discord.js bot for my server, and it has an integrated "Level System" that works with nicknames.
So that means the bot sets the nickname of all users to their display name and simply adds the level they are at the end of it:
bot.guilds.get("693909572167139338").members.forEach((member) => {
if(member.user.bot) return;
const entity = new LevelEntity(member);
bot.levelEntities.set(member, entity);
});
bot.levelEntities.forEach((entity) => {
if(entity.getLevelHolder().displayName.includes("Lv. ")) return;
if(entity.getLevelHolder().displayName.length >= 32) return;
entity.getLevelHolder().setNickname(entity.getLevelHolder().displayName + " " +
entity.getLevel());
console.log(`[${moment().format('DD/MM/YY, h:mm a')}]`.italic.yellow + ` LEVELENTITY `.cyan
+ `Set Level Nick for `.green + `${entity.getLevelHolder().user.username}`.grey);
});
The point is: The bot cannot change my nickname because Im the owner.
My Question: How can I achieve, that the bot can change my nickname or is it even possible?
Greetings,
Linus E.
Sorry, this cannot be done due to discord restrictions.
I want to make a small if statement to see if my bot has administrator privileges in the server.
[bot = new Discord.Client();]
Any help?
I tried:
if(!bot.guild.hasPermission("ADMINISTRATOR") return msg.author.send(":x: I need administartor priviliages in"+bot.guild.name+"! :x:")
bot.guild (client.guild) doesn't exist.
client.guilds is a collection of all the guilds your bot is in, mapped by their IDs.
You can check if a member / the bot has a permission using GuildMember's .hasPermission method.
if (!message.guild.members.get(client.user.id).hasPermission("ADMINISTRATOR")) return message.reply("I need Administrator permissions!")
Shortest way possible would be
if (!message.guild.me.hasPermission("ADMINISTRATOR")) return;
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
So I've looked at a fair few forum posts and can't find something fitting my code.
I'm trying to do something like this...
case "setnick":
if (args[1]) {
if (message.guild.me.hasPermission("MANAGE_NICKNAMES")) {
if (message.member.hasPermission("CHANGE_NICKNAME"))
message.member.setNickname(args[1])
else message.channel.send("You do not have the permissions! **(CHANGE_NICKNAME)**")
}
else message.channel.send("The bot does not have the permissions! **(MANAGE_NICKNAMES)**")
}
else message.channel.send("There's no argument to set your nickname too! **Eg. f!setnick NICKNAME**")
break;
So it checks if there is a argument like
f!setnick NICKNAME
Then checks if the bot has permission MANAGE_NICKNAMES
if it doesn't it sends a chat message.
And it then checks for if the user has the permission CHANGE_NICKNAME
so i'm wondering why its not working.
Any reason why?
Have you tried checking if it works on other users besides yourself? Bots can not do administrator commands (like change someone's nickname) if you are higher/the same in the hierarchy than it, and seeing the owner is the ultimate power it is probably returning a permission error.
Try catching it and see what the error is
message.member.setNickname(args[1]).catch(e=>console.log(e))
if it returns DiscordAPIError: Privilege is too low... then my theory is correct. It should work for lower users.
message.member.me doesn't get the bot's user, it gets the user sending the message. Try message.guild.members.find("id", client.user.id).hasPermission("MANAGE_NICKNAMES")