make an autorole for discord.js v12 - javascript

I wanna make my bot give automatically role when my friends join for first time to my server: I have tried this one my VPS node version: 12.19.0v:?
client.on('guildMemberAdd', member => {
console.log('User #' + member.user.tag + ' has joined the server!');
var role = member.guild.roles.cache.find(role => role.name == "Newbie")
let user = member.user
user.roles.add(role);
});
but nothing is working ! help me pls

Discord is now enforcing privileged intents. The GUILD_MEMBERS intent is required to receive events such as guildMemberAdd and guildMemberUpdate.
To learn how intents work and how to use them, check out discord.js' detailed guide.
Also, as stated in the comments, you need to use message.member instead of message.user

You need to know the difference between Members and Users. A user represents a Discord account, a member represents a user being in a server. So for example to get someone's tag, you need to do user.tag, member.tag wouldn't work. It's the same with nicknames, user.displayName woudln't work, because a user is an account, a member is a user in a server. You'd do member.displayName instead.
Anyway, where you put
let user = member.user
user.roles.add(role);
it should be replaced with
member.roles.add(role);
and that should hopefully solve your problem.

Related

how to make a setNickname command in discord.js v13

I want to create a Change Nickname command in discord.js v13, and It's not working.
My code:
const target = message.mentions.members.first();
const nickname = args.slice(1).join(' ');
if (!target) return message.channel.send('Please specify a target to change nickname');
if (!nickname) return message.channel.send('Please specify a nickname to change');
target.setNickname(nickname);
I am using node.js v16
Your code is working for me, but make sure your bot has the following Permissions and the bot's role is above the role of the users who want to edit his nick:
Change this:
https://i.stack.imgur.com/lKP9h.png
To this:
https://i.stack.imgur.com/xX8GF.png
Also make sure your command is lowercase because uppercase characters are not allowed in command names.
I'm going to throw a guess that the nickname you are trying to set is null or empty, this will cause discord to just reset the nickname to the users normal discord username.
Make sure to debug the values that are being passed on and providing such information when making a question on here as it will help people more easily help you.
With that said, the below code worked fine for me
const target = msg.mentions.members.first();
if (!target) return msg.reply('Please mention a user');
const nick = args[1];
if (!nick) return msg.reply('Please provide a nickname');
const oldNick = target.nickname;
if (oldNick === nick) return msg.reply('That user already has that nickname');
console.log(`Changing ${target.user.tag}'s nickname from ${oldNick} to ${nick}`);
target.setNickname('');
The code you provided seems to work correctly. The error might be your bot's intents. Make sure that you enabled/requested all intents you need for this command (guild members if I remember correctly). Make sure that you also gave your bot the required permissions: MANAGE_NICKNAMES in the server settings (roles).
Good luck!

discord.js Command that gives/removes role from everyone, and only an Admin can use it

I would like to make a command that gives everyone a role, and only an admin can use it.
I found this piece of code on the internet and I tried to modify it, but nothing is helping me out, and I've been reading the error, and I still get nothing
client.on("message", message => {
if (message.content === 'grimm!rainbow') {
let role = message.guild.roles.cache.find(r => r.name == 'Rainbow')
if (!role) return message.channel.send(`a Rainbow role was not found, create one and set it on top of all roles for this command to work!`)
message.guild.members.filter(m => !m.user.bot).forEach(member => member.addRole(role))
message.channel.send(`**${message.author.username}**, The Rainbow has been turned on!`)
}});
And another thing, I would like it if this command can only be used by an admin, but I've been struggling with code in that range, and I have no clue on how to work it.
If someone could help me out a little bit? and explain what I'm doing wrong, I would really appreciate it! Thanks!
To check if user have admin permission you need to using .hasPermission() docs here:
if(message.member.hasPermission('ADMINISTRATOR'){
// Doing Something
}
To add roles to all user you need to access guild members cached , doc here and then loop through all members , access their roles and you will have .add() for add specific role docs here. Similar for remove
message.guild.members.cache.filter(m => !m.user.bot).forEach(member => member.roles.add(role))

How to get list of all members with a role(Online and Offline) in discord.js

I want to write a command for my bot that can get all members of a role, irrelevant of whether or not the are online. As of yet I have tried with variety of ways, including:
message.guild.roles.get('someRoleId').members.map(m=>m.user.id);
and the same using the .roles.find('name', 'someRoleName').
Thou these only seem to give me the online members. Therefor I'm wandering if there is a way to get all relevant members, no mater activity state.
You almost got it right.
What you need to do is
message.guild.roles.get('someRoleID').members.map(m => m.user.id); // change "m.id" to "m.user.id"
Maybe this can help you
let role = message.mentions.roles.first();
if (!role) role = message.guild.roles.cache.find((r) => r.id == args[0]);
if (!role) return message.reply("That role not exist!");
const listMembers = [];
role.members.forEach((user) => {
listMembers.push(user.user.tag);
});
return message.channel.send(listMembers.join(", "));
It works by mentioning the role or with the id
Leoncito is Online, Elly Offline
Hi I had the same problem. I assume you are using Discord js v13?
With the new update you can't access everything so easily.
You need to add "GUILD_PRESENCES" in you're intents.
Then after this you get all members offline and online.

How do I get a discord bot to mention a role and a user?

I am trying to make a bot that, whenever someone says something, it sends a message in the channel saying that that person is there and is waiting for someone to join. However, it only prints #ne1 (the role I want it to mention) as plain text. Also, when I have it mention the user, it only writes the user ID instead of actually sending #(username).
const Discord = require('discord.js');
const keepAlive = require('./server');
const client = new Discord.Client();
client.on('message', message => {
if (message.toString().toLowerCase().includes('ne1 here')) {
message.channel.send('#ne1, ' + message.author + ' is online and waiting for you to join!');
}
});
keepAlive();
client.login(process.env.TOKEN);
What I want is for it to mention the #ne1 role and the person who used the command to run the bot. It only prints, "#ne1 571713056673890324 is online and waiting for you to join!"
Discord mentions (role, user, etc.) have a special format as detailed in the documentation here.
So, to mention a user you need to use their user ID <#USER_ID>/<#!USER_ID> and for a role it is similarly <#&ROLE_ID>.
In order to mention a user, you must have their User ID and use the following format:
<#USERID> or <#!USERID>. For roles do <#&ROLEID> instead.
Make changes to your message to accommodate it:
message.channel.send(`<#&${ROLEID}> <#${message.author}> is online and waiting for you to join!`);
I guess you are using discord.js v12
If you want the easiest way to mention user you can use
message.channel.send(`#ne1, ${message.author} is online and waiting for you to join!`);
Also I'd use ID of role instead of just #role
You can get the ID by using #role and then just using the thing discord outputs
Usually something like <#&randomnumbers>

Add role to user from DMs?

So I'm making a verification bot for one of my Discord Servers, and I've come across a problem.
Purpose:
When the user first joins, they will be only allowed to talk in one specific channel, the verification channel. When they type $verify {username on-site}, the bot will reply in-server telling the user to check their DMs to continue. A unique string linked to a verification row in the database will be given, and the discord user will be told to change their status on-site to the given unique ID. Once they do that, they will be given the "Verified" role and will be able to speak on the discord.
The problem:
When everything goes right, and the user is ready to be given the role on the discord server, the bot will not give the role. I'm assuming this is because the user that the bot is trying give the role to is not linked to the actual discord server for it, because it's in DMs.
What I'm looking for:
If possible, give me an example on how to give a role to a user on a discord server from DMs, or documentation. That would be really helpful!
Code: I'll only include the part that is important, there is no other errors.
snekfetch.get("https://vertineer.com/api/getUniq.php?discord_id="+message.author.id).then(function(r){
console.log("here 2");
let uniq = JSON.parse(r.text);
if(uniq.id == uniq.status){
message.member.addRole("Verified");
message.reply("Success!");
} else {
message.reply("Looks like you didn't change your status to `"+uniq.id+"`, try again with `$finish`.");
}
});
If this is only to work for one server you can do:
client.guilds.get('myguildID').members.get(message.author.id).addRole('193654001089118208')
And also, to give a role to a user you can't use the name. You need to use the ID or the Role Object.
If you would prefer to use the name you would need to do something like this:
var role = client.guilds.get('myguildID').roles.find(role => role.name === "Verified");
client.guilds.get('myguildID').members.get(message.author.id).addRole(role);
This is what worked for me:
client.guilds.fetch(<guild.id>).then(async(guild) => {
let role = guild.roles.cache.find(r => r.name === <role.name>);
let member = await guild.members.fetch(<member.id>);
member.roles.add(role);
});
Substitute <guild.id>, <member.id> and <role.name> for the desired values respectively

Categories

Resources