Fetching webhook breaks inconsistently

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";
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)'
}
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)
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.
2 Replies
d.js docs
d.js docs2y ago
• 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.
HyperNeutrino
HyperNeutrino2y ago
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) ah oops alright lemme try to test that hey sorry how do I test that? 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);
});
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 oh thanks lol the issue happens only after i fetch an invalid URL; i've been doing this previously as well and never had issues since i never tried entering an invalid URL oh like through client.on? not sure how to do that either sorry 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 oh interesting i suppose i could just fetch it without the webhook token then, since the bot does request Manage Webhooks... lmao rip oh weird ok so would a workaround be to just set the client's token again if this GET fails? 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 can't wait to run into some weird race condition 3 months later when someone else's command invocation fails between when the REST client discards its token and when i can set it back