简体   繁体   English

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

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

What I am trying to do is set up an autoresponse discord bot that replies if someone says "I win".我想要做的是设置一个自动响应不和谐机器人,如果有人说“我赢了”,它就会回复。 I cannot figure out why it is not showing up with any responses in discord.我无法弄清楚为什么它没有出现任何不和谐的反应。 I have attached an image of what it is doing.我附上了一张它正在做什么的图片。

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

That is the code that I currently have.这就是我目前拥有的代码。

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

Is the code that will not run out of all of it.是不会全部用完的代码。 It says no you don't after someone says I win.有人说我赢了,它说不,你不知道。 (supposedly) (据说)

On second read, it appears to me that your second client hook for evaluating messages is within the first:在第二次阅读时,在我看来,您用于评估消息的第二个客户端挂钩在第一个中:

//...
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');
  }
 });
});

It should not be this way (consider: how do you register an event handling function during an event handling function?).不应该这样(考虑:如何在事件处理函数期间注册事件处理函数?)。 You have a few options:您有几个选择:
The first, simplest would just be to remove the second client.on event and use if/else to switch between evaluating for commands or messages.第一个,最简单的就是删除第二个 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") {
      // ...
    }
  }
});

However, if you are going to have a lot of commands, it might be worth it to look into writing commands as individual files: https://discordjs.guide/command-handling/但是,如果您有很多命令,那么将命令编写为单独的文件可能是值得的: https : //discordjs.guide/command-handling/

暂无
暂无

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

相关问题 如何让我的 Discord 机器人自动更改状态 - How do I make my Discord bot change status auto 当我们在Discord机器人上触发错误时,如何向用户回复消息? - How do I respond with a message to a user when they trigger an error on my Discord bot? 如何让我的 discord 机器人删除我的消息并对上面的消息作出反应? - How do I make my discord bot delete my message and react to the message above? 如何让我的不和谐机器人复制并发送每个用户写的消息? - How do I make my discord bot copy and message every user written message? 当特定用户玩特定游戏时,如何让我的不和谐机器人发送消息? - How do I make my discord bot send a message when a specific user plays a specific game? 如何让我的 discord 机器人通过回复池回复特定消息? - How do I make my discord bot reply to a specific message with reply from a pool of replies? 如何让我的 Discord.JS 机器人对其自己的直接消息做出反应? - How do I make my Discord.JS bot react to it's own Direct Message? 如何让 discord 机器人在加入时发送消息 - How do I make a discord bot send a message upon joining Discord.js机器人程序需要帮助-如何使我的机器人每6小时自动执行一项任务而不发送命令? - Discord.js bot help needed - how can I make my bot auto do a task every 6 hours without sending it a command? 如何让我的 Discord 机器人发送欢迎消息? - How do I get my Discord bot to send a welcome message?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM