Discord.js: Error while using Webhooks - "Cannot read property 'client' of undefined"

Created on 1 Aug 2017  Â·  3Comments  Â·  Source: discordjs/discord.js

Please describe the problem you are having in as much detail as possible:
I just started using Webhooks a couple of minutes ago, and I ran into an issue. Here's my code:

const config = require("../config.json");

const Discord = require("discord.js");

const hooker = new Discord.WebhookClient(config.webhook.id, config.webhook.token);

module.exports = hooker.send;

The webhook ID and token are not undefined or anything, but when I try to run this:

require("./log.js")("Test")

I get this error:

TypeError: Cannot read property 'client' of undefined
    at send (F:\Projects\Guess That Number\node_modules\discord.js\src\structures\Webhook.js:125:16)
    at eval (eval at execute (F:\Projects\Guess That Number\commands\eval.js:19:40), <anonymous>:2:1)
    at Object.execute (F:\Projects\Guess That Number\commands\eval.js:19:40)
    at module.exports (F:\Projects\Guess That Number\functions\handle-message.js:25:38)
    at Client.bot.on (F:\Projects\Guess That Number\events\message.js:4:32)
    at emitOne (events.js:96:13)
    at Client.emit (events.js:191:7)
    at MessageCreateHandler.handle (F:\Projects\Guess That Number\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (F:\Projects\Guess That Number\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:102:65)
    at WebSocketConnection.onPacket (F:\Projects\Guess That Number\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:325:35)
    at WebSocketConnection.onMessage (F:\Projects\Guess That Number\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:288:17)

I just tested this on discord.js v12, and I get this error:

TypeError: Cannot read property 'name' of undefined
    at send (F:\Projects\Guess That Number\node_modules\discord.js\src\structures\Webhook.js:111:51)
    at eval (eval at execute (F:\Projects\Guess That Number\commands\eval.js:19:40), <anonymous>:2:1)
    at Object.execute (F:\Projects\Guess That Number\commands\eval.js:19:40)
    at module.exports (F:\Projects\Guess That Number\functions\handle-message.js:25:38)
    at Client.bot.on (F:\Projects\Guess That Number\events\message.js:4:32)
    at emitOne (events.js:96:13)
    at Client.emit (events.js:191:7)
    at MessageCreateHandler.handle (F:\Projects\Guess That Number\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (F:\Projects\Guess That Number\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
    at WebSocketConnection.onPacket (F:\Projects\Guess That Number\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:332:35)
    at WebSocketConnection.onMessage (F:\Projects\Guess That Number\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:294:22)

I don't exactly know what the error is, but can anyone help? It doesn't look like it's an error with my code, because the main stracktrace goes back to the lib itself. Can anyone help?

Include a reproducible code sample here, if possible:

const config = require("../config.json");

const Discord = require("discord.js");

const hooker = new Discord.WebhookClient(config.webhook.id, config.webhook.token);

module.exports = hooker.send;

Further details:

  • discord.js version: 11.1.0
  • node.js version: 7.9.0
  • Operating system: Windows 10 Home x64
  • Priority this issue should have – please be realistic and elaborate if possible: High

  • [x] I found this issue while running code on a __user account__
  • [x] I have also tested the issue on latest master, commit hash: fd11381
question (please use Discord instead)

Most helpful comment

You have to bind the context.
module.exports = hooker.send.bind(hooker);

All 3 comments

thats not a lib issue, this for lib issues

You have to bind the context.
module.exports = hooker.send.bind(hooker);

@SpaceEEC Thanks, that worked!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xCuzImPro picture xCuzImPro  Â·  3Comments

Alipoodle picture Alipoodle  Â·  3Comments

DatMayo picture DatMayo  Â·  3Comments

Acaretia picture Acaretia  Â·  3Comments

ghost picture ghost  Â·  3Comments