Josh Haywood
Josh Haywood

Reputation: 81

Discord bot not sending message

I am creating a Discord bot that sends a message when the user's message contains a certain string.

For example, if the user says 'ping', the bot should reply with 'pong'. However, this is not currently working as intended.

The bot itself is online and in the server I am testing, and I am using the correct login token. The code itself does not produce any errors, but it does not function as expected. I am looking for a solution to this issue.

Heres the code:

Ive removed the token just for this post.

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

// Create a new client
const client = new Client({
  // Set the intents to include guilds and guild messages
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildMembers,
  ]
});

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

client.on('message', message => {
  if (message.content === 'ping') {
    message.reply('Pong!');
  }
});

// Log in to Discord using the bot's token
client.login('REMOVED FOR STACK OVERFLOW POST');

Heres my bots permissions enter image description here

Upvotes: 0

Views: 230

Answers (1)

Josh Haywood
Josh Haywood

Reputation: 81

As Zsolt Meszaros pointed out the client on function should use https://stackoverflow.com/users/6126373/zsolt-meszaros

client.on('messageCreate', message => {
  if (message.content === 'ping') {
    message.reply('Pong!');
  }
});

instead of

client.on('message', message => {
  if (message.content === 'ping') {
    message.reply('Pong!');
  }
});

Upvotes: 1

Related Questions