【问题标题】:How to get discord bot to read embed如何让不和谐机器人阅读嵌入
【发布时间】:2019-04-11 14:10:28
【问题描述】:

我了解不和谐机器人如何阅读常规用户输入的消息并使用

进行响应
if(message.content.toLowerCase().includes('cyber'))
    message.channel.send("Key Word Detected ");

但如果它是嵌入的,它不会读取消息。请帮我更改它以在嵌入消息中查找关键字并引起机器人的响应。

【问题讨论】:

  • 听起来您要求 SO 为您编写代码。您能否展示您在这些行中尝试更改的内容,并解释为什么它不起作用?也许链接您正在尝试处理的文档部分
  • 我尝试将代码更改为 if(embed.Message.content.toLowerCase().includes('cyber')) message.channel.send("Key Word Detected "); if(embed.content.toLowerCase().includes('cyber')) message.channel.send("Key Word Detected "); 似乎没有任何效果,我查找的所有内容似乎都无法解决我的问题
  • 您确定 embed.Message 存在吗?对我来说,在查看this guide 之后,似乎嵌入是一个字段列表,所以我会做类似fields = message.embed.fields; len = fields.length; for var i in range(len){ if fields[i].value.contains('cyber')...

标签: javascript discord discord.js


【解决方案1】:

应该是这样,检查消息中的所有嵌入

if(message.content.toLowerCase().includes('cyber'))
    message.channel.send("Key Word Detected ");
else {
    for(var i = 0; i < message.embeds.length; i++) {
        if(message.embeds[i].title.includes("cyber") || message.embeds[i].title.includes("cyber")) {
            message.channel.send("Detected");
            break;
    }
}

【讨论】:

    【解决方案2】:

    MessageEmbed 中的文本可以是 authordescriptionfootermessage.contenttitle。它们也可以在每个文件中,因此可能需要检查所有这些内容。
    这是你可以使用的一个小函数(我知道这看起来很乱,但这只是因为有很多逻辑运算符):

    /*
          message {Discord.Message}: the message you want to search in
          target {string}: the string you're looking for
          {
            caseSensitive {boolean}: whether you want the search to be case case-sensitive
            author {boolean}: whether you want to search in the author's name
            description {boolean}: whether you want to search in the description
            footer {boolean}: whether you want to search in the footer
            title {boolean}: whether you want to search in the title
            fields {boolean}: whether you want to search in the fields
          }
         */
    function findInMessage(message, target, {
      caseSensitive = false,
      author = false,
      description = true,
      footer = true,
      title = true,
      fields = true
    }) {
      if (!target || !message) return null;
      let str = caseSensitive ? target : target.toLowerCase();
    
      if ((caseSensitive && message.content.includes(str)) ||
        (!caseSensitive && message.content.toLowerCase().includes(str))) return true;
    
      for (let embed of message.embeds) {
        if ((caseSensitive && (
            (author && embed.author.includes(str)) ||
            (description && embed.description.includes(str)) ||
            (footer && embed.footer.includes(str)) ||
            (title && embed.title.includes(str)))) ||
          (!caseSensitive && (
            (author && embed.author.toLowerCase().includes(str)) ||
            (description && embed.description.toLowerCase().includes(str)) ||
            (footer && embed.footer.toLowerCase().includes(str)) ||
            (title && embed.title.toLowerCase().includes(str))))
        ) return true;
    
        if (fields)
          for (let field of embed.fields) {
            if ((caseSensitive && [field.name, field.value].includes(str)) ||
              (!caseSensitive && [field.name.toLowerCase(), field.value.toLowerCase()].includes(str))) return true;
          }
      }
    
      return false;
    }
    

    函数在找到您输入的单词时返回 true,在未找到时返回 false,在缺少非可选参数之一时返回 null
    你可以这样使用它:

    if (findInMessage(message, 'cyber')) message.channel.send("Key word detected.");
    

    顶部有一些说明,希望对您有所帮助;)

    【讨论】:

      猜你喜欢
      • 2020-12-06
      • 1970-01-01
      • 2021-01-21
      • 2021-12-21
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-25
      相关资源
      最近更新 更多