我目前正在使用discord.js编写一个discord机器人,并停留在此错误上:
UnhandledPromiseRejectionWarning: TypeError: Cannot create property 'reply' on number '1000'
每次“抢劫”成功时都会出现错误。
这是我的代码:
const Discord = require('discord.js');
const fs = require('fs');
let coins = JSON.parse(fs.readFileSync('./coins.json', 'utf8'));
module.exports.run = async (bot, message, args) => {
let userDiff = args[0];
let userChance = 0.333 * userDiff;
var userEarned = 0
let numAmt = Math.floor(Math.random() * userDiff) + 1;
if(userDiff > 10){
messange.channel.send('Please enter a number between 1-10')
} else if(userDiff <= 10 && userDiff >= 9){
var userEarned = 1000
} else if(userDiff < 9 && userDiff >= 8){
var userEarned = 900
} else if(userDiff < 8 && userDiff >= 7){
var userEarned = 800
} else if(userDiff < 7 && userDiff >= 6){
var userEarned = 700
} else if(userDiff < 6 && userDiff >= 5){
var userEarned = 600
} else if(userDiff < 5 && userDiff >= 4){
var userEarned = 500
} else if(userDiff < 4 && userDiff >= 3){
var userEarned = 400
} else if(userDiff < 3 && userDiff >= 2){
var userEarned = 300
} else if(userDiff < 2 && userDiff >= 1){
var userEarned = 200
}
console.log('numAmt: ', numAmt);
console.log('userChance: ', userChance);
if(numAmt <= userChance){
message.channel.send('Robbery was Succesful you earned ', userEarned)
coins[message.author.id]['coins'] += userEarned
fs.writeFile("./coins.json", JSON.stringify(coins), (err) => {
if (err) console.log(err)
});
}
if(numAmt >= userChance){
message.channel.send('Robbery was Unsuccesful you have been arested, You can pay to bail out or wait 5 mins')
}
}
module.exports.help = {
name: 'bankrobbery'
}
答案 0 :(得分:1)
在您的代码中,您编写了以下代码:
message.channel.send('Robbery was successful, you earned ', userEarned);
这意味着您要传递字符串作为消息,然后将userEarned
传递为MessageOptions
,如TextChannel.send()
的文档所述。
我认为您想做的是将两个字符串合并:您可以使用+
运算符来实现。
message.channel.send('Robbery was successful, you earned ' + userEarned);
或者,您可以使用其他字符串语法:
message.channel.send(`Robbery was successful, you earned ${userEarned}`);