繁体   English   中英

如何让我的不和谐机器人自动回复消息?

[英]How do I make my discord bot auto respond to a message?

我想要做的是设置一个自动响应不和谐机器人,如果有人说“我赢了”,它就会回复。 我无法弄清楚为什么它没有出现任何不和谐的反应。 我附上了一张它正在做什么的图片。

const Discord = require('discord.js');
const config = require('./config.json');
const client = new Discord.Client();

client.login(config.BOT_TOKEN);

const prefix = '+';

client.on('message', function(message) {
 if (message.author.bot) return;
 if (!message.content.startsWith(prefix)) return;
 const commandBody = message.content.slice(prefix.length);
 const args = commandBody.split(' ');
 const command = args.shift().toLowerCase();
 if (command === 'ping') {
  const timeTaken = Date.now() - message.createdTimestamp;
  message.reply(`Pong! This message had a latency of ${timeTaken}ms.`);
 }
 if (command === 'cat') {
  message.reply(`https://media.giphy.com/media/ExN8bEghwc8Ced5Yss/giphy.gif`);
 }
 if (command === 'cat2') {
  message.reply(`image`);
 }
 if (command === 'trashcan') {
  message.reply(
   `https://www.trashcanswarehouse.com/assets/images/product-photos/witt/wcd24cl.jpg`
  );
 }
 if (command === 'trashcan2') {
  message.reply(
   `https://marinedebris.noaa.gov/sites/default/files/styles/max-width600/public/IMG_1187_0.JPG?itok=iFHb98S3`
  );
 }

 if (command === 'rock') {
  var rockpaperscissors = ['Rock', 'Paper', 'Scissors'];
  var rps = Math.floor(Math.random() * rockpaperscissors.length);
  message.channel.send(rockpaperscissors[rps]);
 }
 if (command === 'paper') {
  var rockpaperscissors = ['Rock', 'Paper', 'Scissors'];
  var rps = Math.floor(Math.random() * rockpaperscissors.length);
  message.channel.send(rockpaperscissors[rps]);
 }
 if (command === 'scissors') {
  var rockpaperscissors = ['Rock', 'Paper', 'Scissors'];
  var rps = Math.floor(Math.random() * rockpaperscissors.length);
  message.channel.send(rockpaperscissors[rps]);
 }

 client.on('message', (message) => {
  // If message is i win
  if (message.content === 'i win') {
   // Send no you dont back
   message.channel.send('no you dont');
  }
 });
});

这就是我目前拥有的代码。

client.on('message', (message) => {
 // If message is i win
 if (message.content === 'i win') {
  // Send no you dont back
  message.channel.send('no you dont');
 }
});

是不会全部用完的代码。 有人说我赢了,它说不,你不知道。 (据说)

在第二次阅读时,在我看来,您用于评估消息的第二个客户端挂钩在第一个中:

//...
client.on('message', function(message) {
 if (message.author.bot) return;
 if (!message.content.startsWith(prefix)) return;
 const commandBody = message.content.slice(prefix.length);
 const args = commandBody.split(' ');
 const command = args.shift().toLowerCase();
 if (command === 'ping') {
  const timeTaken = Date.now() - message.createdTimestamp;
  message.reply(`Pong! This message had a latency of ${timeTaken}ms.`);
 }
 //...
 client.on('message', (message) => {
  // If message is i win
  if (message.content === 'i win') {
   // Send no you dont back
   message.channel.send('no you dont');
  }
 });
});

不应该这样(考虑:如何在事件处理函数期间注册事件处理函数?)。 您有几个选择:
第一个,最简单的就是删除第二个 client.on 事件并使用 if/else 在评估命令或消息之间切换。

client.on("message", function (message) {
  if (message.author.bot) return;
  if (message.content.startsWith(prefix)) {
    let command = // parse prefix out, normalize, etc
    if (command === "ping") {
      // ...
    }
  } else {
    if (message.content === "i win") {
      // ...
    }
  }
});

但是,如果您有很多命令,那么将命令编写为单独的文件可能是值得的: https : //discordjs.guide/command-handling/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM