如何将消息发送到特定频道 [英] How to send a message to a specific channel

查看:105
本文介绍了如何将消息发送到特定频道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我的Discord机器人(位于多台服务器中)将消息发送到特定频道.我希望该机器人从一个服务器上接收消息,然后在特定频道中将消息发送到我的个人服务器,但是我无法让它找到"该频道. API是否已更改?我也尝试过npm install discord.js进行更新.

I'm trying to send a message to a specific channel with my Discord bot, which is in several servers. I want the bot to pick up on a message from one server and send a message to my personal server, in a specific channel, but I can't get it to 'find' the channel. Has the API changed or something? I tried npm install discord.js to update too.

代码:

if (message.author.id == 'XXXXX' && !mess.includes("Dank") && message.channel.id != 'XXXXX') {
  bot.channels.get('XXXXX').send('memes');
}

我尝试了一些尝试,但没有任何效果.

I tried a few things but none worked.

TypeError: Cannot read property 'send' of undefined
    at decideIfMention (C:\Users\XXXX\Desktop\Coding Crud\Discord Bot 2\bot.js:80:45)
    at Client.bot.on (C:\Users\XXXX\Desktop\Coding Crud\Discord Bot 2\bot.js:68:3)
    at emitOne (events.js:116:13)
    at Client.emit (events.js:211:7)
    at MessageCreateHandler.handle (C:\Users\XXXX\Desktop\Coding Crud\Discord Bot 2\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:\Users\XXXX\Desktop\Coding Crud\Discord Bot 2\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
    at WebSocketConnection.onPacket (C:\Users\XXXX\Desktop\Coding Crud\Discord Bot 2\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (C:\Users\XXXX\Desktop\Coding Crud\Discord Bot 2\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (C:\Users\XXXX\Desktop\Coding Crud\Discord Bot 2\node_modules\ws\lib\event-target.js:120:16)
    at emitOne (events.js:116:13)

推荐答案

假设您具有 client (这是 Client.find :

Assuming you have the client (which would be an instance of Discord.Client) try finding the desired channel by using Client.find:

const channel = client.channels.cache.find(channel => channel.name === channelName)
channel.send(message)

如果您没有直接的client,但是具有 message 实例,您始终可以在 Message.client 属性.

If you don't have the client directly but have a message instance, you could always access it from within the Message.client property.

这篇关于如何将消息发送到特定频道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆