Как заставить разногласия бота читать встраивать - PullRequest
0 голосов
/ 08 ноября 2018

Я понимаю, как бот-дискорд может читать обычное пользовательское сообщение и отвечать, используя

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

Но оно не будет читать сообщение, если оно вставлено. Пожалуйста, помогите мне изменить это, чтобы найти ключевое слово в сообщении для вставки и получить ответ от бота.

Ответы [ 2 ]

0 голосов
/ 08 ноября 2018

Текст в MessageEmbed может быть в author, description, footer, message.content и title. Они также могут быть внутри каждого поля, поэтому, возможно, захотите проверить все это.
Вот небольшая функция, которую вы могли бы использовать (я знаю, что это кажется беспорядком, но это только потому, что есть много логических операторов):

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

Вверху есть несколько инструкций, надеюсь, это поможет;)

0 голосов
/ 08 ноября 2018

Так и должно быть, проверка всех вложений в сообщении тоже

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