Output a message to any channel

Please tell me how I can display a message in any channel of any server? I mean like:
const channel = client.channels.cache.get("1217194699664291602");
message.channel.send('The Bot is working!')
const channel = client.channels.cache.get("1217194699664291602");
message.channel.send('The Bot is working!')
P.S. Please, attach a code example, I'm just learning)
20 Replies
d.js toolkit
d.js toolkit3mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
chewie 🌈
chewie 🌈3mo ago
you mean every channel? or a specific channel
iFel1x
iFel1x3mo ago
I mean the channel that I will indicate by ID
d.js docs
d.js docs3mo ago
const channel = client.channels.cache.get("222086648706498562");
const channel = guild.channels.cache.find(channel => channel.name === "general");
const channel = client.channels.cache.get("222086648706498562");
const channel = guild.channels.cache.find(channel => channel.name === "general");
- Caches in discord.js are Collections which extend the native Map structure. - learn more
chewie 🌈
chewie 🌈3mo ago
I mean you already have that in your code you just gotta use it
iFel1x
iFel1x3mo ago
I have an error
No description
chewie 🌈
chewie 🌈3mo ago
yes, why are you using message there just channel.send()
iFel1x
iFel1x3mo ago
No description
ahmood
ahmood3mo ago
You're using an invalid channel Id then
chewie 🌈
chewie 🌈3mo ago
or client aint ready
iFel1x
iFel1x3mo ago
1217094699664281602
ahmood
ahmood3mo ago
What am I supposed to do with that
iFel1x
iFel1x3mo ago
No description
iFel1x
iFel1x3mo ago
channel id is valid. I just change it at the 1st post
ahmood
ahmood3mo ago
Do you have the Guilds intent?
iFel1x
iFel1x3mo ago
my script:
const { Client, Events, GatewayIntentBits, Message } = require('discord.js');
const token = process.env.DS_TOKEN;
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
client.once(Events.ClientReady, c => {
console.log(`DiscordBOT ready! Logged in as ${c.user.tag}`);
});
client.login(process.env.DS_TOKEN);
const channel = client.channels.cache.get("1217094699664281602");
channel.send('The Bot is working!');
const { Client, Events, GatewayIntentBits, Message } = require('discord.js');
const token = process.env.DS_TOKEN;
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
client.once(Events.ClientReady, c => {
console.log(`DiscordBOT ready! Logged in as ${c.user.tag}`);
});
client.login(process.env.DS_TOKEN);
const channel = client.channels.cache.get("1217094699664281602");
channel.send('The Bot is working!');
ahmood
ahmood3mo ago
You're trying to access the channel before the bot is ready, put the channel send stuff inside the ready listener
iFel1x
iFel1x3mo ago
Can you give an example of how to do this?
ahmood
ahmood3mo ago
It's really not hard, put this code inside the ready listener, which you're already listening for
iFel1x
iFel1x3mo ago
It really works. Thanks a lot!