Saving roles from mentioned user - discord.js

I am trying to make a tempmute command, I followed a tutorial online which worked... But my own server has users with multiple roles, and these roles allow them to talk even when they receive the "muted" role.
Is there any way to save all the roles from a mentioned user and then to remove and add those roles?
I already tried to make a new let variable
let roleHistory = tomute.member.roles;
and then adding and removing them with:
await(tomute.removerole(roleHistory));
tomute.addRole(roleHistory);
But that didn't work
module.exports.run = async (bot, message, args) => {
let tomute = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
if(!tomute) return message.reply("Couldn't find user.");
if(tomute.hasPermission("MANAGE_MESSAGES")) return message.reply("Can't mute them!");
let muterole = message.guild.roles.find(`name`, "muted");
if(!muterole){
try{
muterole = await message.guild.createRole({
name: "muted",
color: "#000000",
permissions:[]
})
message.guild.channels.forEach(async (channel, id) => {
await channel.overwritePermissions(muterole, {
SEND_MESSAGES: false,
ADD_REACTIONS: false
});
});
}catch(e){
console.log(e.stack);
}
}
let mutetime = args[1];
if(!mutetime) return message.reply("You didn't specify a time!");
await(tomute.addRole(muterole.id));
message.reply(`<#${tomute.id}> has been muted for ${ms(ms(mutetime))}`);
setTimeout(function(){
tomute.removeRole(muterole.id);
message.channel.send(`<#${tomute.id}> has been unmuted!`);
}, ms(mutetime));
}
I want the bot to take the roles away, tempmute the user and giving the roles back after the Timeout.

Your attempt is on the right track, but you missed a small detail. A Guild Member has a method addRole and removeRole which you used. However, these methods are meant for adding/removing a single role.
When you first fetch the user roles with let roleHistory = tomute.member.roles;, it returns a Collection of roles. If you then attempt to use removeRole(roleHistory) it attempts to remove a single role equal to the complete collection (which doesn't exist obviously).
To make it work, you need the methods addRoles and removeRoles which adds/removes an entire collection. So your code would be:
let roleHistory = tomute.roles;
// Removing all the roles
await(tomute.removeRoles(roleHistory));
// Adding all the roles
tomute.addRoles(roleHistory);
P.s. Since your tomute variable is already a user you need to change your code to fetch the roles from let roleHistory = tomute.member.roles; to let roleHistory = tomute.roles;

Related

Discord JS - If the user reacting to message has a specific role I want to add a role

I'm attempting to create a more complex reaction role function.
Basically I want to achieve that if a user has 2 roles, it should have access to some channels.
So the way I'm trying to do this is by checking if the user has one of the roles when clicking on a reaction that grants the member the second role. If the user has the other role, he should get a third role that unlocks these channels.
But, I haven't been able to figure out how to look at the roles of the person reacting to the message.
client.on('messageReactionAdd', async (reaction, user) => {
const message = await reaction.message.fetch(true);
const channelAccess = '918617193988649002';
const foodSweden = message.guild.roles.cache.find(role => role.name === 'πŸ²πŸ‡ΈπŸ‡ͺ');
const foodCategoryReaction = '🍲';
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.channel.id === channelAccess) {
if (reaction.emoji.name === foodCategoryReaction && message.member.roles.has('πŸ‡ΈπŸ‡ͺ')) {
await reaction.message.guild.members.cache.get(user.id).roles.add(foodSweden);
} else {
return;
}
}
});
So this is the code. I'm uncertain how message.member.roles.has('πŸ‡ΈπŸ‡ͺ') should be coded.
I've tried reaction.message.member.roles.has('πŸ‡ΈπŸ‡ͺ'), reaction.member.roles.has('πŸ‡ΈπŸ‡ͺ'), user.roles.has('πŸ‡ΈπŸ‡ͺ') and shifted out member for user.
I'm uncertain how message.member.roles.has('πŸ‡ΈπŸ‡ͺ') should be coded. I've tried reaction.message.member.roles.has('πŸ‡ΈπŸ‡ͺ'), reaction.member.roles.has('πŸ‡ΈπŸ‡ͺ'), user.roles.has('πŸ‡ΈπŸ‡ͺ') and shifted out member for user.
None of those are existing methods. member.roles does not have a .has() method. And users represent general discord users outside of guilds; users do not have roles, only guild members have roles. The .has() method of any manager in discord.js also usually utilizes IDs (which is what they are mapped to in the underlying Collection cache), not names or other properties. See the docs.
Here is how you check if the member who reacted has a specific role:
const hasRole = reaction.message.member.roles.cache.some(role => role.name == "πŸ‡ΈπŸ‡ͺ");
The role cache for guild members is almost always up-to-date, so you don't really need to do any fetching. You can rely on the role cache here.

Command is giving the bot role

I'm trying to make a command that gives a user a role. It was working before, but not it just gives the bot a role.
module.exports = {
name: "addevent",
description: "Add the event ping role to a user",
async run(client, message, args) {
const { member } = message;
if (message.member.roles.cache.has('793222158981529621')) {
const { member } = message;
message.channel.send(`${member} You already have this role.`)
}
if (message.channel.id !== '793224633759432755') return;
member.roles.add('793222158981529621')
message.channel.send(`${member} Added the event ping role.`)
}
}
This is also happening to my level command that's supposed to check a user's level. It checks the bot's level, not the user's.
let role = message.guild.roles.cache.find(r => r.name === "Role Name Here");
if (message.channel.id !== 'Channel ID Here') return;
if (message.member.roles.cache.has(role.id)) {
return message.channel.send(`${message.member} You already have this role.`)
}
message.member.roles.add(role);
message.channel.send(`${message.member} Added the event ping role.`);
My example has a variable called role which finds the role by its name, so you have to provide the name of the role there. Your if-statement with the message.channel.id was understandable and is a nice way to get the role in only one channel. The second if-statement just asks if a user already has that role (dont forget: in .has() you have to provide an ID, in this case it is easily done with role.id). Anyways if the user has that role it will send a message and return. Otherwise it will add the role to the user and also send a message.

Custom Roles on join with enmap

I’ve tried to add code to allow people to customise which role people get when they join a server which can be set on a per server basis however I cannot seem set get it working. The greeting channel, the greeting and the DM are all working. It is only the role adding. If you could point me in the right direction then that would be very helpful.
client.settings = new Enmap({
name: "settings",
fetchAll: false,
autoFetch: true,
cloneLevel: 'deep'
});
// Just setting up a default configuration object here, to have somethign to insert.
const defaultSettings = {
prefix: "!",
modLogChannel: "mod-log",
modRole: "Moderator",
adminRole: "Administrator",
welcomeChannel: "chat",
welcomeMessage: "Welcome to the server, {{user}} πŸ˜‰",
welcomeDMMessage: "text",
rolesOnJoin: "Basic"
}
client.on("guildDelete", guild => {
// When the bot leaves or is kicked, delete settings to prevent stale entries.
client.settings.delete(guild.id);
});
client.on("guildMemberAdd", member => {
client.settings.ensure(member.guild.id, defaultSettings);
let roleAdd = client.settings.get(member.guild.id, "rolesOnJoin");
let welcomeMessage = client.settings.get(member.guild.id, "welcomeMessage");
let welcomeDMMessage = client.settings.get(member.guild.id, "welcomeDMMessage");
let role = member.guild.roles.find(role => role.name == roleAdd);
welcomeMessage = welcomeMessage.replace("{{user}}", member.user.tag)
member.guild.channels
.find("name", client.settings.get(member.guild.id, "welcomeChannel"))
.send(welcomeMessage, {files: ["https://cdn.glitch.com/ecc1aef4-3247-42a1-9361-cfc56e9c5ba1%2F75AC6C9B-3E71-4F25-B8CF-47050B4B8F21.jpeg"]})
.catch(console.error);
member.send(welcomeDMMessage);
member.addRole(role);
});
addRole() needs the role ID (or the Role as an object), you can still find it via it's name with
let role = guild.roles.find(role => role.name == roleAdd)
member.addRole(role);
Presumably the problem is that addRole() expects either a Role object or a role ID:
https://discord.js.org/#/docs/main/stable/class/GuildMember?scrollTo=addRole
https://discord.js.org/#/docs/main/stable/typedef/RoleResolvable
"Basic" is neither.
If all you have is the role name, you need to find it by that name in the guild.roles Collection.

identifying admins from mentions discordjs

I want to find out if there are mentions in a message which I am doing using if(message.mentions.users.first()) but now among all the mentions I also want to filter out mentions of the admin team and not those of the community members. How to achieve that?
I tried filtering users based on roles like this
let r = [];
message.mentions.users.forEach( user => {
console.log('user' + user)
user.roles.forEach(role => {
console.log('role' + role)
r.push(role);
})
console.log('roles' + r);
var member = false;
for (i in r) {
if (i == 'admin') {
member = true;
}
}
})
This doesn't seem to work.
const adminName = ['<role name>'];
const adminId = ['<role ID>'];
client.on('message', (msg) => {
let admins = msg.mentions.members.filter( (user) => {
return adminId.some(id => user.roles.has(id)) || user.roles.some(r => adminName.includes(r.name));
});
console.log(admins.map(d => d.user.username));
});
There, I created 2 arrays, one with the role ID and another with the role name. You can use both, or only one (I prefer to use the id, because you can change a role name, but you can't change an id, except if you delete the role).
Then, I iterate through the members in the mentions of the messages. Why the members and not the users? Because the GuildMember is a type linked to a guild, which is an instance of an user in a guild, and so it has the role of the user.
Then I check if one of the id of my admins roles is in the roles of the user or if one of the roles name is inside my adminName.
The variable admins is a collection of GuildMember.
You can change the function inside the some to match whatever condition make someone an admin for you.

Add a role to people that react with a certain emoji in a certain channel

I'm trying to make a discord.js bot for my server I made for people in my school. I'm trying to make a #classes channel and if you react to certain messages it gives you a role (which gives you access to the text channel for that class).
client.on('messageReactionAdd', (reaction, user) => {
console.log("client.on completed.")
if (message.channel.name === 'classes') {
console.log("if(message) completed.")
if (reaction.emoji.name === "reminder_ribbon") {
console.log("emoji test completed.")
const guildMember = reaction.message.guild.members.get(user.id);
const role = reaction.message.guild.roles.find(role => role.name === "FACS");
guildMember.addRole(role);
}
}
});
This is what I have tried so far, however, it does not give me/the other people reacted to it the role nor does it return an error message.
P.S. Also, how would I be able to make it so when they unreact it removes the role?
Edit: It seems it only gets reactions from cached messages/messages sent after bot startup. Also, message is not defined on the first if(message.channel.id) message.
Try to use the following code:
client.on('messageReactionAdd', (reaction, user) => {
if (reaction.message.channel.id === '552708152232116224') {
if (reaction.emoji.name === "reminder_ribbon") {
const guildMember = reaction.message.guild.members.get(user.id);
const role = reaction.message.guild.roles.get('552709290427940875');
guildMember.addRole(role);
}
}
});
First of all, reaction.users is an Object with all the users that reacted on the message, so you first have to define to which user you want to assign the role. I fixed this fetching the guildMember with user.id.
The second mistake was that you tried to assign a role ID to a guildMember although you first have to fetch the role and then assign the role Object to the guildMember.

Resources