discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.36k stars 3.97k forks source link

Guild#fetchBan(userId) throws an error even if it resolves successfully #4600

Closed noftaly closed 4 years ago

noftaly commented 4 years ago

Please describe the problem you are having in as much detail as possible: Guild#fetchBan(userId) throws an error even tho it happened successfully...

Include a reproducible code sample here, if possible:

console.log('DEBUG: Starting');
const banInfos = await guild.fetchBan("490635207125368842").catch(console.error);
console.log('DEBUG: Finishing');
console.log(banInfos);

What I get:

DEBUG: Starting
(node:54750) UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Member
    at RequestHandler.execute (/Swan/node_modules/discord.js/src/rest/RequestHandler.js:170:25)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:54750) UnhandledPromiseRejectionWarning: Unhandled promise rejection. blah blah blah
(node:54750) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. blah blah blah
DEBUG: Finishing
{
  reason: 'erzre',
  user: User {
    id: '490635207125368842',
    bot: false,
    username: 'Warrior',
    discriminator: '9558',
    avatar: null,
    flags: UserFlags { bitfield: 0 },
    lastMessageID: null,
    lastMessageChannelID: null
  }
}

Btw I get the same error by "noop-ing" the .catch with .catch(() => {});...

Further details:

Sorry if this is just me doing something wrong but no one was able to answer on the discord.

almostSouji commented 4 years ago

I can not reproduce the described behavior, be it with or without this exact provided id "490635207125368842" on v12.2.0. Can you reproduce this with any ban?

I'm not sure how this specific error can be thrown in this code either.

https://github.com/discordjs/discord.js/blob/1c275afd7cee4b3663a04f77b0271ac80e927a5e/src/structures/Guild.js#L646-L657

647 Try to resolve the provided value to a User UserManager#resolveID / BaseManager#resolveID 648 Throw error if none found ("Couldn't resolve the user ID to fetch the ban." in src/errors/Messages.js l.76) 653 ff. Doing the request (throws "DiscordAPIError: Unknown Ban" if not banned)

The fact that you get "UnhandledPromiseRejectionWarning" further suggests that the provided code is not in fact the cause for the error, as you do catch any error that would throw during the #fetchBan call in the provided code. (would also log undefined, since that's the return value of console.error, which banInfos would resolve to)

Here is one such error log:

DEBUG: Starting
DiscordAPIError: Unknown Ban
    at RequestHandler.execute (C:\Users\souji\Documents\projects\djs-testing\v12\node_modules\discord.js\src\rest\RequestHandler.js:170:25)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  name: 'DiscordAPIError',
  message: 'Unknown Ban',
  method: 'get',
  path: '/guilds/---snip---/bans/1',
  code: 10026,
  httpStatus: 404
}
DEBUG: Finishing
undefined
noftaly commented 4 years ago

The fact that you get "UnhandledPromiseRejectionWarning" further suggests that the provided code is not in fact the cause for the error, as you do catch any error that would throw during the #fetchBan call in the provided code.

From where does it come then?

I tried with another member, same thing. I added a try {} catch {} around the line but the error is not caught. I also tried to add debug messages inside the discordjs module but to be fair I did not understand the code very well... 😂 (in the API calls. My debug message right before the return inside fetchBan was called before the error, but after that, I don't know where to put the console.logs... I tried something and it might come from the .get. Now that I've done it seems obvious, but we got the confirmation 😂)

So yeah something's definitely going wrong because as you said I should not get the baninfo if it threw + with my noop catch it should not log the error + my try/catch around it should catch it anyway...

I don't know if all of that comes from my code, because maybe if I delete all my folder and start from something fresh (= deleting all the cache etc) it will work.

monbrey commented 4 years ago

It comes from somewhere that you attempt to perform an API operation on GuildMember, such as fetching.

The fetchBan method has correct error handling, therefore as Souji pointed out this is not where your UnhandledRejection is thrown.

noftaly commented 4 years ago

It comes from somewhere that you attempt to perform an API operation on GuildMember, such as fetching. The fetchBan method has correct error handling, therefore as Souji pointed out this is not where your UnhandledRejection is thrown.

How can i know where it comes from then ? Because my humble debugging clearly showed that the error occured at fetchBan... I added a debug in discord.js right before the .get.then() and it appeared before the error, and one in my code right after de .fetchBan and it appeared after the error...

EDIT: The other developper working with me on this bot had the same error with the same code... (yet we have our own client and our own databases etc etc... Just the code is the same as it is sync with github)

almostSouji commented 4 years ago

If you still believe this to be a failure of the #fetchBan method please move the faulty code into an isolated environment (new bot) and strip away its functionalities until you reached the bare-bones of what's needed to reproduce this behavior (mwe).

If you can still reproduce this after isolating your code please post said example here (it should be in a format where others can just download it, plug their token into the login function and be able to reproduce and verify this bug report). Further please provide the exact steps as manual we can follow to attempt reproduction.


Because my humble debugging clearly showed that the error occured at fetchBan... I added a debug in discord.js right before the .get.then() and it appeared before the error, and one in my code right after de .fetchBan and it appeared after the error...

That's a misconception on your end, as i explained above your debugging showed that the error is not from anything happening in #fetchBan, else you would not get the behavior you show in op (unhandled rejection)

so far you provided

console.log('DEBUG: Starting');
const banInfos = await guild.fetchBan("490635207125368842").catch(console.error);
console.log('DEBUG: Finishing');
console.log(banInfos);

which, if plugged into a fresh bot (without whatever other functionality you have on yours), does not result in the behavior you outline, as i explained in this comment.

If your co-dev runs the same (complete) code, them too experiencing the same issue is expected.

noftaly commented 4 years ago

I finally found where this came from, it was indeed another part of the code where i fetched the member. The timing of the debug messages made me think this was coming from the fetchban but it was not... Sorry for bothering you all and thanks for your time :)