【问题标题】:"Cannot send an empty message"“无法发送空消息”
【发布时间】:2020-12-21 16:10:22
【问题描述】:

我正在使用 Node.js 开发一个机器人,但我不知道为什么,我不断收到此错误。

(node:999) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message

我试图修复它,但我无法修复它

这是我的代码(furderen.txt 不为空)

const client = new Discord.Client();
const fs = require('fs')

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});
var data = fs.readFileSync("furderen.txt", "utf8");

//note this will be async
function getRandomLine(filename){
  fs.readFileSync("furderen.txt", "utf8")
  var lines = data.split('\n');
  /*do something with */ lines[Math.floor(Math.random()*lines.length)];
}

client.on('message', message => {
  if(message.content === '!hesap') {
    var hesap = getRandomLine("furderen.txt")
    client.users.cache.get(message.author.id).send(hesap);
  }
})

client.login(I wont put it here :p)

【问题讨论】:

  • hesap 为空。

标签: node.js discord discord.js bots


【解决方案1】:

您的代码散布着不必要和错误的部分。我总结了这一点,并用最少的代码复制了它。重点是getRandomLine 没有返回值。

const fs = require('fs')

function getRandomLine(filename){
  var data = fs.readFileSync(filename, "utf8");
  var lines = data.split('\n');
  /*do something with */
  return lines[Math.floor(Math.random()*lines.length)];
}

var hesap = getRandomLine("furderen.txt");
console.log(hesap);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-07
    • 2021-05-05
    • 2021-06-25
    • 2022-01-01
    • 2020-01-25
    • 2022-01-20
    • 2019-03-05
    相关资源
    最近更新 更多