[英]How to fix “TypeError: Cannot read property 'toString' of undefined” | discord.js
Everything works perfectly, except for the end part where it shows who won the giveaway.一切都很完美,除了最后部分显示谁赢得了赠品。 I'm trying to convert the
winners
array into a string so that I can send a message that shows the winner(s).我正在尝试将
winners
数组转换为字符串,以便我可以发送一条显示获胜者的消息。 I spent an entire day trying to fix this (I'm not too good at programming, to be honest) to no avail.我花了一整天的时间试图解决这个问题(说实话,我不太擅长编程)但无济于事。 Please help!
请帮忙!
message.channel.send(" **GIVEAWAY** ").then(() => { message.channel.send(Embed).then((gMessage) => { gMessage.react(""); setTimeout(function() { var peopleReacted = gMessage.reactions.cache.get("").users; var winners = []; // Checks if fewer people reacted than the the winner count allows users to win if (peopleReacted.length <= args[2]) { winners = peopleReacted; } else { // Gets as many random users from the peopleReacted as the winner count allows users to win for (var i = 0; i < args[2]; i++) { var index = Math.floor( Math.random() * peopleReacted.length ); winners.push(peopleReacted[index]); // After adding a user to winners, remove that item from the array to prevent them from winning multiple times peopleReacted.cache.array().splice(index, 1); } } var winnerMsg = "User(s) "; for (var i = 0; i < winners.length; i++) { // Add each winner to the winnerMsg winnerMsg += winners[i].toString() + ", "; } var haveHas; if (winners.length === 1) { haveHas = "has"; } else { haveHas = "have"; } message.channel.send(`${winnerMsg} ${haveHas} won ${msgArgs}`); }, time * (60 * 1000)); }); });
If it helps, all of my bot's code can be found here .如果有帮助,我的所有机器人代码都可以在这里找到。
gMessage.reactions.cache.get("").users
is probably the problem, since gMessage.reactions.cache.get("")
returns a collection, it does not have an users property (= it is undefined
). gMessage.reactions.cache.get("").users
可能是问题所在,因为gMessage.reactions.cache.get("")
返回一个集合,它没有 users 属性(= 它是undefined
)。 If you want the reaction as an array, simply use gMessage.reactions.cache.get("").array()
, this will return an array with the user objects (Note: Those are not "normal" user objects, but ReactionUserManager
, you can find more on these in the docs https://discord.js.org/#/docs/main/stable/class/ReactionUserManager )如果您想将反应作为一个数组,只需使用
gMessage.reactions.cache.get("").array()
,这将返回一个包含用户对象的数组(注意:这些不是“普通”用户对象,而是ReactionUserManager
,您可以在文档https://discord.js.org/#/docs/main/stable/class/ReactionUserManager中找到有关这些的更多信息)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.