Hey today I'd like give and remove roles from a user by their ID
First try:
const user = '5454654687868768'
const role = '451079228381724672'
user.roles.remove(role)
Second Try:
const user = '5454654687868768'
const role = '451079228381724672'
user.removeRole(role)
Neither one of these methods seems to work, however.
const user = '5454654687868768'
const role = '451079228381724672'
These are just numbers which happen to be the id of your user and role object. They are nothing on their own and you can't call any Discordjs methods on them. To get the user and role object you will first have to get them using their respective ID.
Let's assume you want to add a role when someone joins the server, you can do the same thing in any type of event but we will use guildMemberAdd event for example:
bot.on('guildMemberAdd', async member => {
const memberID = '5454654687868768'; // you want to add/remove roles. Only members have roles not users. So, that's why I named the variable memberID for keeping it clear.
const roleID = '451079228381724672';
const guild = bot.guilds.cache.get('guild-ID'); // copy the id of the server your bot is in and paste it in place of guild-ID.
const role = guild.roles.cache.get(roleID); // here we are getting the role object using the id of that role.
const member = await guild.members.fetch(memberID); // here we are getting the member object using the id of that member. This is the member we will add the role to.
member.roles.add(role); // here we just added the role to the member we got.
}
See these methods are only working because they are real discordjs objects and not some numbers like you were trying to do. Also the async/await thing is there because we need to wait for the bot to get the member object from the API before we can add role to it.
Your first try was actually not that far off. The only problem is that roles.remove() is a guildMember function.
So first we need to define the member.
const member = message.guild.members.cache.get("member ID here");
Now we can remove or add a role. Source
member.roles.remove("role ID here");
Related
is there a way to delete all users from certain groups in a limit-saving way?
The way to delete every user one by one from a group seems very time consuming to me.
There are both possibilities, via a bot (Discord.js) that is already on the server or the Discord API.
I'd say your best bet is to just delete the role. The Role.delete method returns the Role after it has been deleted. You can just pass that exact return value back into guild.roles.create to clone the options from the previous role, in case you need to keep the properties of the Role but do not want the members anymore.
Ex:
declare const client: Client<true>
const guild = await client.guilds.fetch(`some-guild-id`)
const role = await guild.roles.fetch(`deleting-role-id`)
if (role) {
const deleted = await role.delete()
const recreated = await guild.roles.create(deleted)
}
This would be a pretty clean solution.
Is there a way to get "member" from "user"?
I use slash command and i want to add role to a member. But there is no way to find member in slash command. Here the way how I find user.
const user = interaction.options.getUser('user')
when i try
user.roles.add(role id)
some error occur:
user.roles.add(role id)
^
"add" is not a function
When you get the user through interaction.options, you receive a User object. But to add roles, you need a GuildMember object. So you have to change user to:
const user = interaction.guild.members.cache.get(interaction.options.getUser('user').id)
Easier Way
At the time of writing the answer, I didn't know we could do this but you can just use interaction.options.getMember('user') to get the GuildMember details of the mentioned user. Even if you use .addUserOption to add the option, this method would work. So you can do this:
const user = interaction.options.getMember('user')
I am making a verification bot and I want to do a command where if somebody unlinks their Roblox account then it removes the verified role from them in all the servers they are in. I know how to do it in a single server like this:
role1 = discord.utils.get(ctx.guild.roles, name='Verified')
await ctx.author.remove_roles(role1)
But how would I do it across all of the servers the member is in with the bot. Thanks!!!
A role is unique per guild, you'd need to iterate through every guild, get the member and role, and remove it.
for guild in bot.guilds:
member = guild.get_member(member_id) # Change the ID accordingly
if member is not None:
role = discord.utils.get(guild.roles, name='Verified')
await member.remove_roles(role)
This code loops through every guild the bot is in, tried to get the member, if it's not a nonetype (cause the member doesn't have to share that specific guild with the bot), it gets the role obj and removes it.
Reference:
Bot.guilds
Guild.get_member
Also you need intents.guilds and intents.members
Pretty much the title. Every time I try adding a role to someone using this:
const member = message.author;
member.roles.add('732727208774205460');
I end up with this TypeError:
TypeError: Cannot read property 'add' of undefined
However, if I use it like this:
const member = message.mentions.members.first();
member.roles.add('732727208774205460');
It works completely fine. Problem is, that only works if the person I'm adding a role to was mentioned/pinged by the user. I'm trying to add a role to the user himself where the user doesn't ping anyone (for censorship, mainly). Is there a way I could do this without getting the TypeError?
You are receiving an error because message.author.roles is undefined, so trying to use .add() on it causes an error.
message.author is of type User, which represents a discord user, and is not associated with any particular server.
You're looking instead for message.member which has type GuildMember, which represents a particular user's profile inside of a guild (message.member is just message.author but as a GuildMember instead of a User). It is the GuildMember type that has roles.
Solution:
const member = message.member;
member.roles.add('732727208774205460');
Official Documentation:
User,
GuildMember
I am making a "user info" command that returns the user's Discord username, their ID, their server join date and whether or not they are online. I am able to display all the information through user.id, user.username, and user.presence.status. But when I try to use user.joinedAt I get undefined in the display.
I know this is because the User class and the GuildMember class are not the same, and that the GuildMember class contains a User object.
But my problem is: how I could get the .joinedAt data from my user mention?
Here is my current code:
let user = message.mentions.users.first();
let embed = new Discord.RichEmbed()
.setColor('#4286f4')
.addField("Full Username:", `${user.username}#${user.discriminator}`)
.addField("User ID:", `${user.id}`)
.addField("Server Join Date:", `${user.joinedAt}`)
.addField("Online Status:", `${user.presence.status}`)
.setThumbnail(user.avatarURL);
message.channel.send(embed);
Here's the code for my user info command:
if (msg.split(" ")[0] === prefix + "userinfo") {
//ex `member #Rinkky
let args = msg.split(" ").slice(1) // gets rid of the command
let rMember = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0])) // Takes the user mentioned, or the ID of a user
let micon = rMember.displayAvatarURL // Gets their Avatar
if(!rMember)
return message.reply("Who that user? I dunno him.") // if there is no user mentioned, or provided, it will say this
let memberembed = new Discord.RichEmbed()
.setDescription("__**Member Information**__")
.setColor(0x15f153)
.setThumbnail(micon) // Their icon
.addField("Name", `${rMember.username}#${rMember.discriminator}`) // Their name, I use a different way, this should work
.addField("ID", rMember.id) // Their ID
.addField("Joined at", rMember.joinedAt) // When they joined
await message.channel.send(memberembed)
};
This will send an embed of their user info, this is my current code and
rMember.joinedAt
does work for me.
edit:
After looking at your question again, I found out I didn't need to post everything, you can't get the joined at because it's just the mention. Try this:
let user = message.guild.member(message.mentions.users.first())
Should work
You can technically find it by fetching getting the member from the guild and then using GuildMember.joinedAt: since the User class represents the user in every guild, you will always need the GuildMember to get info about a specific guild.
let user = message.mentions.users.first(),
member;
if (user) member = message.guild.member(user);
if (member) embed.addField("Server Join Date:", `${member.joinedAt}`);
With this said, I would not suggest you to do that, since it's not really efficient. Just take the mention from the members' collection and then take the user from that.
let member = message.mentions.members.first(),
user;
if (member) user = member.user;
The downside of this is that you can't use it if you want your command to be executable from the DMs too. In that case you should use the first method.
const member = message.channel.guild.members.cache.find(member => member.user == message.mentions.users.first())