简体   繁体   English

Discord.js 的代码响应抛出错误

[英]Code for Discord.js responding with a throw error

I created a simple logging bot that will log a server to a specific logging server, soon I'll make it a simple and easy to use bot for everyone to use, but I came across a problem.我创建了一个简单的日志记录机器人,它将服务器记录到特定的日志记录服务器,很快我将使它成为一个简单易用的机器人,供所有人使用,但我遇到了一个问题。

the code:编码:

const Discord = require('discord.js');
const client = new Discord.Client();
const KeepAlive = require('./server');

client.on('ready', async ()=> {
    console.log('Connected as '+client.user.tag)
});

client.on('message', function (msg) {
    const Logged = new Discord.MessageEmbed()
        .setTitle(msg.author.tag)
        .addField('userID -', msg.author.id)
        .addField('Channel -', msg.channel)
        .addField('Message -', msg.content)
        .setColor('0x00AAFF')
        .setTimestamp();

    if (msg.author.tag === 'CLIENT ID GOES HERE') {
        console.log('')
    } else if (msg.author.username) {
        const channel = client.channels.cache.find(channel => channel.id === 'CHANNEL ID GOES HERE')
        channel.send(Logged)
    }
});

client.on('messageDelete', msg => {
    const DLogged = new Discord.MessageEmbed()
        .setTitle(msg.author)
        .addField('Channel -', msg.channel)
        .addField('Message -', msg.content)
        .setColor('#ff221a')
        .setTimestamp();

    if (msg.author.id === 'CLIENT ID GOES HERE') {
        console.log('')
    } else if (msg.author.username) {
        const channel = client.channels.cache.find(channel => channel.id === 'CHANNEL ID GOES HERE')
        channel.send(DLogged)
    }
});

KeepAlive()
client.login(process.env.token);

the error:错误:

sageEmbed.js:432
    if (!value) throw new RangeError('EMBED_FIELD_VALUE');
                ^

RangeError [EMBED_FIELD_VALUE]: MessageEmbed field values may not be empty.

but the error for me wasn't as simple as I thought, it says something about an embed so I tried to simplify it and it never worked as in a simple description with the message.但对我来说,错误并不像我想象的那么简单,它说明了嵌入的一些内容,所以我试图简化它,但它从来没有像消息的简单描述那样起作用。 That never worked for me though.但这对我从来没有用过。 I had no choice but to post on stackoverflow so hopefully the community can help me out, it'll be much appreciated!我别无选择,只能在 stackoverflow 上发帖,希望社区可以帮助我,不胜感激!

This appears to be caused by messages with no text content (embed/image upload, etc)这似乎是由没有文本内容的消息(嵌入/图像上传等)引起的

When a message is sent with no text content, msg.content will be set to "" .当发送没有文本内容的消息时, msg.content将设置为""

As a result, the field's value is then an empty string and throws the error.结果,该字段的值是一个空字符串并引发错误。

To fix this, add a check for the content:要解决此问题,请添加内容检查:

// for example
client.on('message', function (msg) {
    const Logged = new Discord.MessageEmbed()
        .setTitle(msg.author.tag)
        .addField('userID -', msg.author.id)
        .addField('Channel -', msg.channel)
        // check for empty content
        .addField('Message -', msg.content === "" ? "no content" : msg.content)
        .setColor('0x00AAFF')
        .setTimestamp();

    if (msg.author.tag === 'CLIENT ID GOES HERE') {
        console.log('')
    } else if (msg.author.username) {
        const channel = client.channels.cache.find(channel => channel.id === 'CHANNEL ID GOES HERE')
        channel.send(Logged)
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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