简体   繁体   中英

Discord.js v12 Ban Command - UnhandledPromiseRejectionWarning: RangeError [EMBED_FIELD_VALUE]: MessageEmbed field values may not be empty

I'm having some issues with a ban command I made, and I'm not quite sure why. Everything here looks like it works but I'm getting a lot of errors in the console every time I try to run the ban command. When you run the command it should both ban the user, send a message to the logs channel, and store the punishment info using quick.db . Does anybody see something I'm doing wrong?

The code:

const { MessageEmbed } = require("discord.js");
const convertToMS = require('../lib/convertTime');
const embedStruc = require('../lib/embedStructure');
const e = require('../embeds.json');
const consola = require("consola");
const db = require("quick.db");

const validateUser = (guild, id) => {
    let userID = id;

    if (userID.indexOf('<@!') > -1) {
        userID = userID.split('<@!')[1];
        userID = userID.slice(0, -1)
    } else if (userID.indexOf('<@') > -1) {
        userID = userID.split('<@')[1];
        userID = userID.slice(0, -1)
    }

    const actualMember = guild.members.cache.get(userID);

    if (!actualMember) return false;
    return actualMember.id;
}

module.exports = {
    name: 'ban',
    description: 'Bans a user',
    alias: [],
    async execute(message, args, client, prefix, logsChannel) {
        if(!message.member.hasPermission("ADMINISTRATOR")) return;
        if (args.length === 0) {
            return message.channel.send(
                new MessageEmbed()
                    .setColor(e.red)
                    .setDescription(
                        `:x: **Invalid Arguments**\n` +
                        `\`${prefix}ban [@mention/user id] (reason)\``
                    )
            ).catch();
        }

        let punishedUser = validateUser(message.guild, args[0]);
        if (!punishedUser) {
            return message.channel.send(
                new MessageEmbed()
                    .setColor(e.red)
                    .setDescription(
                        `:x: **Invalid User**`
                    )
            ).catch();
        }

        let reason = 'No reason provided';
        if (args.length > 1) {
            let tempArgs = args.join(' ').split(' ');
            await tempArgs.shift();
            await tempArgs.shift();
            reason = tempArgs.join(' ');
        }

        const bannedUser = message.guild.members.cache.get(punishedUser);
        const banID = db.add(`${message.guild.id}.base`, 1).base;

        if (!bannedUser.bannable) {
            return message.channel.send(
                new MessageEmbed()
                    .setColor(e.red)
                    .setDescription(`:x: Unable to ban <@!${bannedUser.id}>.`)
            );
        }

        await bannedUser.send(
            embedStruc.userBan(
                reason,
                message.guild.name,
                banID
            )
        ).catch(() => {});

        await bannedUser.ban(`Banned for "${reason}" by ${message.author.tag}`)
            .catch(() => {
                return message.channel.send(
                    new MessageEmbed()
                        .setColor(e.red)
                        .setDescription(`:x: Unable to ban <@!${bannedUser.id}>.`)
                );
            });

        message.channel.send(
            new MessageEmbed()
                .setColor(e.default)
                .setDescription(
                    `:scream: <@!${bannedUser.id}> was banned.`
                )
        ).catch();

        logsChannel.send(
            embedStruc.logsBan(
                bannedUser.id,
                message.author.id,
                reason,
                Date.now(),
                banID
            )
        ).catch();

        db.set(`${message.guild.id}.punishments.${banID}`, {
            user: bannedUser.id,
            moderator: message.author.id,
            reason,
            time: Date.now(),
            ID: banID,
            type: "BAN"
        });

        db.push(`${message.guild.id}.${bannedUser.id}.punishments`, banID);
    },
};

Error:

(node:23906) UnhandledPromiseRejectionWarning: RangeError [EMBED_FIELD_VALUE]: MessageEmbed field values may not be empty.
    at Function.normalizeField (/Users/evandeede/Downloads/modbot/node_modules/discord.js/src/structures/MessageEmbed.js:432:23)
    at /Users/evandeede/Downloads/modbot/node_modules/discord.js/src/structures/MessageEmbed.js:452:14
    at Array.map (<anonymous>)
    at Function.normalizeFields (/Users/evandeede/Downloads/modbot/node_modules/discord.js/src/structures/MessageEmbed.js:451:8)
    at MessageEmbed.addFields (/Users/evandeede/Downloads/modbot/node_modules/discord.js/src/structures/MessageEmbed.js:266:42)
    at MessageEmbed.addField (/Users/evandeede/Downloads/modbot/node_modules/discord.js/src/structures/MessageEmbed.js:257:17)
    at Object.logsBan (/Users/evandeede/Downloads/modbot/lib/embedStructure.js:163:10)
    at Object.execute (/Users/evandeede/Downloads/modbot/commands/ban.js:98:24)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:23906) 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:23906) [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.

Apart from your current error, your ban function will not work:

await bannedUser.ban(`Banned for "${reason}" by ${message.author.tag}`)

You will have to put the reason in as an object, since your able to put in an amount of days too, as shown in the docs here

await bannedUser.ban({ reason: `Banned for "${reason}" by ${message.author.tag}` })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM