There are a few errors with this.
Before I get into the more complicated stuff, please know that the message
event doesn't work -- use messageCreate
.
The second smaller thing that I need to point out is that you don't have the MessageContent
Intent enabled; that needs to be enabled for you to detect the text/attachments of a message that isn't a Direct Message to the bot, and doesn't ping/mention the bot.
Now, for the bigger stuff.
client.channels.get()
isn't a function, and that's because of the Channel Cache.
I'll try my best to break down what a Cache is, but I'm not too good with this, so someone can correct me below.
A cache is similar to a pocket; when someone (being discord.js) looks inside of your pocket (the cache), that someone can only see the items inside of the pocket. It can't detect what isn't there. That's what the cache is. It's the inside of the pocket, and holds the things that you want discord.js to see.
If your item isn't in the cache, discord.js won't use it. But, you can get discord.js to look for that item elsewhere, and put it into that pocket for later.
This is the .fetch()
method. It puts something that isn't in the cache, well, in the cache.
An example of using it:
const channel = await client.channels.fetch("CHANNEL_ID");
Or, what I'd call the more "traditional" way of doing it:
client.channels.fetch("CHANNEL_ID").then(channel => {
});
Now, after fetching your channel, you can call it like this:
client.channels.cache.get("CHANNEL_ID");
Another fun thing about the .fetch()
method is that you can use it to shove everything in the cache, though it isn't necessary in most cases.
client.channels.fetch();
That will fetch every channel that the bot can see.
client.guilds.fetch();
That will fetch every guild the bot is in.
guild.members.fetch();
That will fetch every guild member of a guild.
And so on.
After Everything
After applying everything that I've said previously, you should wind up with code that looks like this:
const { Client, GatewayIntentBits } = require("discord.js");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent // Make sure this is enabled in discord.com/developers/applications!
]
});
client.on("messageCreate", async(message) => {
if(message.content.toLowerCase() === "ping") {
// I added the .toLowerCase() method so that you can do PING and it works.
client.channels.fetch("1028177045773094915").then(channel => {
channel.send({ content: "pong!" });
});
}
});
client.login("Your Token");