4

I have been trying to set up a discord bot and by following the docs, I have been able to set up a slash command but have not been able to get the bot to reply to messages on the server.

Here is the code I used to set up slash command from docs.

const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');

const commands = [{
  name: 'ping',
  description: 'Replies with Pong!'
}]; 

const rest = new REST({ version: '9' }).setToken('token');

(async () => {
  try {
    console.log('Started refreshing application (/) commands.');

    await rest.put(
      Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID),
      { body: commands },
    );

    console.log('Successfully reloaded application (/) commands.');
  } catch (error) {
    console.error(error);
  }
})();

After this I set up the bot with this code:

const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });


client.once('ready', () => {
    console.log('Ready!');
    console.log(`Logged in as ${client.user.tag}!`);
});

client.on('interactionCreate', async interaction => {
    // console.log(interaction)
    if (!interaction.isCommand()) return;

    if (interaction.commandName === 'ping') {
        await interaction.reply('Pong!');
        // await interaction.reply(client.user + '');
    }
});

client.login('BOT_TOKEN');

Now I am able to get a response of Pong! when I say /ping.

reply image

But after I added the following code from this link I didn't get any response from the bot.

client.on('message', msg => {
  if (msg.isMentioned(client.user)) {
    msg.reply('pong');
  }
});

I want the bot to reply to messages not just slash commands. Can someone help with this. Thanks!!

Skulaurun Mrusal
  • 2,792
  • 1
  • 15
  • 29
  • 2
    Does this answer your question? "[message event listener not working properly](https://stackoverflow.com/q/64394000/90527)", "[Having trouble sending a message to a channel with Discord.js](https://stackoverflow.com/q/68795635/90527)". – outis Oct 30 '21 at 22:18

1 Answers1

3

First of all you are missing GUILD_MESSAGES intent to receive messageCreate event.

const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });

Secondly the message event is deprecated, use messageCreate instead.

client.on("messageCreate", (message) => {
    if (message.mentions.has(client.user)) {
        message.reply("Pong!");
    }
});

At last, Message.isMentioned() is no logner a function, it comes from discord.js v11. Use MessageMentions.has() to check if a user is mentioned in the message.

enter image description here

Tested using discord.js ^13.0.1.

Skulaurun Mrusal
  • 2,792
  • 1
  • 15
  • 29
  • Thanks a lot! This worked like a charm. I got confused and mixed up the versions. I am also trying to fetch the list of all users, and i believe we need GuildMemberManager for it and for manager we need cache, how do we generate cache? – kushagra agarwal Aug 16 '21 at 23:19
  • 1
    I am glad that it helped. I think you should open a new question about that topic, since it concerns a different problem. I have once written a post about fetching all user ids from all guilds, might be helpful, link [here](https://stackoverflow.com/a/68729412/9776840). – Skulaurun Mrusal Aug 16 '21 at 23:25
  • Thanks!! that link helped. Although I was only able to get the list of people who had recently messaged and not the whole thing. any idea on how to do that? Can you redirect me to some resource where I can learn about the usage. Like I am able to find classes and functions in the docs, but I am unable to implement them, maybe coz i am unaware of it's prerequisites like creating a MemberManager etc. – kushagra agarwal Aug 17 '21 at 09:24
  • You are not supposed to create `MemberManager`, the manager is already there, you just need to access its instance. Look at [`Guild.members`](https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=members). Then use [`.fetch()`](https://discord.js.org/#/docs/main/stable/class/GuildMemberManager?scrollTo=fetch) on the manager to fetch all the members. – Skulaurun Mrusal Aug 17 '21 at 11:21
  • Alright, thanks man! this is was really helpful, I'll try to get it done...Thanks again – kushagra agarwal Aug 17 '21 at 17:58