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](https://cdn.answeroverflow.com/1171669990127181894/image.png)
21 Replies
- 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 OPthe returned error ...
![No description](https://cdn.answeroverflow.com/1171673592166105168/image.png)
you should post what
../config.json
looks likeUse partials, they are made for that
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"
]
}
}
}
}
partials ... like these ???
![No description](https://cdn.answeroverflow.com/1171677464062332928/image.png)
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
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 invalidso ... Initializations.cache ... is probably wrong too ?
given your json, yes, since
cache
is not a property of Initializations
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]
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 issuesweet ... 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
Partials are explained in the guide and remove the need for this code as they provide events for uncached messages
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 !!!