Discord.js v12 禁止命令 - UnhandledPromiseRejectionWarning: RangeError [EMBED_FIELD_VALUE]: MessageEmbed

Posted

技术标签:

【中文标题】Discord.js v12 禁止命令 - UnhandledPromiseRejectionWarning: RangeError [EMBED_FIELD_VALUE]: MessageEmbed 字段值可能不为空【英文标题】:Discord.js v12 Ban Command - UnhandledPromiseRejectionWarning: RangeError [EMBED_FIELD_VALUE]: MessageEmbed field values may not be empty 【发布时间】:2021-07-16 00:58:06 【问题描述】:

我的禁令命令有些问题,我不太清楚为什么。这里的一切看起来都有效,但是每次我尝试运行 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` +
                        `\`$prefixban [@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);
    ,
;

错误:

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

【问题讨论】:

能否请您包括您在问题中遇到的错误? 在没有错误的情况下调试任何错误非常困难 - 请考虑 对不起,我刚刚在错误信息中添加了 看起来您在嵌入中缺少values,因此它会有效地创建一个空字段,这是不可能的。调试代码并在嵌入中查找 undefined 变量 这似乎是您使用的一个值未定义的问题,或者是../lib/embedStructure 文件中的问题。检查您在第 93 行传递给函数的变量是否正确定义,如果它们定义正确,则给我们embedStructure 文件。 【参考方案1】:

除了你当前的错误,你的ban功能将不起作用:

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

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

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

【讨论】:

以上是关于Discord.js v12 禁止命令 - UnhandledPromiseRejectionWarning: RangeError [EMBED_FIELD_VALUE]: MessageEmbed的主要内容,如果未能解决你的问题,请参考以下文章

discord.js v12 解禁命令

Discord.js v12 清除命令

使用 discord.js v12 解禁命令

discord.js V12,我的命令[重复]

discord.js v12 用户信息命令

机器人不响应命令(discord.js v12)