Search code examples
javascripttypeerrordiscordsend

TypeError: Cannot read property 'send' of undefined discord bot


Does anyone know how to fix this?

    client.channels.get(config.logChannel).send(embed)
                                          ^

TypeError: Cannot read property 'send' of undefined
    at Client.client.once (/home/fynn/DiscordTickets/server.js:119:43)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:111:20)
    at Client.emit (events.js:208:7)
    at WebSocketConnection.triggerReady (/home/fynn/DiscordTickets/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:128:17)
    at WebSocketConnection.checkIfReady (/home/fynn/DiscordTickets/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:144:61)
    at ReadyHandler.handle (/home/fynn/DiscordTickets/node_modules/discord.js/src/client/websocket/packets/handlers/Ready.js:80:8)
    at WebSocketPacketManager.handle (/home/fynn/DiscordTickets/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:108:65)
    at WebSocketConnection.onPacket (/home/fynn/DiscordTickets/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:336:35)
    at WebSocketConnection.onMessage (/home/fynn/DiscordTickets/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:299:17)

public source code: https://github.com/eartharoid/DiscordTickets

      .setAuthor(`${client.user.username} / Ticket Log`, client.user.avatarURL)
      .setColor("#2ECC71")
      .setDescription(":white_check_mark: **Started succesfully**")
      .setFooter(`DiscordTickets by Eartharoid`);
    client.channels.get(config.logChannel).send(embed)
  } else {
    client.channels.get(config.logChannel).send(":white_check_mark: **Started succesfully**")
  } ```

Solution

  • Are you sure the bot has access to that channel?
    This is because if the client is not able to see the channel, it will give you an undefined back

    Are you sure the ID in of the channel in your config is a String?
    discord.js needs a Snowflake in the ChannelManager.get() method

    Maybe try this instead of .get()

    client.channels.fetch(config.logChannel)
    

    This is a method that ships with the ChannelManager, that as far as I know does basicly the exact same as .get() method on a Discord Collection.

    Sorry for my not so good english, tried my best to answer your questions with some explanations! 😅