UnhandledPromiseRejectionWarning:TypeError:无法在数字“1000”上创建属性“回复”
Posted
技术标签:
【中文标题】UnhandledPromiseRejectionWarning:TypeError:无法在数字“1000”上创建属性“回复”【英文标题】:UnhandledPromiseRejectionWarning: TypeError: Cannot create property 'reply' on number '1000' 【发布时间】:2019-02-13 09:54:58 【问题描述】:我目前正在使用 discord.js 编写一个不和谐机器人,但遇到了这个错误:
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'
【问题讨论】:
对不起,我把实际代码而不是图像放在了如此无知的地方 【参考方案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`);
【讨论】:
以上是关于UnhandledPromiseRejectionWarning:TypeError:无法在数字“1000”上创建属性“回复”的主要内容,如果未能解决你的问题,请参考以下文章
[Unhandled promise rejection: TypeError: null is not an object (evaluating '_reactNativeImageCropPic