NodeJS Discord.js(v.13)错误:无法发送空消息

u3r8eeie  于 2022-11-04  发布在  Node.js
关注(0)|答案(1)|浏览(221)

几个月前,我也遇到了这个代码的问题,但是我在这里找到了一些解决方案后修复了这个问题。但是,从昨天开始,机器人又出现了和以前一样的问题,但是我不明白为什么,我甚至没有在代码中修改什么,但是它一直有同样的错误。
这是一个机器人程序,当你询问时,它会显示指定位置的天气。它对某些位置有效,但在其他位置,它会出现以下错误:

/home/container/node_modules/discord.js/src/rest/RequestHandler.js:350
      throw new DiscordAPIError(data, res.status, request);
            ^
DiscordAPIError: Cannot send an empty message
    at RequestHandler.execute (/home/container/node_modules/discord.js/src/rest/RequestHandler.js:350:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (/home/container/node_modules/discord.js/src/rest/RequestHandler.js:51:14)
    at async TextChannel.send (/home/container/node_modules/discord.js/src/structures/interfaces/TextBasedChannel.js:175:15) {
  method: 'post',
  path: '/channels/959726696393748519/messages',
  code: 50006,
  httpStatus: 400,
  requestData: {
    json: {
      content: undefined,
      tts: false,
      nonce: undefined,
      embeds: undefined,
      components: undefined,
      username: undefined,
      avatar_url: undefined,
      allowed_mentions: undefined,
      flags: undefined,
      message_reference: undefined,
      attachments: undefined,
      sticker_ids: undefined
    },
    files: []
  }
}

我不知道这样说是否有用,但在我运行命令和显示错误消息之间有一段时间。
下面是完整的代码:
index.js

const Discord = require("discord.js");
var weather = require('weather-js');
const { Client, GuildMember, Message } = require("discord.js");
const config = require("./config.json")
const client = new Client({
    intents: ["GUILD_MESSAGES", "GUILD_MEMBERS", "GUILDS"]
});
const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
  const command = require(`./commands/${file}`);
  client.commands.set(command.name, command);
}

const prefix = "!";

client.on("ready", () => {
...

client.on('messageCreate', async message => {
    if (!message.content.startsWith(prefix)) return;

    let args = message.content.slice(prefix.length).split(" ");
    let command = args.shift().toLowerCase()
    if (command === "weather") {
        client.commands.get("weather").execute(message, args, Discord, weather)
      }
    });

client.login(config.token);

weather.js

var weather = require('weather-js');

const Discord = require('discord.js');

module.exports = {
    name: 'weather',
    description: "Used to get the weather of a place",
    execute(message, args, Discord, weather) {

      const city = args[0]

      weather.find({search: args.join(" "), degreeType: "C"}, function(error, result){

        if (error) return message.channel.send(error)
        if (!city) return message.channel.send("You must specify a location please!")

        if (result === undefined || result.length === 0) return message.channel.send("**Invalid location**")

        let current = result[0].current
        let location = result[0].location

        const embed = new Discord.MessageEmbed()
.setTitle(`Weather forecast for ${current.observationpoint || "NO DATA"}`)
.setDescription(`${current.skytext || "NO DATA"}`) //Using a direct call can cause an error.
.setThumbnail(current.imageUrl) //If this part is the culprit let me know
.setColor("RANDOM")
.setTimestamp()
.addField("Temperature: ", `${current.temperature || "NO DATA"} °C`, true)
.addField("Wind Speed: ", `${current.winddisplay || "NO DATA"}`, true)
.addField("Humidity: ", `${current.humidity  || "NO DATA"}%`, true)
.addField("Timezone: ", `UTC${location.timezone  || "NO DATA"}`, true)

message.channel.send({ embeds: [embed]});

      })

    }
  }

我在一些网站上搜索如何解决“无法发送空消息”的错误,但没有任何效果。我还尝试使用weather-js输入另一个代码,但也没有效果。

1hdlvixo

1hdlvixo1#

这是因为if (error) return message.channel.send(error)中的error是一个对象,您只能发送一个字符串作为消息内容。
将其更改为error.message(这是一个字符串),它应该可以正常工作:

if (error) return message.channel.send(error.message)

相关问题