Search code examples
javascriptnode.jsdiscorddiscord.js

Js discord bot why dont work prefix?How to decide a problem with discord.js bot?


const {
  Client,
  GatewayIntentBits,
  MessageEmbed,
  Partials,
  SlashCommandBuilder,
  ButtonBuilder,
  ButtonStyle,
} = require("discord.js");
const prefix = '!';



const client = new Client({
  intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages,],
  partials: [Partials.Channel, Partials.Message],
});


client.on("message", msg => {
  if (msg.author.bot || !msg.content.startsWith(prefix)) return;
  const args = msg.content.slice(prefix.length).split(/ +/);
  const cmd = args.shift().toLowerCase();

  // Обработка команд
  if (cmd === "ping") {
    const pingTime = Date.now() - msg.createdTimestamp;
    msg.reply(`Pong! Latency is ${pingTime}ms`);
  }
});



client.login(config.BOT_TOKEN);

The code seems to be quite simple, it runs online, the '/' commands work, but everything related to command with prefix does not work. I don’t know what the reason is, I’ve already tried a lot of code options, but nothing helps. Where is the mistake?


Solution

  • If you want the bot to respond to a prefix you'll have to add the MessageContent intent to your intents when instantiating a new client. Example:

    const client = new Client({
      intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
      partials: [Partials.Channel, Partials.Message],
    });
    

    Don't forget to also enable this option in the developer portal (Here is a small tutorial.)