I need the code to send a message to a channel I have looked on stack overflow but there all too old and through up a error
Asked
Active
Viewed 8,181 times
-1
-
please share your code to show what you have tried. – e-frank May 20 '21 at 12:31
4 Answers
3
There is a guide for this on the discord.js guide.
const channel = <client>.channels.cache.get('<id>');
channel.send('<content>');
An improved version would be:
<client>.channels.fetch('<id>').then(channel => channel.send('<content>'))

mmoomocow
- 1,173
- 7
- 27
-
TypeError: Cannot read property 'send' of undefined at loginconsole (C:\Users\theen\Desktop\c3 SigServer\sigserv.js:30:9) at Object.
(C:\Users\theen\Desktop\c3 SigServer\sigserv.js:72:1) at Module._compile (internal/modules/cjs/loader.js:1137:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10) at Module.load (internal/modules/cjs/loader.js:985:32) at Function.Module._load (internal/modules/cjs/loader.js:878:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) – DrMeepso May 20 '21 at 04:51 -
@DrMeepso I have added an improved one-line version. The error you were getting was most likely caused by discord.js not getting the channel fast enough, but check that the ID is correct – mmoomocow May 20 '21 at 06:40
-
@mmoomocow that's not it. He's doing them out the events. Also channels are always cached so you don't need to fetch them. – Radnerus May 20 '21 at 06:48
2
At first you need to get the channel ID or Channel Name to do that
/* You handle in command and have message */
// With Channel Name
const ChannelWantSend = message.guild.channels.cache.find(channel => channel.name === 'Channel Name');
// With Channel ID
const ChannelWantSend = message.guild.channels.cache.get(channelId);
ChannelWantSend.send('Your Message');
/* If you start from root of your bot , having client */
// With Channel Name
const ChannelWantSend = client.channels.cache.find(channel => channel.name === 'Channel Name');
// With Channel ID
const ChannelWantSend = client.channels.cache.get(channelId);
ChannelWantSend.send('Your Message');
// In both case If ChannelWantSend is undefined where is a small chance that discord.js not caching channel so you need to fetch it
const ChannelWantSend = client.channels.fetch(channelId);

HellCatVN
- 866
- 1
- 6
- 11
-
ReferenceError: message is not defined at Object.
(C:\Users\theen\Desktop\c3 SigServer\sigserv.js:26:25) ←[90m at Module._compile (internal/modules/cjs/loader.js:1137:30)←[39m ←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)←[39m ←[90m at Module.load (internal/modules/cjs/loader.js:985:32)←[39m ←[90m at Function.Module._load (internal/modules/cjs/loader.js:878:14)←[39m ←[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)←[39m ←[90m at internal/main/run_main_module.js:17:47←[39m – DrMeepso May 20 '21 at 04:52 -
Where are you using that block of code. In a command or root of your bot – HellCatVN May 20 '21 at 05:03
-
i hope this helps https://cdn.discordapp.com/attachments/524471091415089162/844816266107813888/unknown.png – DrMeepso May 20 '21 at 05:58
-
-
https://cdn.discordapp.com/attachments/524471091415089162/844821145576275998/unknown.png – DrMeepso May 20 '21 at 06:17
-
@DrMeepso why is every thing outside the message event? Do them inside the message event. And ID is a snowflake, meaning it has to be a String. Take a deep look at the [guide](https://discordjs.guide/). – Radnerus May 20 '21 at 06:40
-
its ment to print out console out put form a Node JS server to a disocrd bot so i can look at things remotly – DrMeepso May 20 '21 at 07:01
-
I updated my solution. And still not seeing where you call loginconsole function – HellCatVN May 20 '21 at 07:12
-
here's where I'm calling it https://cdn.discordapp.com/attachments/524471091415089162/844836820205699082/unknown.png – DrMeepso May 20 '21 at 07:20
0
Discord.js sending a message to a specific channel
Not sure if you have tested out this code yet, but it looks like this may answer your question?
I haven't tested this, but the thread I linked seems to have tested it as of June 2020!

Real.Cryptc
- 73
- 8
-
TypeError: client.channels.get is not a function at loginconsole (C:\Users\theen\Desktop\c3 SigServer\sigserv.js:28:17) at Object.
(C:\Users\theen\Desktop\c3 SigServer\sigserv.js:70:1) at Module._compile (internal/modules/cjs/loader.js:1137:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10) at Module.load (internal/modules/cjs/loader.js:985:32) at Function.Module._load (internal/modules/cjs/loader.js:878:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) – DrMeepso May 20 '21 at 04:54 -
To use this function you must first login to the Discord client with 'client.login('INSERT TOKEN HERE')' where the token being used is your bot token – Real.Cryptc May 20 '21 at 15:31
0
Shortly, I send message to specific channel like under.
<client>.channels.cache.get("<channel_id>").send("SEND TEXT");
Under code piece is my own usage.
In my case, I save all of Direct Messages to my own channel.
const Discord = require('discord.js');
const client = new Discord.Client();
function saveDMToAdminChannel(message) {
var textDM = `${message.author.username}#${message.author.discriminator} : ${message.content}`;
client.channels.cache.get("0011223344556677").send(textDM);
// "0011223344556677" is just sample.
}
client.on("message", async message => {
if(message.author.bot) return;
if(message.channel.type == 'dm') {
saveDMToAdminChannel(message);
}
});
In my own channel, DM's are saved like,
00:00 User1#1234 : Please fix bug
07:30 User2#2345 : Please fix bug!!
10:23 User3#3456 : Please fix bug!!!!

Zem
- 464
- 3
- 14