role.setPosition() in Discord.js v12 - discord

In Discord.js v11 you could use guild.setRolePosition({ role: '123456789012345678', position: 1 }); to set the position of a specific role. How can you specify a role (like i.e. 'Muted') with the new role.setPosition() method? It seems to only accept a position number and a few options like options.relative. What I want is to assign a role position to the roles Admin, Friends, Muted in the roleCreate() event. I know that the roleCreate event only runs when a role is created, but somehow the 'position' parameter doesn't work well with guild.roles.create.

GuildRoleManager.create() actually works fine with the position parameter, you just have to add it ass a property to the data object.
guild.roles.create({
data: {
name: 'Role Name',
// any other options...
position: 1
},
});
If you still want to use role.setPosition(), you'll have to fetch the role object before-hand, then call the method on that object.
// <guild> is a placeholder for the guild object
// get the role by id
const role = <guild>.roles.cache.get('Role ID');
// get role by name (or other property)
const role = <guild>.roles.cache.find((role) => role.name === 'Role Name');
role.setPosition(1);

Related

Add role to user by ID

I try to add a role to a user by ID, but I always receive this error
user.roles.add(role)
^
TypeError: Cannot read properties of undefined (reading 'add')
Here is my code
let role = Guild.roles.cache.find((r) => r.name === dataobj.Guild.name);
client.users.fetch(useritem).then((user) => {
user.roles.add(role)
.catch(error => { })
});
dataobj.Guild.name is the name of the role.
useritem is the ID of the user.
var role is correct and output the right role.
Var user is correct and output the right user.
I tried many things, but nothing works.
Thank for your help.
You should fetch guild.members instead of client.users and to add role by its ID you should use role.id instead of role

How to Edit Embedded Message on Discord When Role Has Been Assigned or Removed

I am working on a Discord bot and I have an embed that shows the names of people who has that role, I want to make it edit that one message everytime the role is assigned or removed. Help would be very appreciated 🙂
You can use the Client#guildMemberUpdate. The event is fired whenever the GuildMember is updated. (This includes: role added, role removed, nickname changes etc.)
Here's a simple example:
client.on("guildMemberUpdate", (oldGuildMember, newGuildMember) => {
if (oldGuildMember.guild.id == "GuildID") { // Checking if the event was fired within the required Guild.
if (!oldGuildMember.roles.cache.equals(newGuildMember.roles.cache)) { // Checking if the roles were changed.
const Channel = client.channels.cache.get("ChannelIUD"); // Getting the channel your MessageEmbed is in.
const Role = oldGuildMember.guild.roles.cache.get("RoleID"); // Getting the Role by ID.
if (!Role || !Channel) return console.error("Invalid role or channel.");
Channel.messages.fetch("MessageID").then(message => { // Getting the MessageEmbed as a Message by ID
const Embed = new Discord.MessageEmbed(); // Updating the MessageEmbed.
Embed.addField(`Members of ${Role.name}`, Role.members.size > 0 ? `${Role.members.map(member => member.user.tag).join("; \n")};` : "This role has no members.");
Embed.setColor("RED");
message.edit(Embed).catch(error => console.error("Couldn't edit the message."));
}).catch(error => console.error("Couldn't fetch the 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.

Saving roles from mentioned user

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;

Resources