admin管理员组

文章数量:1389762

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 mandBody = message.content.slice(prefix.length);
 const args = mandBody.split(' ');
 const mand = args.shift().toLowerCase();
 if (mand === 'ping') {
  const timeTaken = Date.now() - message.createdTimestamp;
  message.reply(`Pong! This message had a latency of ${timeTaken}ms.`);
 }
 if (mand === 'cat') {
  message.reply(`.gif`);
 }
 if (mand === 'cat2') {
  message.reply(`image`);
 }
 if (mand === 'trashcan') {
  message.reply(
   `.jpg`
  );
 }
 if (mand === 'trashcan2') {
  message.reply(
   `.JPG?itok=iFHb98S3`
  );
 }

 if (mand === 'rock') {
  var rockpaperscissors = ['Rock', 'Paper', 'Scissors'];
  var rps = Math.floor(Math.random() * rockpaperscissors.length);
  message.channel.send(rockpaperscissors[rps]);
 }
 if (mand === 'paper') {
  var rockpaperscissors = ['Rock', 'Paper', 'Scissors'];
  var rps = Math.floor(Math.random() * rockpaperscissors.length);
  message.channel.send(rockpaperscissors[rps]);
 }
 if (mand === '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)

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 mandBody = message.content.slice(prefix.length);
 const args = mandBody.split(' ');
 const mand = args.shift().toLowerCase();
 if (mand === 'ping') {
  const timeTaken = Date.now() - message.createdTimestamp;
  message.reply(`Pong! This message had a latency of ${timeTaken}ms.`);
 }
 if (mand === 'cat') {
  message.reply(`https://media.giphy./media/ExN8bEghwc8Ced5Yss/giphy.gif`);
 }
 if (mand === 'cat2') {
  message.reply(`image`);
 }
 if (mand === 'trashcan') {
  message.reply(
   `https://www.trashcanswarehouse./assets/images/product-photos/witt/wcd24cl.jpg`
  );
 }
 if (mand === 'trashcan2') {
  message.reply(
   `https://marinedebris.noaa.gov/sites/default/files/styles/max-width600/public/IMG_1187_0.JPG?itok=iFHb98S3`
  );
 }

 if (mand === 'rock') {
  var rockpaperscissors = ['Rock', 'Paper', 'Scissors'];
  var rps = Math.floor(Math.random() * rockpaperscissors.length);
  message.channel.send(rockpaperscissors[rps]);
 }
 if (mand === 'paper') {
  var rockpaperscissors = ['Rock', 'Paper', 'Scissors'];
  var rps = Math.floor(Math.random() * rockpaperscissors.length);
  message.channel.send(rockpaperscissors[rps]);
 }
 if (mand === '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)

Share Improve this question edited Sep 24, 2020 at 11:17 Lioness100 8,4226 gold badges20 silver badges50 bronze badges asked Sep 24, 2020 at 3:49 Ryan LipmanRyan Lipman 11 gold badge1 silver badge2 bronze badges 1
  • 2 Can you elaborate as to why exactly you’re creating an event listener (.on()) for the message event within another handler for the message event (further explained in @Allister’s answer)? You really should double-check and ensure your block closures are exactly where you want them to be. – esqew Commented Sep 24, 2020 at 4:06
Add a ment  | 

1 Answer 1

Reset to default 3

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 mandBody = message.content.slice(prefix.length);
 const args = mandBody.split(' ');
 const mand = args.shift().toLowerCase();
 if (mand === '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 mands or messages.

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

However, if you are going to have a lot of mands, it might be worth it to look into writing mands as individual files: https://discordjs.guide/mand-handling/

本文标签: javascriptHow do I make my discord bot auto respond to a messageStack Overflow