text: const malScraper = require('mal-scraper')
  // seiyuu bedo lib
  const Mal = require("node-myanimelist")
  
  
  // some trash here
  const channelRegex = /\<\#(.+?)\>/
  const imageLinkRegex = /(https?:\/\/.*\.(?:png|jpg|gif|jpeg)\??.*)/i
  
  class yinani {
      static handleMalQuery(query, message, deletemessage, is7up) {
          malScraper.getResultsFromSearch(query)
              .then(data => {
                  if (is7up) {
                      var above7 = data.filter(show => show.payload.score >= 7)
                      if (is7up && above7.length === 0) {
                          message.channel.send("above 7!")
                          return
                      }
                  }
                  var candidateShow = above7 ? above7[0] : data[0]
                  var {
                      name,
                      url
                  } = candidateShow
                  var {
                      start_year: year,
                      score,
                      status
                  } = candidateShow.payload
                  malScraper.getInfoFromURL(url)
                      .then(data => {
                          var image = data.picture
                          var genres = "`" + data.genres.join("` `") + "`"
                          var studios = data.studios.join(", ")
                          var {
                              rating,
                              ranked,
                              episodes,
                              popularity,
                              synopsis
                          } = data
                          synopsis = toTweetLength(synopsis)
                          message.channel.send({
                              content: url,
                              embed: {
                                  title: name,
                                  url: url,
                                  color: 1752220,
                                  footer: {
                                      icon_url: message.author.avatarURL,
                                      text:"/w discord.js, Invoked by " + message.author.username + "#" + message.author.discriminator
                                  },
                                  image: {
                                      url: image
                                  },
                                  timestamp: new Date(),
                                  fields: [{
                                          name: "**Synopsis**",
                                          value: synopsis,
                                      },
                                      {
                                          name: "**Genres**",
                                          value: genres,
                                      },
                                      {
                                          name: "**Studios**",
                                          value: studios,
                                      },
                                      {
                                          name: "**Score**",
                                          value: score,
                                          inline: true
                                      },
                                      {
                                          name: "**Ranked**",
                                          value: ranked,
                                          inline: true
                                      },
                                      {
                                          name: "**Popularity**",
                                          value: popularity,
                                          inline: true
                                      },
                                      {
                                          name: "**Rating**",
                                          value: rating,
                                      },
                                      {
                                          name: "**Year**",
                                          value: year,
                                          inline: true
                                      },
                                      {
                                          name: "**Total eps**",
                                          value: episodes,
                                          inline: true
                                      },
                                     
                                      {
                                          name: "**Status**",
                                          value: status,
                                          inline: true
                                      },
                                      {
                                          name: "**Download [if available]**",
                                          value: ":flag_id: https://yin.kireisubs.org/lib/anime.php?q="+query.replace(/ /g, "%2B") + "\n:flag_id::clapper: "+"https://nino.zonawibu.cc/?s="+query.replace(/ /g, "+")+"\n:flag_us:"+"https://nyaa.si/?f=0&c=0_0&q="+query.replace(/ /g, "+"),
                                          inline: true
                                      }
                                  ]
                              }
                          })
                          
                      })
                      .catch((err) => console.error(err))
              })
              .catch((err) => console.error(err))
      }
  
  
      static handleSeiyuuQuery(query, message) {
          Mal.search("person", query, {
              limit: 1,
              Page: 1
          }).then(j => {
              var seiyuuarray = []
              var seiyuuarraylength = j.results.length
              j.results.forEach(person => {
                  Mal.person(person.mal_id).then(function (j) {
                      person.popularity = j.member_favorites
                      seiyuuarray.push(person)
                      handlejump()
                  })
              })
  
              function handlejump() {
                  if (seiyuuarraylength == seiyuuarray.length) {
                      seiyuuarray.sort(function (a, b) {
                          return b.popularity - a.popularity
                      })
                      var result = seiyuuarray[0]
                      var {
                          name,
                          url,
                          image_url: image
                      } = result
                      var embedobj = {
                          title: name,
                          url: url,
                          color: 16728193,
                          footer: {
                              icon_url: message.author.avatarURL,
                              text:"/w discord.js, Invoked by " +message.author.username + "#" + message.author.discriminator
                          },
                          image: {
                              url: image
                          },
                          timestamp: new Date(),
                          fields: []
                      }
  
                      Mal.person(result.mal_id).then(j => {
                          var birthday = new Date(Date.parse(j.birthday))
                          var formattedbirthday
                          var birthdayexists = false
                          var bdaymonth = birthday.getMonth() + 1
  
                          if (birthday.getFullYear()) {
  
                              formattedbirthday = birthday.getDate() + "/" + bdaymonth + "/" + birthday.getFullYear()
                              birthdayexists = true
  
                          } else if (birthday.getMonth()) {
  
                              formattedbirthday = birthday.getDate() + "/" + bdaymonth
                              birthdayexists = true
  
                          }
  
                          if (birthdayexists) {
                              embedobj.fields.unshift({
                                  name: "Birthday",
                                  value: formattedbirthday,
                              })
                          }
  
                          var about = j.about
                          if (typeof (about) == "string") {
                              about = about.split("\n")
                              for (var element of about) {
                                  var clean = element.replace(/(\r\n|\n|\r|\\n)/gm, "").split(/:(.+)/)
                                  if (clean.length >= 2) {
                                      embedobj.fields.unshift({
                                          name: clean[0],
                                          value: clean[1]
                                      })
                                  }
                              }
                          }
                          message.channel.send({
                              content: url,
                              embed: embedobj
                          })
                          
                      })
                  }
              }
          })
      }
  
      static handleMangaQuery(query, message) {
          Mal.search("manga", query, {
              limit: 1,
              Page: 1
          }).then(j => {
              var {
                  title,
                  url,
                  image_url: image
              } = j.results[0]
              Mal.manga(j.results[0].mal_id).then(data => {
                  image = data.image_url
                  var genres = processMangaGenres(data.genres)
                  var {
                      rank: ranked,
                      popularity,
                      status
                  } = data
                  if (ranked == null) {
                      ranked = "N/A"
                  }
                  var score = data.score.toString()
                  var synopsis = toTweetLength(data.synopsis)
                  message.channel.send({
                      content: url,
                      embed: {
                          title: title,
                          url: url,
                          color: 2817854,
                          footer: {
                              icon_url: message.author.avatarURL,
                              text:"/w discord.js, Invoked by " + message.author.username + "#" + message.author.discriminator
                          },
                          image: {
                              url: image
                          },
                          timestamp: new Date(),
                          fields: [{
                                  name: "Synopsis",
                                  value: synopsis,
                              },
                              {
                                  name: "Genres",
                                  value: genres,
                              },
                              {
                                  name: "Score",
                                  value: score,
                                  inline: true
                              },
                              {
                                  name: "Ranked",
                                  value: ranked,
                                  inline: true
                              },
                              {
                                  name: "Popularity",
                                  value: popularity,
                                  inline: true
                              },
                              {
                                  name: "\n**read and download [if possible]**",
                                  value: ":flag_id: http://yin-manga.kireisubs.org/get/?n0="+query.replace(/ /g, "%2B") + "\n:flag_us: "+"https://mangadex.org/search?title="+query.replace(/ /g, "+") + "\n:flag_us: "+"https://mangahub.io/search?q="+query.replace(/ /g, "+"),
                                  inline: true
                              },
                              {
                                  name: "Status",
                                  value: status,
                                  inline: true
                              }
                            
                          ]
                      }
                  })
                  
              })
          })
      }
  
  
  
      static temporaryMessage(sendstr, message) {
          message.channel.send(sendstr)
              .then(msg => {
                  msg.delete(3000)
              })
          message.delete().catch((err) => {})
      }
  
  }
  
  module.exports = yinani
  
  function processMangaGenres(input) {
      var genrearr = input.map(m => m.name)
      return "`" + genrearr.join("` `") + "`"
  }
  
  function toTweetLength(input) {
      if (input.length <= 280) {
          return input
      }
      return input.slice(0, 280) + "..."
  }