简体   繁体   中英

How to send a message in a specific channel with id

I used replit database to save channel id
This is my code:
client.channels.cache.get(db.get(args[1])).send(args[2]);
and i get this error:

client.channels.cache.get(db.get(args[1])).send(args[2]);
                                                ^

TypeError: Cannot read properties of undefined (reading 'send')
    at Client.<anonymous> (/home/runner/anonymesssendbot/index.js:44:49)
    at Client.emit (node:events:390:28)
    at Client.emit (node:domain:475:12)
    at MessageCreateAction.handle (/home/runner/anonymesssendbot/node_modules/discord.js/src/client/actions/MessageCreate.js:34:18)
    at Object.module.exports [as MESSAGE_CREATE] (/home/runner/anonymesssendbot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/home/runner/anonymesssendbot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:346:31)
    at WebSocketShard.onPacket (/home/runner/anonymesssendbot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:478:22)
    at WebSocketShard.onMessage (/home/runner/anonymesssendbot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:317:10)
    at WebSocket.onMessage (/home/runner/anonymesssendbot/node_modules/ws/lib/event-target.js:199:18)
repl process died unexpectedly: exit status 1

I'm guessing that db.get() returns a promise which you cannot pass as a value. Try retrieving the value first and then get the channel. Something like

const id = await db.get(args[1]);
const channel = client.channels.cache.get(id);
channel.send(args[2]);

There is also a possibility that the id is stored incorrectly in your server. So have a look at that. You could log the id to check if that is the case.

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