Discord.js v12 Ban 命令 - UnhandledPromiseRejectionWarning: RangeError [EMBED_FIELD_VALUE]: MessageEmbed 字段值不能为空

问题描述

我发出的禁止命令有一些问题,我不太确定是什么原因。这里的一切看起来都有效,但每次我尝试运行 ban 命令时,控制台中都会出现很多错误。当您运行该命令时,它应该禁止用户,向日志通道发送消息,并使用 quick.db 存储惩罚信息。有人看到我做错了什么吗?

代码

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,time: Date.Now(),ID: banID,type: "BAN"
        });

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

错误

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

解决方法

除了您当前的错误之外,您的禁止功能将不起作用:

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

您必须将原因作为对象输入,因为您也可以输入天数,如文档中所示here

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