简体   繁体   中英

Cannot read property 'send' of undefined discord.js

Does anyone know what's wrong? I am using this line of script -

client.on('presenceUpdate', (oldPresence, newPresence) => {
    let member = newPresence.member;
    if (member.id === '603517534720753686') {
        if (oldPresence && oldPresence.status !== newPresence.status) {
            channel = client.channels.cache.get('788547135234375712');
            let text = "";
            if (newPresence.status === "online") {
                text = "**Hello @everyone, Timer Bot is now online! Thank you for your patience.**";
            } else if (newPresence.status === "offline") {
                text = "**@everyone Due to issues, Timer Bot is currently offline. We apologize for the inconvenience.**";
            }
            channel.send(text);
        }
    }
});

Every time I run it, my console says -

/home/runner/TimerUtilities/index.js:32
            channel.send(text);
                    ^

TypeError: Cannot read property 'send' of undefined
    at Client.<anonymous> (/home/runner/TimerUtilities/index.js:32:21)
    at Client.emit (events.js:315:20)
    at Client.EventEmitter.emit (domain.js:483:12)
    at PresenceUpdateAction.handle (/home/runner/TimerUtilities/node_modules/discord.js/src/client/actions/PresenceUpdate.js:39:19)
    at Object.module.exports [as PRESENCE_UPDATE] (/home/runner/TimerUtilities/node_modules/discord.js/src/client/websocket/handlers/PRESENCE_UPDATE.js:4:33)
    at WebSocketManager.handlePacket (/home/runner/TimerUtilities/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at Immediate.<anonymous> (/home/runner/TimerUtilities/node_modules/discord.js/src/client/websocket/WebSocketManager.js:379:14)
    at processImmediate (internal/timers.js:456:21)

It looks like "channel" is unidentified. Does anyone know how to make it identified? Thanks!

Try this

client.channels.get("ID").send("Your message")

Tried doing 'message.channel.send(text)' rather than 'channel.send(text)'?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM