简体   繁体   English

如何修复“TypeError:无法读取未定义的属性 'toString'” | discord.js

[英]How to fix “TypeError: Cannot read property 'toString' of undefined” | discord.js

I'm trying to make a giveaway command for my discord bot.我正在尝试为我的 discord 机器人制作赠品命令。

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.

相关问题 如何解决:“TypeError:无法读取discord.js中未定义的属性&#39;标签&#39;错误 - How to fix: “TypeError: Cannot read property 'tag' of undefined” error in discord.js 如何解决discord.js中的“无法读取未定义的属性&#39;反应&#39;” - How to fix “Cannot read property 'react' of undefined” in discord.js discord.js错误TypeError:无法读取未定义的属性“ voiceChannel” - discord.js Error TypeError: Cannot read property 'voiceChannel' of undefined Discord.JS:TypeError:无法读取未定义的属性“角色” - Discord.JS:TypeError: Cannot read property 'roles' of undefined discord.js TypeError:无法读取未定义的属性“名称” - discord.js TypeError: Cannot read property 'name' of undefined “ TypeError:无法读取未定义的属性&#39;push&#39;” discord.js - “TypeError: Cannot read property 'push' of undefined” discord.js 类型错误:无法使用 Discord.js 读取未定义的属性“集” - TypeError: Cannot read property 'set' of undefined with Discord.js 类型错误:无法读取未定义的 Discord.js 错误的属性“get” - TypeError: Cannot read property 'get' of undefined Discord.js Error 类型错误:无法读取未定义的属性“成员” - Discord.JS - TypeError: Cannot read property 'members' of undefined - Discord.JS 类型错误:无法读取未定义 discord.js 的属性“解决” - TypeError: Cannot read property 'resolve' of undefined discord.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM