Home > Back-end >  Unban command, .catch not working, UnhandledPromiseRejectionWarning
Unban command, .catch not working, UnhandledPromiseRejectionWarning

Time:12-07

I'm making a unban command, it was all going well till I tried to make a unban command, I can't use .catch to check if the user is banned or not, so is there a better way or a fix?

I'm planning on changing it to embeds to add more style to it, if that may help you some how :D

Here is the ban / unban command:

const { MessageEmbed } = require('discord.js');

//ban
module.exports = {
    name: 'ban',
    category: 'Owner',
    description: 'Bans a member.',
    aliases: [],
    usage: 'ban <user>',
    userperms: [],
    botperms: [],
    run: async (client, message, args) => {
    if (!message.guild.member(message.author).hasPermission("BAN_MEMBERS")) {
            return message.channel.send('You do not have the permission to ban users!');
        }
    if (!message.guild.member(client.user).hasPermission("BAN_MEMBERS")) {
            return message.channel.send("I don't have the permission to ban users!");
    }
    if (message.mentions.users.size === 0) {
        return message.channel.send("Can't find this member.");
    }
    if (message.mentions.members.first().id == message.author.id) {
        return message.channel.send("Silly you, why are you trying to ban yourself?")
    }
    var member = message.mention.members.first();
    member
        .ban()
        .then(member => {
            guild.members.ban(id);
            message.channel.send("*it's not a bird,*, *it's just* **"   member.displayName   "** *getting banned! :hammer:*");
        })
        .catch(() => {
            message.channel.send("You can't ban this member");
        });
    },
};

//unban
module.exports = {
    name: 'unban',
    category: 'Owner',
    description: 'Unbans a banned member.',
    aliases: [],
    usage: 'unban <user>',
    userperms: [],
    botperms: [],
    run: async (client, message, args) => {
    if (!message.guild.member(message.author).hasPermission("BAN_MEMBERS")) {
            return message.channel.send('You do not have the permission to unban users!');
        }
    if (!message.guild.member(client.user).hasPermission("BAN_MEMBERS")) {
            return message.channel.send("I don't have the permission to unban users!");
    }
    if (message.mentions.users.size === 0) {
        return message.channel.send("Can't find this member.");
    }
    if (message.mentions.members.first().id == message.author.id) {
        return message.channel.send("Lol, your not banned....")
    }
    var member = message.mentions.members.first();
        (member => {
            message.guild.members.unban(id)
            message.channel.send("**"   member.displayName   "** **unbanned! :hammer:**");
        })
        .catch(() => {
            message.channel.send("User not banned.");
    });
    },
};

Error:

(node:1551) UnhandledPromiseRejectionWarning: TypeError: message.mentions.members.first.catch is not a function
    at Object.run (/home/runner/DwaCraft-Ticket-bot/commands/owner/ban-unban.js:65:45)
    at module.exports (/home/runner/DwaCraft-Ticket-bot/events/guild/message.js:55:11)
    at Client.emit (events.js:314:20)
    at MessageCreateAction.handle (/home/runner/DwaCraft-Ticket-bot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (/home/runner/DwaCraft-Ticket-bot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/home/runner/DwaCraft-Ticket-bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/home/runner/DwaCraft-Ticket-bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/home/runner/DwaCraft-Ticket-bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/home/runner/DwaCraft-Ticket-bot/node_modules/ws/lib/event-target.js:132:16)
    at WebSocket.emit (events.js:314:20)
(node:1551) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1551) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

CodePudding user response:

You might need to chain the catch block with the unban function.

message.guild.members.unban(id)
  .then(() => {
    message.channel.send("**"   member.displayName   "** **unbanned! :hammer:**" )
   })
  .catch(() => {
    message.channel.send("User not banned."); 
  })  

You can get the member's Id from the member variable through member.id

  • Related