无法发送空消息

我目前正在与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!
}

我无法弄清楚为什么世界上的字符串没有得到任何文字 . 有没有人注意到什么?

回答(1)

2 years ago

首先需要初始化 defaultDrawnBoarddrawnBoardX ,否则循环打印这样的东西 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!
}