Discord.js sending a message to a specific channel

安稳与你 提交于 2020-11-25 02:16:18

问题


I'm failing to achieve something very simple. I can't send a message to a specific channel. I've browsed trough the documentation and similar threads on stack overflow.

client.channels.get().send()

does NOT work. It is not a function. I also do not see it as a method on the Channel class on the official documentation yet every thread I've found so far has told me to use that.

I managed to have the bot reply to a message by listening for a message and then using message.reply() but I don't want that. I want my bot to say something in a specific channel in client.on('ready')

What am I missing?


回答1:


You didn't provide any code that you already tested so I will give you the code for your ready event that will work!

    client.on('ready', client => {
        client.channels.get('CHANNEL ID').send('Hello here!');
    })

Be careful that your channel id a string.

Let me know if it worked, thank you!

2020 Jun 13 Edit:

A Discord.js update requires the cache member of channels before the get method.

If your modules are legacy the above would still work. My recent solution works changing the send line as follows.

        client.channels.cache.get('CHANNEL ID').send('Hello here!')



回答2:


Here's how I send a message to a specific channel every time a message is deleted. This is helpful if you'd like to send the message without a channel ID.

client.on("messageDelete", (messageDelete) => {
  const channel = messageDelete.guild.channels.find(ch => ch.name === 'channel name here');
  channel.send(`The message : "${messageDelete.content}" by ${messageDelete.author} was deleted. Their ID is ${messageDelete.author.id}`);
}); 

Make sure to define it if you're not using messageDelete.




回答3:


This will work for the newest version of node.js msg.guild.channels.cache.find(i => i.name === 'announcements').send(annEmbed)



来源:https://stackoverflow.com/questions/52258064/discord-js-sending-a-message-to-a-specific-channel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!