Discord JS message.member.roles.cache.some - discord

i tried making a command with role restrictions, and i'm having problems with role restriction itself.
if(!message.member.roles.cache.some(role => role.name === 'Role1')) return message.reply("You can't use the command!")
So, if i use only 1 role as restriction it works completey fine, but as long as i add second role
if(!message.member.roles.cache.some(role => role.name === 'Role1'||'Role2')) return message.reply("You can't use the command!")
It turns into a mess, anyone, even a person without any roles can use the command! I tried many ways of making it work, i tried using Role ID's instead of names like this
message.member.roles.cache.has('role-id-here'||'role2id-here');
and still, it didn't work!
I would appreciate any help to me with that.Thanks!

Your conditional is incorrect. It is not set up correctly and I think you may not understand the OR operator || completely.
// your original
role.name === 'Role1' || 'Role2'`
The or operator || is misused in this statement. The || returns the left side if that value is true and returns the right side if the left value is false. The left side of your original condition is role.name === 'Role1 therefore if this is false return 'Role2'
What you want to do is
role => (role.name === 'Role1' || role.name === 'Role2')

Related

Discord.js Add a role to members with a specific role

I wanted to make a command, that adds a role to members with a specific role.
EXAMPLE
i do the command !roleall
The commands finds members with the role "test123", and adds a role called "test111" to members who has that role.
I tried this but didnt work:
const Role2 = message.guild.roles.cache.get("981657701962629172");
message.guild.members.filter((role => role.name === '🌍・Verified')).forEach(member => member.addRole(Role2))
One way to do that would be to first fetch both the roles, then filter through all the members in the guild, check which member has the first role and then add the second role to them. We can fetch the roles by using message.guild.roles.cache.get() if you have the role id or message.guild.roles.cache.find() if you have the role name. Then, to filter through the members, we can use message.guild.members.cache.forEach() and then use member.roles.cache.has(role-id) to check if the member has the role. The code might look something like this:
const role1 = message.guild.roles.cache.get('981657701962629172')
const role2 = message.guild.roles.cache.find(r => r.name === '🌍・Verified')
message.guild.members.cache.forEach((member) => {
if (member.roles.cache.has(role1.id)) {
member.roles.add(role2)
}
})

Adding a user to a role by name using arguments Discord.JS

so I am currently trying to find out if there is a way I could give a user a role by typing the a variable name. Like, example, '!role testrole', and then the bot will give them what the testrole is defined at. Like, 'if(args[1] === "testrole")...'. Testrole can just be defined as that, but the actual role name can just be like test. You can add multiple roles, and it will add it. '!role hello', etc...? Discord.JS
You can try something simple using a function to find the role name. You can do something like:
const roleName = message.guild.roles.cache.find(r => (r.name === args[1].toString()) || (r.id === args[1].toString().replace(/[^\w\s]/gi, '')));
This will either check for a mentioned role or a role name given

Alternative Ways to Define Users in Discord.JS

So to define users for things like displaying avatars, etc. i've been using this;
var user = message.mentions.users.first() || message.author;
But i've been trying to figure out how people have been able to define users without mentions. Example - my command requires me to tag someone whereas Dyno can do it with partial names. Any tips would be great, thanks!
An easy way to do so would probably be using the .find() function, where you can search for a certain object based on a method.
For example, if we were to have an args variable in our callback (Very easy to do so using a proper command handler - I'd suggest looking for tutorials if you aren't familiar with command handlers), and we were to ask a user to pass in a member's name, we could very easily get the user object using:
const user = message.guild.users.cache.find(user => user.username === args[0]);
// Keep in mind, 'user' is just a variable I've defined. It could also be 'monke => monke.username' if you wish.
Or, if we were to ask for their ID, we could use the .get() function to get the user object by ID:
const user = message.guild.users.cache.get(args[0]);
Do keep in mind it's not the greatest to have these kinds of object getting functions, as there are always multiple conflicts that could occur whilst getting the object, such as if there are multiple users with the same name/tag. I'd highly recommend sticking to your mention-based user objects, as it's the most accurate and non-conflicting method.
Every guild has a list of members which you can search through by enabling the Server Members Intent for your bot via the Discord Developer Portal. Once you have done that, you can fetch a collection of all members of a guild by doing
guild.members.cache;
You can then search this collection to find a member based on a search query using .includes(), .filter() or something similar. For example:
let query = "something";
let list = guild.members.cache.filter(member => member.user.username.includes(query));
console.log(Object.entries(list));
// expected output: list of all server members who's usernames contain "something"
You could also use the .find() method (since a collection is a map) to return a member with an exact username:
let member = guild.members.cache.find(member => member.user.username === query);
console.log(member.tag);
// expected output: tag of the user who's username is "something"
This is as simple as that:
var user = message.guild.members.cache.find(u => u.user.username.toUpperCase() === args.join(" ") || u.user.username.toLowerCase() === args.join(" ") || u.user.username === args.join(" "))
This will find the user on the current guild but you can also search your bot for this user by doing:
var user = client.users.cache.find(u => u.username.toUpperCase() === args.join(" ") || u.username.toLowerCase() === args.join(" ") || u.username === args.join(" "))
I have to assume that you already defined args and client. The example above will find users just by typing their name. toUpperCase means if you type the username in uppercase letters it will find the users anyways. toLowerCase means if you type the username in lowercase letters it will find the user as well. And you could also just type the username as it is. || means or so you can decide how you write the username, it will be found anyways.

How can I fix role.setPermissions function error?

I want to change the permissions of a role, but I keep getting an error saying TypeError: Cannot read property 'setPermissions' of undefined. As far as I can tell, all of my syntax and logic are fine:
let role = message.guild.roles.cache.find(role => role.name === "name");
role.setPermissions(["SEND_MESSAGES"])
Any suggestions?
Check & ensure that role is not undefined.
If role were a proper role object, this should go all as initially planned given you're using the appropriate version of discord.js.
Regarding reasons why role would be undefined, it seems there was a failure in finding a role that has a name property equal to "name".

Collection#find: pass a function instead

I'm fairly new to node.js and I'm working on a discord bot with discord.js, I'm am trying to do assigned roles with commands. When I do the code and type in the command it works successfully but pops up with "DeprecationWarning: Collection#find: pass a function instead" in the console, how can I get rid of this?
https://i.imgur.com/agKFNsF.png
This warning is caused by the following line:
var role = message.guild.roles.find('name', 'Epic Gamer');
On an earlier version of Discord.js, this would be valid, but they have now redone the find function. Instead of taking in a property and a value, you pass in a filtering function instead. This should work:
var role = message.guild.roles.find(role => role.name === "Epic Gamer")
Instead of passing in 'name' (the property), and 'Epic Gamer' (the value we want to search for/filter out), we pass in the arrow function role => role.name === 'Epic Gamer'. This is like mapping. find passes every role from message.guild.roles into the function as role, and then checks if the property we want equals the value we want.
If you would like to learn more about the find function, please check out the official documentation.
Pass a predicate function in find method, take a look at the discord.js document on the find function.
Change the find statement to
var role = message.guild.roles.find(role => role.name === 'Epic Gamer');
Hope this will help!

Resources