So hey Stackoverflow community, Im here kinda confused on why this stopped working all of the sudden, and I get the error that is stated in the title.
Basically it would check to see if the user who was attempting to mute a user had a higher role than the other user, and if they did.. it would mute them, and if they didn't it would throw an error.
But now all it does is just throw the error stated in the title once again and I cannot find a fix on the docs?
Also when i remove the highest.position
from the check, it allows anyone with the right perms to mute anyone above or below them.
So here I am, asking nicely for some help/understanding on why this method of muting users has stopped working unexpectedly.
const { MessageEmbed } = require("discord.js");
const db = require("quick.db");
const ms = require("ms");
const modSchema = require("../../models/modLogs");
module.exports = {
name: "mute",
description: "Mutes a user in a server",
usage: "[name | nickname | mention | ID] (reason)",
run: async (client, message, args) => {
try {
if (!message.member.permissions.has('MANAGE_ROLES')) return message.reply('You do not have perms to mute users. - `[MANAGE_ROLES]`')
if (!message.guild.me.permissions.has('MANAGE_ROLES')) return message.reply('I do not have perms to mute users. - `[MANAGE_ROLES]`');
if (!args[0]) return message.reply("```Usage: c!mute <user>```");
var mutedMember = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.guild.members.cache.find(r => r.user.username.toLowerCase() === args[0].toLocaleLowerCase()) || message.guild.members.cache.find(ro => ro.displayName.toLowerCase() === args[0].toLocaleLowerCase());
if (!mutedMember) return message.reply("Please provide a valid user to mute.");
if (mutedMember === message.member) return message.reply("I'm afraid you cannot mute yourself, Captain.");
if (message.author.roles.highest.position <= mutedMember.roles.highest.position) return message.reply("<a:CL_No:909440866622517318> You cannot mute that user because they have the same or higher role than you.");
let reason = args.slice(1).join(" ");
if (mutedMember.user.bot) return message.reply("<a:CL_No:909440866622517318> I am unable to mute other bots.");
const userRoles = mutedMember.roles.cache.filter(role => role.id !== message.guild.id).map(role => role.id);
let muterole;
let dbmute = await db.fetch(`muterole_${message.guild.id}`);
let muteerole = message.guild.roles.cache.find(role => role.name === "Muted");
if (!message.guild.roles.cache.has(dbmute)) {
muterole = muteerole;
} else {
muterole = message.guild.roles.cache.get(dbmute);
}
if (!muterole) {
try {
message.reply("<a:CL_No:909440866622517318> I was unable to find the `Muted` role, attempting to create one now...");
muterole = await message.guild.roles.create({
name: "Muted",
color: "#010101",
permissions: []
});
message.guild.channels.cache.forEach(async channel => {
await channel.permissionOverwrites.create(muterole, {
SEND_MESSAGES: false,
ADD_REACTIONS: false,
SPEAK: false,
CONNECT: false
});
});
message.reply("<a:CL_CheckMark:858853559940808724> Successfully created the \`Muted\` role.");
} catch (err) {
message.reply(`\`${err}\``)
}
}
if (mutedMember.roles.cache.has(muterole.id)) return message.reply(`\`${mutedMember.user.tag}\` is already muted.`);
db.set(`muteeid_${message.guild.id}_${mutedMember.id}`, userRoles);
try {
mutedMember.roles.set([muterole.id]).then(() => {
const muteEmbed1 = new MessageEmbed()
.setColor("RED")
.setAuthor({ name: message.guild.name, iconURL: message.guild.iconURL() })
.setDescription(`${mutedMember.user.tag}, You were muted in \`${message.guild.name}\``)
.addField("Reason:", `${reason || "No Reason Specified."}`)
.setTimestamp()
.setFooter({ text: `Moderator: ${message.author.tag}` });
mutedMember.send({ embeds: [muteEmbed1] });
});
} catch {
mutedMember.roles.set([muterole.id]);
}
if (reason) {
const muteEmbed2 = new MessageEmbed()
.setColor("RED")
.setAuthor({ name: message.guild.name, iconURL: message.guild.iconURL() })
.setDescription(`\`${mutedMember.user.tag}\` has been muted.`)
.addField("User Roles:", "<a:CL_CheckMark:858853559940808724> | Removed all user roles.")
.setTimestamp();
message.reply({ embeds: [muteEmbed2] });
} else {
const muteEmbed3 = new MessageEmbed()
.setColor("RED")
.setAuthor({ name: message.guild.name, iconURL: message.guild.iconURL() })
.setDescription(`\`${mutedMember.user.tag}\` has been muted.`)
.addField("User Roles:", "<a:CL_CheckMark:858853559940808724> | Removed all user roles.")
.setTimestamp();
message.reply({ embeds: [muteEmbed3] });
}
modSchema.findOne({ Guild: message.guild.id }, async (err, data) => {
if (!data) return;
const logschannel = message.guild.channels.cache.get(data.Channel);
if (logschannel) {
const muteMemberEmbed = new MessageEmbed()
.setTitle('User Muted')
.addField('User', `${mutedMember.user.username} (${mutedMember.user.id})`)
.addField('Moderator', `${message.author.tag}`)
.addField('Reason', `${reason || 'No Reason Specified'}`)
.addField('Muted', `<t:${Math.floor(Date.now() / 1000)}:R>`)
.setColor('ORANGE')
.setThumbnail(mutedMember.user.displayAvatarURL({ dynamic: true }))
logschannel.send({ embeds: [muteMemberEmbed] });
}
});
} catch (err) {
return message.reply(`\`\`\`${err}\`\`\``);
}
}
};
CodePudding user response:
The problem is that message.author
returns a User
and User
s don't have roles; only GuildMember
s have. While a User
is a global user on Discord, a GuildMember
is a user on a specific server and this member has roles on that server.
So, instead of message.author
you should use message.member
that returns a GuildMember
:
if (message.member.roles.highest.position <= mutedMember.roles.highest.position)
return message.reply(
"<a:CL_No:909440866622517318> You cannot mute that user because they have the same or higher role than you."
);