【问题标题】:Discord.JS bot does not respondDiscord.JS 机器人没有响应
【发布时间】:2021-01-06 04:52:56
【问题描述】:

我的 Discrod.JS 机器人在线但没有响应。这是代码-

// require the discord.js module
const Discord = require('discord.js');

// create a new Discord client
const client = new Discord.Client();

// when the client is ready, run this code
// this event will only trigger one time after logging in
client.once('ready', () => {
    console.log('Ready!');
});

module.exports = function(controller) {
  controller.hears(
    "!appreciates",
    ["direct_mention", "mention"],
    (bot, message) => {
      let response;
      let sender = message.user;
      let recipient = message.mentions.users
        .filter(user => user.bot === false)
        .last();
      let responses = [
        `Hey, ${sender}, I appreciate you!  :heart_eyes:`,
        `Hey, ${sender}, I appreciate your enthusiasm!  :muscle:`,
        `Hey, ${sender}, I appreciate your commands!  :wink:`,
        `Hey, ${sender}, I appreciate your particaption! :grinning:`,
        `Hey, ${sender}, I appreciate all your works! :blush:`,
        `Hey, ${sender}, I appreciate everything you do. :star_struck:`
      ];

      response = responses[Math.floor(Math.random() * responses.length)];

      bot.reply(message, response);
    }
  );
};
client.login('app token');

控制台中没有错误。控制台中的唯一消息是“Ready!”。我还检查了是否安装了 Discord.JS NPM 模块。

【问题讨论】:

  • 你调用过这个函数吗?
  • 使用 call() 方法?不,我没有。顺便说一下,你看到的代码是完整的。
  • 您是否有理由不只使用消息事件处理程序? (例如client.on('message', message => { ... });
  • 不,没有特殊原因。我只是在重写一个旧机器人(它仍在工作),它有这段代码。
  • 好吧,目前,您的代码正在导出一个函数,该函数接受一个能够侦听消息的“控制器”。要么添加该功能,要么切换到使用消息事件处理程序(如我上面的评论所示)。

标签: javascript node.js discord.js


【解决方案1】:

您需要触发一个事件。在这种情况下,您希望它回复。所以你需要消息事件。

bot.on('message', message => {
    //

    //This is where your function would go
    //

});

【讨论】:

    【解决方案2】:

    为了让你的机器人能够在收到消息时做一些事情,你需要client.on('message',message =>{//your code}

    示例:

    client.on('message',message =>{
    
    //execute a command
    message.channel.send("hello world");
    });
    

    【讨论】:

      猜你喜欢
      • 2020-08-09
      • 2020-07-03
      • 2021-11-21
      • 2021-05-25
      • 2021-08-18
      • 2021-11-03
      • 2021-11-10
      • 2021-09-04
      • 2021-08-30
      相关资源
      最近更新 更多