【发布时间】:2022-02-02 09:39:14
【问题描述】:
我正在使用收集器构建一个 Discord 机器人,并尝试为游戏轮次收集选票。 但是,机器人不断返回错误的值。它通常会返回undefined 或[object Object]。 stringifier() 函数应该返回一轮的名称,但它返回的是未定义的。例如如果我在 Discord 上输入“spleef”,它应该将“Spleef”输出到控制台和 Discord 中,但它在两个错误中都返回未定义或仅返回错误。
更新:我发现变量返回布尔值(变量是从battleBoxVotes 到 cTFVotes)。
稍后更新:所以我解决了这个问题,但我现在的新问题是,如果变量没有值,我无法将变量 bbv 转换为 ctfv 以不返回错误。控制台记录:(node:8700) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'content' of undefined
const filter = m => (m.author.id != client.user.id);
const channel = message.channel;
const collector = channel.createMessageCollector(filter, { time: 5000 });
collector.on('collect', m => console.log(`Collected ${m.content}`));
collector.on('end', async collected => {
var bb = collected.find(collected => collected.content === 'battle box');
var pw = collected.find(collected => collected.content === 'spleef');
var s = collected.find(collected => collected.content === 'spleef');
var sw = collected.find(collected => collected.content === 'skywars');
var ctf = collected.find(collected => collected.content === 'capture the flag');
const bbv = bb.content || null;
const pwv = pw.content || null;
const sv = s.content || null;
const swv = sw.content || null;
const ctfv = ctf.content || null;
const stringifier = function(a, b, c, d, e) {
let results;
if (a>b&&a>c&&a>d&&a>e) {results = "Battle Box"}
else if (b>a&&b>c&&b>d&&b>e) {results = "Parkour Warrior"}
else if (c>a&&c>b&&c>d&&c>e) {results = "Spleef"}
else if (d>a&&d>b&&d>c&&d>e) {results = "SkyWars"}
else if (e>a&&e>b&&e>c&&e>c) {results = "Capture the Flag"}
return results;
}
message.channel.send(`And the winner is... ${stringifier(bbv, pwv, sv, swv, ctfv)}!`),
console.log(stringifier(bbv, pwv, sv, swv, ctfv))
});
【问题讨论】:
-
哪个
values是错误的/未定义的?在您的问题中澄清这一点 -
从
stringifier()函数返回的results变量返回未定义,已在帖子中修改。 -
你能调试你的
stringifier函数并检查所有参数是否有效吗? -
因为我只是在本地测试了这个功能并且它有效
-
变量(
battleBoxVotes到cTFVotes)似乎返回的是布尔值而不是数字。
标签: javascript node.js discord.js