REST Crash
I am using the REST class of discord.js to edit messages with the interaction token, I am using .patch on the webhookMessage endpoint.
It normally works, but I sometimes get an invalid token error which crashes the entire server, even though the request is awaited and in a try catch block. What is the cause of this, are there any solutions? Ty!
10 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!Sure! The error is:
home/container/node_modules/@discordjs/rest/dist/index.js:730
throw new DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
^
DiscordAPIError[50027]: Invalid Webhook Token
at handleErrors [90m(/home/container/[39mnode_modules/[4m@discordjs[24m/rest/dist/index.js:730:13[90m)[39m
[90m at process.processTicksAndRejections (node:internal/process/task_queues:95:5)[39m
at async SequentialHandler.runRequest [90m(/home/container/[39mnode_modules/[4m@discordjs[24m/rest/dist/index.js:1133:23[90m)[39m
at async SequentialHandler.queueRequest [90m(/home/container/[39mnode_modules/[4m@discordjs[24m/rest/dist/index.js:963:14[90m)[39m
at async _REST.request [90m(/home/container/[39mnode_modules/[4m@discordjs[24m/rest/dist/index.js:1278:22[90m)[39m
at async addUser [90m(/home/container/[39mcommands/multiGame.js:201:7[90m)[39m {
requestBody: {
files: [90mundefined[39m,
json: {
embeds: [
// not relevant
]
}
},
rawError: { message: [32m'Invalid Webhook Token'[39m, code: [33m50027[39m },
code: [33m50027[39m,
status: [33m401[39m,
method: [32m'PATCH'[39m,
url: [32m'https://discord.com/api/v10/webhooks/1225076420980641882/aW50ZXJhY3Rpb246MTI2NDkwNzc2NTI5MDEwNjkwMDpOQnREa2lkSEdER2dmSnU0VkdPN1pRbWJrZDF0dUhhY2wxTTg0SHRzQVphTXMxMmxDTENtcE1LdVZpM0xGUUtkaDdqRHBSbWh4WnpuU0pVeEdDVGd5OENabUhDa2NJSlZLVVcxejJzQUJpbVVpZmdmQ2lUN0hLbWFMdXNjVGNkQg/messages/@original'[39m
}
are you trying to edit the reply after 15 minutes (of the original interaction happening)?
could be the case actually, since it is a rare one
thanks
but still, i don't get how the server crashes :OMEGAlul:
it's awaited and in try catch
Alrighty:
client.on("interactionCreate", async (interaction) => {
if (!interaction.isButton()) return;
if (interaction.customId !== "single_guess_confirm") return;
try {
// ...
const discordApi = new REST().setToken(client.token);
await discordApi.patch(Routes.webhookMessage(client.user.id, token), {
files: [{ data: finalMapImage, name: "map.png" }],
body: {
// not relevant, embed with buttons
},
});
// ...
} catch (error) {
console.error(error);
}
});
where are you getting token and is there a reason you can't just use
interaction.editReply
instead
nvm where you get token is probably irrelevantthe original message has a button that replies with an ephemeral containing yet another button, which edits the original
if that makes sense lol
oh so you're using rest because you don't have the context of the original interaction anymore
you wouldn't happen to be getting
token
from the current interaction though would you? that would only let you edit the ephemeral message you're sending
Would it be easier to get that messageId you're trying to edit via interaction.message.reference.messageId
and edit it with interaction.channel.messages.edit(messageId, options)
since it's non-ephemeral(?)Hmmm yeah, I intend it to be a user app though and that would lead to permission issues
Biut thanks
well you need to keep context of the first interaction then, since the token for both replies is going to be different
It's a button press event
Sorry, line 201 is the one starting with await discordApi.patch(...)
But I might just consider a whole new approach to this, something without rest
Ahh yes I’ll use the built in WebhookClient, thanks for the help though :)
Gotcha, thank you