如何使Discord机器人阅读嵌入

时间:2018-11-08 04:11:59

标签: javascript discord discord.js

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

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

但是如果它是嵌入的,它将不会读取该消息。请帮助我进行更改,以在嵌入消息中寻找关键字并引起漫游器的响应。

2 个答案:

答案 0 :(得分:0)

应该是这样,也检查邮件中的所有嵌入内容

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

答案 1 :(得分:0)

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

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