Newer
Older
yuyuko-bot / src / main / scala / listeners / Reactions.scala
@Илья Панфёров Илья Панфёров on 15 Jul 1 KB fix: Update yuyuko
package social.gensokyo.discordbot
package listeners

import ackcord.EventListenerMessage.findCache
import ackcord.requests._
import ackcord.syntax._
import ackcord.data.{ Guild, PartialEmoji, RoleId }
import ackcord.{ APIMessage, DiscordClient, EventListener, EventsController, TextChannelEventListenerMessage }
import ackcord.requests.{ AddGuildMemberRole, Requests }
import akka.NotUsed
import social.gensokyo.discordbot.roleassign.RoleAssign

class Reactions(requests: Requests)(implicit client: DiscordClient) extends EventsController(requests) {
  private def getEmojiName(emoji: PartialEmoji): Option[String] = emoji match {
    case PartialEmoji(None, emojiName, None) => emojiName
    case PartialEmoji(Some(_), emojiName, None) => emojiName.map(en => s":$en:")
    case _ => None
  }

  val onUserReactAddRole: EventListener[APIMessage.MessageReactionAdd, NotUsed] = TextChannelEvent.on[APIMessage.MessageReactionAdd].withRequestOpt { m =>
    m.event.messageId match {
      case id if id.toString == RoleAssign.textId =>
        getEmojiName(m.event.emoji)
          .flatMap { emojiName =>
            RoleAssign.getRole(emojiName).flatMap { roleId =>
              m.event.guild.map { guild =>
                AddGuildMemberRole(guild.id, m.event.userId, RoleId(roleId))
              }
            }
          }
      case _ => None
    }
  }
  val onUserReactRemoveRole: EventListener[APIMessage.MessageReactionRemove, NotUsed] = TextChannelEvent.on[APIMessage.MessageReactionRemove].withRequestOpt { m =>
    m.event.messageId match {
      case id if id.toString == RoleAssign.textId =>
        getEmojiName(m.event.emoji)
          .flatMap { emojiName =>
            RoleAssign.getRole(emojiName).flatMap { roleId =>
              m.event.guild.map { guild =>
                RemoveGuildMemberRole(guild.id, m.event.userId, RoleId(roleId))
              }
            }
          }
      case _ => None
    }
  }
}