如何让不和谐机器人阅读嵌入

How to get discord bot to read embed(如何让不和谐机器人阅读嵌入)
本文介绍了如何让不和谐机器人阅读嵌入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I understand how a discord bot can read a regular user inputed message and respond using

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

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

But it won't read the message if it is an embed. Please help me change that to look for the keyword in a embed message and elicit a response from the bot.

推荐答案

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

The text in a MessageEmbed can be in author, description, footer, message.content and title. They can also be inside every filed, so might want to check for all that stuff.
Here's a little function you could use (I know it seems a mess but it's just because there are a lot of logical operators):

/*
      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缺少非可选参数.
你可以这样使用它:

The functions returns true when finds the word you put in, false when it doesn't find it and null when one of the non-optional arguments is missing.
You can use it like this:

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

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

There are some instructions at the top, hope this helps ;)

这篇关于如何让不和谐机器人阅读嵌入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 频道中的消息?)
how to make my bot mention the person who gave that bot command(如何让我的机器人提及发出该机器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修复必须使用导入来加载 ES 模块 discord.js)
How to list all members from a specific server?(如何列出来自特定服务器的所有成员?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修复“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服务器时的欢迎消息)