Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

259
Views
how to handle a DiscordAPIError: Missing Permissions in discord.js when removing roles

I have a bot that removes all roles from a user, however, when the bots role is below the role it is trying to remove. It obviously doesn't have permissions so it throws the error DiscordAPIError: Missing Permissions, crashing my bot. How can i handle this to message.reply(member + 'roles are too high for me to remove') When this happens as i cant think of a way to code this in. Heres the function that removes the roles at the moment:

if (message.member.permissions.has('ADMINISTRATOR')) {
            if (!args[0]) {message.reply('you need to ping someone to smac after =smacmedown')
            } else {
            let member = message.mentions.members.first();
            member.roles.remove(member.roles.cache);
            }
        } else { message.reply('only admins can use the smacmedown command!:sob::sob:') }

Thanks for any help :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

.remove() returns a promise (Promise<GuildMember>). It will resolve with the member if the action was successful and reject with the error object on any kind of error. Handle the promise.

By simply adding a .catch() callback, the promise is handled and therefor does not throw an Unhandled promise rejection error

ALSO: The best way to remove all roles of a member is to do member.roles.set([]);

member.roles.set([])
   .then(member => /* On Success */)
   .catch(err => /* On Error */);

If you want to use Async/Await

try {
   await member.roles.set([]);

   /* ... On Success */
} catch (err) {
   /* On Error */
}

Here's the Documentation for GuildMemberRoleManager#remove() and GuildMemberRoleManager#set()

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!