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";


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.
Was this page helpful?