0

So my code is this:

const { GatewayIntentBits } = require('discord.js');
const Discord = require('discord.js');

const token = 'My Token';

const client = new Discord.Client({
    intents:
    [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages
    ]
});

client.on('ready', () => 
{
    console.log("Bot is on");
});

client.on('message', (message) =>
{
    const channel = client.channels.get("1028177045773094915")
    if (message.content === 'ping') 
    {
        channel.send("pong")
    }
});

// This is always the end
client.login(token);

I don't know why it is not working, it's in online but when I type anything in channel it doesnt do anything.

  • Long time that I‘ve used Discrod.js, but I think you need to fetch the channels first before using .get. You‘d want to .fetch() first and then get(). Initially I think only guilds are cached. – GabeRAMturn Oct 08 '22 at 20:46
  • 1
    Have you enabled the message content intent through your developer portal? – Elitezen Oct 08 '22 at 21:11
  • You need to add the `GatewayIntentBits.MessageContent` intent as well in the client to access the message content – Caladan Oct 09 '22 at 02:55
  • You need to use `messageCreate` Event if using discord.js@13.x.x – Pooyan Oct 09 '22 at 17:29

1 Answers1

0

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");
Connor
  • 58
  • 3