javascript Js discord bot为什么不工作前缀?如何决定discord.js bot的问题?

xsuvu9jc  于 5个月前  发布在  Java
关注(0)|答案(1)|浏览(61)
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);

字符串
代码看起来很简单,它可以在线运行,“/”命令可以工作,但是所有与前缀命令相关的命令都不工作。我不知道原因是什么,我已经尝试了很多代码选项,但是没有任何帮助。错误在哪里?

bhmjp9jg

bhmjp9jg1#

如果您希望bot响应前缀,则必须在示例化新客户端时将MessageContent Intent添加到Intent中。示例:

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

字符串
不要忘记在developer portal中也启用此选项(Here是一个小教程。

相关问题