简体   繁体   English

如何检测短信内容Discord.js?

[英]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.我正在为我的服务器编写一个 Discord 机器人的代码,我目前对一个神奇的 8 球功能 go 数字感到困惑。 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.我试图让它检查 8 球命令后是否有消息,如果没有,则向同一频道发送一条消息,告诉用户向它提供一条消息来回答。

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.我是 Javascript 的新手,我试图将随机代码拼接在一起以使其工作,所以我可能在这里做错了。 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.我很感激任何人关于如何至少制作此代码 function 并了解有关 JS 的更多信息的建议,这样我就不必“拼接”随机代码。

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.考虑到您使用的是 Discord V13,您需要执行message.channel.send({embeds: [pEmbed]})到 function,而不是message.channel.send(pEmbed)它是版本 13 的新功能添加。

You would also need to do你还需要做
randomMessage = messages[Math.floor(Math.random() * messages.length)]; and then state .setDescription(`Answer: ${randomMessage}`) as an example然后以 state .setDescription(`Answer: ${randomMessage}`)为例

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

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

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