discordjs / discord.js

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

Webhook#token returns undefined #3605

Closed KhafraDev closed 4 years ago

KhafraDev commented 4 years ago

Please describe the problem you are having in as much detail as possible:

Include a reproducible code sample here, if possible:

let webhook = await message.channel.fetchWebhooks();
if(webhook.size) {
    webhook = await webhook.first().edit({ name: message.author.username, avatar: message.author.displayAvatarURL() });
} else {
    webhook = await message.channel.createWebhook(message.author.username, { avatar: message.author.displayAvatarURL() 
}
console.log(`https://discordapp.com/api/webhooks/%s/%s`, webhook.id, webhook.token);
// ... /webhooks/id/undefined

Further details:

MBR-0001 commented 4 years ago

According to the docs, Webhook#token cannot be null/undefined (and it wouldn't make sense for it to be, as you can't use a webhook without it).

This used to be true until Discord added server announcement following, webhooks that are used for following don't have the token and have type 2 (images below)

image

image

This PR should solve this

almostSouji commented 4 years ago

According to the docs, Webhook#token cannot be null/undefined (and it wouldn't make sense for it to be, as you can't use a webhook without it).

This information is wrong, according to discords documentation .token can be undefined, and that will be the case for the "webhooks" generated from following an announcement channel. You can not utilize these with a bot, and should filter them out. webhooks.filter(hook => hook.token) or a similar approach should do.

Nonetheless this needs to be edited in 11.5-dev and 12.0.0-dev and typings.

KhafraDev commented 4 years ago

@almostSouji this issue occurs when creating or getting an existing webhook (as shown in my code above). When getting the URL manually, it shows a token, and the ID is the same as the Webhook object shown in the output. I am not using a webhook for announcements, but a "normal" one that was generated using the code shown in my initial message. When referring to the "docs" I was referring to the discord.js.org docs, my bad - https://discord.js.org/#/docs/main/master/class/Webhook?scrollTo=token

@MBR-0001 Thanks for the info, last time I was active in bot development that announcement hadn't came out yet. I'll check out the PR!

Thank you both for the quick responses