简体   繁体   English

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

[英]How to send a message in a specific channel with id

I used replit database to save channel id我使用 replit 数据库来保存频道 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.我猜db.get()返回一个不能作为值传递的承诺。 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.也有可能 id 被错误地存储在您的服务器中。 So have a look at that.所以看看那个。 You could log the id to check if that is the case.您可以记录 id 以检查是否是这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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