discord.js change nickname of server owner - discord

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.

Related

Discord.js bot command to take users with a specific role and to return an embedded message with all the users in that role

I've been working on this and I cannot figure out why the bot will not enact the command. Essentially this is how the situation should work:
In the channel you send a message: "!listroletest"
After that, the bot should send an embedded with every user who has the role "test" with their userID's
client.on("message", message => {
if(message.content == "!listroletest") {
const ListEmbed = new Discord.MessageEmbed()
.setTitle('Users with the test role:')
.setDescription(message.guild.roles.get('901592912171765850').members.map(m=>m.user.tag).join('\n'));
message.channel.send(ListEmbed);
}
});

Set autorole on specific guilds only

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');
});

discord.js DiscordApi: missing permissions

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.

Trying to get my discord.js bot to mention a user

I have recently been trying to figure out how to get my discord.js bot to welcome new users by tagging them in a message, but it just comes out as #user with their discriminator. Any help/suggestions would be appreciated.
Here is the code:
client.on("guildMemberAdd", (member) => {
let guild = member.guild;
let memberTag = member.user.tag;
if(guild.systemChannel){
guild.systemChannel.send("#" + memberTag + " has joined the server!")
}
});
In my personal experience, I've always used the global mentioning method, using <#id-of-the-user>. If this is the method you'd like to try, change
guild.systemChannel.send("#" + memberTag + " has joined the server!") with guild.systemChannel.send("<#" + member.id + "> has joined the server!").

Privilege too low, setNickname()

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 ', ''));
}

Resources