简体   繁体   中英

How to detect message content Discord.js?

I am working on a code for a Discord bot for my server, and I'm currently stumped on a magic 8 ball feature, go figure. I'm trying to get it to check if there is a message after the 8 ball command, and if not, send a message to the same channel telling the user to supply it a message to answer.

This is my code:

if (commandName === '8ball') {
          message.author.get(args[0]);
      const message = args.join(' ').slice(0);
      if (message.length < 10) message.channel.reply; 'You need to give me a question to anser, bud.';

      const messages = [
         '8ball messages here'  
      ];
      const pEmbed = new Discord.MessageEmbed()
          .setTimestamp('')
          .setColor(`${bedcolor}`)
          .randomMessage = messages[Math.floor(Math.random() * messages.length)];
      return message.reply(pEmbed);

It returns an error saying it cannot access message before initialization, and is also doesn't embed despite the code being there. I'm new to Javascript and I'm kind of attempting to splice random code together to make it work, so I'm probably doing something very wrong here. I'd appreciate anyone's advice on how to at least make this code function and to learn more about JS so I don't have to 'splice' random code.

Considering that you are on Discord V13, you would need to do message.channel.send({embeds: [pEmbed]}) for it to function, instead of message.channel.send(pEmbed) its a new feature to the Version 13 addition.

You would also need to do
randomMessage = messages[Math.floor(Math.random() * messages.length)]; and then state .setDescription(`Answer: ${randomMessage}`) as an example

Happy coding. Let me know if you run into issues.

Try this:

function someFunctionName() {
    var messages = ['...','...']
    return messages[Math.floor(Math.random()*rand.length)];
}

const pEmbed = new Discord.MessageEmbed()
          .setColor(`${bedcolor}`)
          .setDescription(someFunctionName())
      return message.reply(pEmbed);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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