不一致机器人没有回复消息 [英] Discord bot is not replying to messages

查看:16
本文介绍了不一致机器人没有回复消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试设置不一致机器人,通过遵循文档,我已经能够设置斜杠命令,但未能使该机器人答复服务器上的消息。

以下是我用来从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);
  }
})();

之后,我用以下代码设置了机器人:

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');

现在我可以得到Pong的回复了!当我说/ping的时候。

但在我从this link添加了以下代码后,我没有从机器人那里得到任何响应。

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

我希望机器人回复消息,而不仅仅是斜杠命令。有人能帮个忙吗。谢谢!!🙂

推荐答案

首先,您缺少GUILD_MESSAGES接收messageCreate事件的意图。

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

其次,message事件已弃用,请改用messageCreate

client.on("messageCreate", (message) => {
    if (message.mentions.has(client.user)) {
        message.reply("Pong!");
    }
});
最后,Message.isMentioned()不再是一个函数,它来自于discord.js v11。使用MessageMentions.has()检查消息中是否提到用户。

已使用discord.js^13.0.1测试。

这篇关于不一致机器人没有回复消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆