r/badcode • • Sep 17 '19

js Discord.js is op

Post image
5 Upvotes

12 comments sorted by

24

u/00benallen Sep 17 '19

I'm not noticing what's so bad about this. This is what data from large APIs generally looks like, the code author seems to be coding correctly.

7

u/fl223 Sep 17 '19

I'm guessing commandData.message.author is the same as the member they are looking for here.

8

u/spaghettu Sep 17 '19

They may be exaclty the same, but the point isn't to try and "find" the member, they're trying to see if the member is in the guild list. So even if they already have a member, if that member isn't in that guild, the find will return null. So it doesn't seem like a pointless check to me.

15

u/ColdSnickersBar Sep 17 '19

Eh, there's no way to know that just from this example.

The members of commandData.client.guilds.members might be a different type than commandData.message.author. Especially if guild members aren't necessarily message authors in whatever product this is.

3

u/Superpickle18 Sep 17 '19

i think the code is checking if the author is in the first guild the bot is in, not the guild the message appears.

-1

u/natemi10 Sep 17 '19

Yep. The message class has a member variable which he uses to compare ids with :D Then checks if member is null when the author of a message can't be null

5

u/3636373536333662 Sep 17 '19

But they're checking whether or not the author's id exists in the guild collection, not whether it exists in general. If it's not in the collection, then find would return null.

25

u/natemi10 Sep 17 '19

Sure, get the member of the message by going through all the members in the server and checking whether they have the same id as the member that sent the message.

7

u/ColdSnickersBar Sep 17 '19

What if the members of commandData.client.guilds.members are a different type than commandData.message.author? Especially if guild members aren't necessarily message authors in whatever product this is.

I don't see anything in this example that would lead me to believe that we can trust the author is a in the list of members, or even that they're the same type at all.

10

u/spaghettu Sep 17 '19

So we have a message author, which presumably is the same type as member, but we need to see if they belong to a guild member list. So they search the guild list with find for that member id. Maybe the search is linear (I don't use JS so I'm not familiar), but I otherwise don't see the problem. I guess it'd be faster if each member already had a list of guilds it was a member of directly on the object, but I mean to me this doesn't seem that bad. Am I missing something?

5

u/3636373536333662 Sep 17 '19

Seems fine to me too. The search is linear, but it won't really matter unless the guild list is quite large.

1

u/Plus_Cryptographer Sep 19 '19

Seems like the problem here would more be that you are checking for a command in a server, and then immediately check if the person sending that is on the server.

e.g. between when the user sent their command and you checking it, it's possible that the user left the server?