我目前正在与Node JS一起制作一个不和谐的聊天机器人,试图在其中建立四个连接。目前,我唯一要做的就是绘制初始游戏板。

我收到一个错误:


  “ UnhandledPromiseRejectionWarning:DiscordAPIError:无法发送空消息”错误。


这是我的代码,应该绘制一个矩形框(游戏板)。

const boardSquareDef = "| |"
const boardSquareRed = "|R|"
const boardSquareBlue = "|B|"

var defaultDrawnBoard;
var drawnBoardX;

var boardX = 7;
var boardY = 6;

var botChannel = client.channels.get("469825469697622029");

// This function is called elsewhere
function BoardDraw ()
{
  for (var i = 0; i >= boardX; i++) {
    drawnBoardX += boardSquareDef;
  }

  for (var i = 0; i >= boardY; i++) {
    defaultDrawnBoard += drawnBoardX + "\n";
  }

  botChannel.send(defaultDrawnBoard); // <--- error here!
}


我不知道为什么在世界上字符串没有得到任何文本。有人注意到有什么事吗?

最佳答案

首先,您需要初始化defaultDrawnBoard和DrawnBoardX,否则循环将打印类似undefined|| || || |的内容


  var defaultDrawnBoard =“”;
  
  var DrawnBoardX =“”;


在BoardDraw函数内部,使用i<=而不是i> =,因为这不会使循环开始。

因此您的代码应如下所示:

const boardSquareDef = "| |"
const boardSquareRed = "|R|"
const boardSquareBlue = "|B|"

var defaultDrawnBoard="";
var drawnBoardX="";

var boardX = 7;
var boardY = 6;

var botChannel = client.channels.get("469825469697622029");

// This function is called elsewhere
function BoardDraw ()
{
  for (var i = 0; i <= boardX; i++) {
    drawnBoardX += boardSquareDef;
  }

  for (var i = 0; i <= boardY; i++) {
    defaultDrawnBoard += drawnBoardX + "\n";
  }

  botChannel.send(defaultDrawnBoard); // <--- error here!
}

10-05 23:21