最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sending message to specific channel in discord.js - Stack Overflow

programmeradmin6浏览0评论

I want to send a message to a specific channel but it doesn't work, I've tried client.guilds.cache.get("<id>").send("<msg>") but I get the error ".send is not a function". What am I missing here?

I want to send a message to a specific channel but it doesn't work, I've tried client.guilds.cache.get("<id>").send("<msg>") but I get the error ".send is not a function". What am I missing here?

Share Improve this question asked Mar 10, 2022 at 20:21 Yuval52Yuval52 111 gold badge1 silver badge3 bronze badges 1
  • What am I missing here? Getting the channel. client.guilds.cache.get("<id>") returns a guild, not a channel. – Zsolt Meszaros Commented Mar 10, 2022 at 20:53
Add a ment  | 

2 Answers 2

Reset to default 4

There are three ways of sending message to a specific channel.

1. Using a fetch method

const channel = await <client>.channels.fetch('channelID')
channel.send({content: "Example Message"})

2. Using a get method

const channel = await <client>.channels.cache.get('channelID')
channel.send({content: "Example Message"})

3. Using a find method

a.

const channel = await <client>.channels.cache.find(channel => channel.id === "channelID")
channel.send({content: "Example Message"})

b.

const channel = await <client>.channels.cache.find(channel => channel.name === "channelName")
channel.send({content: "Example Message})

Your current code line is getting the GUILD_ID not the CHANNEL_ID.

You are trying to get from the cache a Guild not a Channel, or more specifically a TextChannel, that's why what you try is not working, you should try to change the code to something like this and it should work.

const channel = client.channels.cache.get('Channel Id');
channel.send({ content: 'This is a message' });

Also, as something extra, I would remend using the fetch method for these things, since it checks the bot's cache and in the case of not finding the requested object it sends a request to the api, but it depends on the final use if you need it or not.

const channel = await client.channels.fetch('Channel Id');
channel.send({ content: 'This is a message' });
发布评论

评论列表(0)

  1. 暂无评论