obj.forEach() does not work

I'm new to Discord ... so, no, I don't know what I'm doing ... & also no, I can't find any examples or discussion online. That's why I'm here. Started working on a bot to assign a new user role after new member accepts rules ... then self-assigns their roles. Old msgs & reactions are not cached when bot is initialized. I can hard code the chanID/msgID & initialize/cache them so that events will fire ... specifically the messageReactionAdd/Remove events. The main things that I want to accomplish are the role assignments & toggle the other reactions off to force only a single reaction for polls & things. So now I'd like to make this a function instead of hard coding every single chan/msg combination. So my objective is to build an object in 'config.json' to store the chan/msg ID's so they will be cached/initialized during bot's clientReady event. typeof(Initializations) = object However, I cannot figure out how to ... for(msg in/of obj) ... obj.forEach() ... or obj.each(). What am I doing wrong?
No description
21 Replies
d.js toolkit
d.js toolkitβ€’8mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - βœ… Marked as resolved by OP
Rough Rider-L 94
Rough Rider-L 94β€’8mo ago
the returned error ...
No description
Pulse
Pulseβ€’8mo ago
you should post what ../config.json looks like
treble/luna
treble/lunaβ€’8mo ago
Use partials, they are made for that
Rough Rider-L 94
Rough Rider-L 94β€’8mo ago
config.json ... { "token" : "nope", "clientId" : "another nope", "guildId" : "another nope", "bots" : ["list of bot names here"], "AdminRoles" : ["Admin", "Mods", "Server Mgrs", "other admin roles ..."], "UserRoles" : ["New Users", "user_role_1", "user_role_2", "user_role_3"], "Initializations" : { "rules" : { "chanId" : "rules chan id goes here", "msgId" : "its msg id goes here" }, "roles" : { "chanId" : "roles chan id goes here", "msgId" : "its msg id goes here" }, "polls" : { "poll_1" : { "chanId" : "poll_1 chan id goes here", "msgs" : [ "1st msg id goes here", "other msg id's after this", "etc" ] } } } }
Rough Rider-L 94
Rough Rider-L 94β€’8mo ago
partials ... like these ???
No description
Rough Rider-L 94
Rough Rider-L 94β€’8mo ago
ahh ... i suspect i had an old or bad example ... Partials should be declared in the ... const {Client, Partials, other stuff...} = require('discord.js;)' then my ... partials: [list here] ... is probably the wrong syntax too
Pulse
Pulseβ€’8mo ago
in your config.json, Initializations is an object, not an array. so yes, all of those functions you tried to call on it would be invalid
Rough Rider-L 94
Rough Rider-L 94β€’8mo ago
so ... Initializations.cache ... is probably wrong too ?
Pulse
Pulseβ€’8mo ago
given your json, yes, since cache is not a property of Initializations
Rough Rider-L 94
Rough Rider-L 94β€’8mo ago
so convert to ... Initializations: [ stuff ] ... then call w/ ... Initializations[rules] ... like normal for specific items ? ... and the ... Initializations.forEach() ... should then work too? ... ie. to get the chanID ... Initializations[rules][chanId]
Pulse
Pulseβ€’8mo ago
it's developers choice. making Initializations an array would work, but if rules and roles and polls are hard-coded/expected keys, those could be arrays instead of Initializations itself. but this is starting to get into #other-js-ts territory but yes, making Initializations an array would solve the original issue
Rough Rider-L 94
Rough Rider-L 94β€’8mo ago
sweet ... last q ... so then the calling syntax would remain the same ... obj[key][next key][& so on] ... ? ... & yes, i'm trying to get away from the static coding & make it a dynamic handler to initialize all the stuff
treble/luna
treble/lunaβ€’8mo ago
Partials are explained in the guide and remove the need for this code as they provide events for uncached messages
Rough Rider-L 94
Rough Rider-L 94β€’8mo ago
ok ... but in order to toggle the reactions & restrict the user's selection to only 1 reaction ... ie. for a vote or poll ... i'm going to have to avoid partials since i don't necessarily want/need the msgReactAdd/Remove events to fire for every uncached msg ... yes/no ? how else to isolate & restrict the event trigger to occur only on certain msg's ... except for maintaining a list of chan/msg id's ? @Pulse ... thanks for the suggested solution ... but many more thanks for the discussion !!!
treble/luna
treble/lunaβ€’8mo ago
you will have to check for the id But why not use buttons or selectmenus?
Rough Rider-L 94
Rough Rider-L 94β€’8mo ago
those are beyond my current paygrade ... lol
treble/luna
treble/lunaβ€’8mo ago
they are much more easier to use than your current approach though
Rough Rider-L 94
Rough Rider-L 94β€’8mo ago
ain't that the God's honest truth right there tbh ... i haven't seen much on buttons & select menus reactions are the main interaction w/ msg's that i've seen so my why, is because, up until you mentioned it just now, i had no clue about them or that I should even look into using them
treble/luna
treble/lunaβ€’8mo ago
the guide fully covers them
Rough Rider-L 94
Rough Rider-L 94β€’8mo ago
well, i'll now look into the buttons & select menus @wolvinny 🌈 many thanks for the suggestion & discussion