#Fetching webhook breaks inconsistently

51 messages · Page 1 of 1 (latest)

brittle quail
#

I have a command that takes a webhook URL and should do something (haven't gotten there yet) with it. It works fine if I start up the bot and call it on a valid URL.

However, if I call this command on a webhook URL with an invalid token, then when I call the command again with a valid URL, it does not work until I restart the bot.

This is the core of the code:

const webhook_url = cmd.options.getString("webhook");
const match = webhook_url.match(
    /https:\/\/discord\.com\/api\/webhooks\/(\d+)\/(.+)/
);

if (!match) {
    return fail("That does not appear to be a valid webhook URL.");
}

console.log(match[1], match[2]);

let webhook;

try {
    webhook = await cmd.client.fetchWebhook(match[1], match[2]);
} catch (error) {
    console.error(error);
    return fail(
        "That webhook does not exist, or the token is invalid (make sure you copy-pasted correctly)."
    );
}

return "OK";

The error I get when I use it on an invalid URL is this:

DiscordAPIError[50027]: Invalid Webhook Token
    at SequentialHandler.runRequest (/mnt/c/Users/hyper/Desktop/hyper-neutrino/projects/tcn-autosync/node_modules/@discordjs/rest/dist/index.js:743:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async SequentialHandler.queueRequest (/mnt/c/Users/hyper/Desktop/hyper-neutrino/projects/tcn-autosync/node_modules/@discordjs/rest/dist/index.js:549:14)
    at async REST.request (/mnt/c/Users/hyper/Desktop/hyper-neutrino/projects/tcn-autosync/node_modules/@discordjs/rest/dist/index.js:988:22)
    at async Client.fetchWebhook (/mnt/c/Users/hyper/Desktop/hyper-neutrino/projects/tcn-autosync/node_modules/discord.js/src/client/Client.js:316:18)
    at async execute (file:///mnt/c/Users/hyper/Desktop/hyper-neutrino/projects/tcn-autosync/src/commands/autosync.js:262:27)
    at async Client.<anonymous> (file:///mnt/c/Users/hyper/Desktop/hyper-neutrino/projects/tcn-autosync/src/index.js:35:28) {
  requestBody: { files: undefined, json: undefined },
  rawError: { message: 'Invalid Webhook Token', code: 50027 },
  code: 50027,
  status: 401,
  method: 'GET',
  url: 'https://discord.com/api/v10/webhooks/(ID)/(TOKEN)'
}

Then, when I use it on the correct URL with the correct token, I get the following:

Error: Expected token to be set for this request, but none was present
    at RequestManager.resolveRequest (/mnt/c/Users/hyper/Desktop/hyper-neutrino/projects/tcn-autosync/node_modules/@discordjs/rest/dist/index.js:865:15)
    at RequestManager.queueRequest (/mnt/c/Users/hyper/Desktop/hyper-neutrino/projects/tcn-autosync/node_modules/@discordjs/rest/dist/index.js:838:46)
    at REST.raw (/mnt/c/Users/hyper/Desktop/hyper-neutrino/projects/tcn-autosync/node_modules/@discordjs/rest/dist/index.js:992:32)
    at REST.request (/mnt/c/Users/hyper/Desktop/hyper-neutrino/projects/tcn-autosync/node_modules/@discordjs/rest/dist/index.js:988:33)
    at REST.get (/mnt/c/Users/hyper/Desktop/hyper-neutrino/projects/tcn-autosync/node_modules/@discordjs/rest/dist/index.js:973:17)
    at Client.fetchWebhook (/mnt/c/Users/hyper/Desktop/hyper-neutrino/projects/tcn-autosync/node_modules/discord.js/src/client/Client.js:316:34)
    at execute (file:///mnt/c/Users/hyper/Desktop/hyper-neutrino/projects/tcn-autosync/src/commands/autosync.js:262:44)
    at Client.<anonymous> (file:///mnt/c/Users/hyper/Desktop/hyper-neutrino/projects/tcn-autosync/src/index.js:35:34)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Except, the console.log(match[1], match[2]) indicates that I am providing the token in the Client#fetchWebhook call.

native oceanBOT
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

astral turtle
#

cc @sturdy hemlock since the other thread never had any followup at all

brittle quail
#

hm, it looks similar, but i'm not using djs/rest so i am not sure if it is caused by the exact same thing - (also yeah unless i missed it i don't see a fix there? looks like a similar issue though)

astral turtle
#

you're not using it directly

#

at RequestManager.resolveRequest (/mnt/c/Users/hyper/Desktop/hyper-neutrino/projects/tcn-autosync/node_modules/@discordjs/rest/dist/index.js:865:15)

#

yeah, there is no fix since the guy never came back with anything

#

so we;re in same place as we were 3 months ago

#

now, if you could maybe try to test that

#

that would be wonderful 😅

brittle quail
#

alright lemme try to test that

brittle quail
#

i've never used djs/rest before; currently just trying the following

#
const rest = new REST({ version: "10" }).setToken(config.discord_token);

rest.on("response", (req, res) => {
    console.log(req, res);
});
#

(just inserted this right before my login line)

#

but i am not getting any output

astral turtle
#

you don't have to use rest directly

#

i believe you should be able to just listen to the rest events

vagrant mesa
#

I didn't realise this was an issue, my bot fetches webhooks on startup for months and never encountered this

astral turtle
#

did it ever fetch one with invalid url/token?

vagrant mesa
vagrant mesa
brittle quail
brittle quail
vagrant mesa
#

I think /rest unsets the token, that's why

astral turtle
#

i think you can attach listeners on client.rest (so client.rest.on())

vagrant mesa
#

Invalid webhook tokens are a 401, so

#

The token will be removed, then the subsequent API call will not have the token set

#

Also @calm owl if you wanna read

brittle quail
#

wait, like it unsets the client's token or something?

#

since I do provide the webhook's token again in the next call

#

anyway yes, I do get a 401 from the invalid webhook fetch which then causes the subsequent GET to not even be called

astral turtle
#

yes, we essentially never check the path

#

and always assume that it's your token that's invalid

brittle quail
#

oh interesting

astral turtle
#

isn't it

brittle quail
#

i suppose i could just fetch it without the webhook token then, since the bot does request Manage Webhooks...

astral turtle
#

this was found and bright up internally those 3mo ago

#

but topic was just essentially shrugged off as "discord sometimes sends 401 lol"

vagrant mesa
brittle quail
#

so would a workaround be to just set the client's token again if this GET fails?

astral turtle
#

you probably could put a bandaid out of REST.setToken(), yeah

brittle quail
#

hm, yep adding a cmd.client.rest.setToken(...) in the catch block does fix this

#

alright thanks ❤️ that's pretty scuffed though but it works so oh well