在开始之前,我想说的是,我看到的页面与我遇到的问题相同。但是由于某种原因,我不知道我的问题在哪里。 我是编程的新手,如果在程序中遇到任何重大错误,请对不起。我将解释我的问题:每次运行代码时,游戏实际上都可以运行,但是过一会儿,我的终端机上出现一长文本,内容为:“ DiscordAPIError:无法发送空消息”。我知道它可能与message.delete代码有关,但我无法修复它。
const botconfig = require("./botconfig.json");
const Discord = require("discord.js");
const bot = new Discord.Client({disableEveryone: true});
let word;
let letter;
let lWord = [];
let charLength;
let txt = [];
let outputTxt;
let misscount;
let misses = 0;
let succes;
bot.on("ready", async () => {
console.log(`${bot.user.username} is online!`);
bot.user.setActivity("Hangman with y'all");
});
bot.on("message", async message => {
if(message.author.bot) return;
if(message.channel.type === "dm") return;
let prefix = botconfig.prefix;
let messageArray = message.content.split(" ");
let cmd = messageArray[0];
if(cmd === `${prefix}word`){
word = messageArray[1];
message.delete(100);
console.log("\x1b[33m", "Word has been set to: " + word);
console.log("\x1b[0m");
startGame();
}
else if(cmd === `${prefix}reset`){
console.log("\x1b[34m", "Player has reset the game");
console.log("\x1b[0m");
reset();
}
else if(cmd === `${prefix}letter`){
letter = messageArray[1];
console.log("\x1b[36m", "Entered letter was: " + letter);
console.log("\x1b[0m")
tryletter();
}
function startGame(){
lWord = word.split("");
charLength = lWord.length;
for(i = 0; i<charLength; i++) {
txt[i] = "?"
}
outputTxt = txt.join(" ");
txtOutput();
}
function txtOutput(){
return message.channel.send(outputTxt);
}
function tryletter() {
misscount = 0;
for(i = 0; i<charLength; i++) {
if(letter === lWord[i]) {
txt[i] = letter;
misscount = 0;
}
else {
misscount++;
}
if(misscount === charLength) {
misscount = 0;
misses++;
lostCheck();
if(misses > 0 && misses < 8) {
return message.channel.send("You have made " + misses + "/8 mistakes!");
}
}
}
winCheck();
outputTxt = txt.join(" ");
if(misses < 8){
return message.channel.send(outputTxt);
}
}
function winCheck() {
succes = 0;
for(i=0; i<charLength; i++) {
if(txt[i] === lWord[i]) {
succes++
}
else {
succes = 0;
}
}
if(succes === charLength) {
succes = 0;
for(i=0; i<charLength; i++) {
if(txt[i] === lWord[i]) {
succes++
}
else {
succes = 0;
}
}
if(succes === charLength && charLength > 0) {
succes = 0;
message.channel.send("You have won, the word was " + word + "!");
console.log("\x1b[32m", "Player has guessed the word");
console.log("\x1b[0m");
reset();
}
}
}
function lostCheck() {
if(misses === 8){
message.channel.send("You lost, the word was " + word + "!");
console.log("\x1b[31m", "Player lost the game");
console.log("\x1b[0m");
reset();
}
}
function reset() {
word = "";
letter = "";
lWord = [];
charLength = 0;
txt = [];
outputTxt = "";
misscount = 0;
misses = 0;
succes = 0;
console.log("\x1b[35m", "Game has been reset");
console.log("\x1b[0m");
return message.channel.send("Game has been reset!");
}
});
bot.login(botconfig.token);
答案 0 :(得分:0)
这可能是因为您尝试在outputTxt
为空(''
或' '
)时发送的消息:Discord仅在其至少有一个非空格的情况下才能发送消息,喜欢性格。
您必须决定当该字符串为空时显示什么。假设如果outputTxt
为空,则您希望机器人发送'None'
:在这种情况下,您应编辑txtOutput()
和tryletter()
以这种方式发送消息>
return message.channel.send(outputTxt || "None");
||
意味着如果第一个变量是undefined
,它将使用秒:这是一种有用的速记形式。
// Extended form
if (outputTxt) return message.channel.send(outputTxt);
else return message.channel.send("None");
// Shorter form with ternary operator
return message.channel.send(outputTxt ? outputTxt : "None");
// Since we're only checking if that exists or not, you can write it directly as below
return message.channel.send(outputTxt || "None");