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 🙂

Answer

.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()