如何在具有id的特定通道中发送消息



我使用replit数据库保存通道id
这是我的代码:
client.channels.cache.get(db.get(args[1])).send(args[2]);
,我得到这个错误:

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

我猜db.get()返回一个你不能作为值传递的承诺。尝试先检索值,然后获取通道。就像

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

也有可能id在服务器中存储不正确。看看这个。您可以记录id以检查是否存在这种情况。

最新更新