<commit_before>package twitch

import java.util.Optional
import javax.inject.{Inject, Provider}

import com.cobble.bot.common.api.PermissionLevel
import com.cobble.bot.common.api.PermissionLevel.PermissionLevel
import com.cobble.bot.common.models.{CustomCommand, FilterSettings}
import com.cobble.bot.common.ref.MtrConfigRef
import net.engio.mbassy.listener.Handler
import org.kitteh.irc.client.library.element.MessageTag
import org.kitteh.irc.client.library.event.channel.ChannelMessageEvent
import org.kitteh.irc.client.library.event.client.ClientConnectedEvent
import org.kitteh.irc.client.library.feature.twitch.event.UserNoticeEvent
import play.api.cache.SyncCacheApi
import play.api.db.Database
import twitch.api.TwitchChatMessageEvent
import twitch.api.usernotice.UserNoticeMessageId
import twitch.events.{TwitchCheerEvent, TwitchCommandExecutionEvent, TwitchMessageEvent, TwitchSubEvent}
import twitch.filters.{TwitchBlacklistFilter, TwitchCapsFilter, TwitchLinksFilter}
import twitch.util.TwitchMessageUtil

class TwitchBotEventListener @Inject()(
                                          implicit twitchBot: Provider[TwitchBot],
                                          mtrConfigRef: MtrConfigRef,
                                          twitchCommandRegistry: TwitchCommandRegistry,
                                          db: Database,
                                          cache: SyncCacheApi,
                                          capsFilter: TwitchCapsFilter,
                                          linksFilter: TwitchLinksFilter,
                                          blacklistFilter: TwitchBlacklistFilter,
                                          twitchMessageUtil: TwitchMessageUtil,
                                          twitchBotCheerEventHandler: TwitchBotCheerEventHandler
                                      ) {

    @Handler
    def connected(event: ClientConnectedEvent): Unit = {
        TwitchLogger.info("Monster Truck Bot connected!")
    }

    @Handler
    def messageReceived(msgEvent: ChannelMessageEvent): Unit =
        if (msgEvent.getActor.getNick != mtrConfigRef.twitchUsername)
            twitchBot.get.client.getEventManager.callEvent(new TwitchMessageEvent(msgEvent))

    @Handler
    def twitchMessageReceived(msgEvent: TwitchMessageEvent): Unit = {
        if (!filterMessage(msgEvent))
            if (msgEvent.getMessage.startsWith(mtrConfigRef.commandPrefix)) {
                val messageSplit: Array[String] = msgEvent.getMessage.split("\\s")
                twitchBot.get.client.getEventManager.callEvent(new TwitchCommandExecutionEvent(
                    msgEvent.getMessageEvent,
                    messageSplit.head.substring(mtrConfigRef.commandPrefix.length).toLowerCase,
                    messageSplit.tail
                ))
            }

        if (msgEvent.getTag("bits").isPresent)
            twitchBot.get.client.getEventManager.callEvent(new TwitchCheerEvent(msgEvent.getMessageEvent, msgEvent.getTag("bits").get().getValue.get().toInt))
    }

    @Handler
    def twitchUserNoticeRecived(userNoticeEvent: UserNoticeEvent): Unit = {
        val msgIdTag: Optional[MessageTag] = userNoticeEvent.getTag("msg-id")
        if (msgIdTag.isPresent && msgIdTag.get().getValue.isPresent) {
            val msgId: String = msgIdTag.get().getValue.get()
            if (msgId == "sub" || msgId == "resub")
                twitchBot.get.client.getEventManager.callEvent(new TwitchSubEvent(userNoticeEvent))
        }
    }

    def filterMessage(message: TwitchMessageEvent): Boolean = {
        var hasBeenFiltered: Boolean = false
        val filterSettings: Option[FilterSettings] = FilterSettings.get(mtrConfigRef.guildId)
        if (filterSettings.isDefined) {
            val userPermissionLevel: PermissionLevel = getUserPermissionLevel(message)
            if (filterSettings.get.capsFilterEnabled && userPermissionLevel < filterSettings.get.getCapsFilterExemptionLevel)
                hasBeenFiltered = hasBeenFiltered || capsFilter.filterMessage(message, filterSettings.get)
            if (filterSettings.get.linksFilterEnabled && userPermissionLevel < filterSettings.get.getLinksFilterExemptionLevel)
                hasBeenFiltered = hasBeenFiltered || linksFilter.filterMessage(message, filterSettings.get)
            if (filterSettings.get.blacklistFilterEnabled && userPermissionLevel < filterSettings.get.getBlackListFilterExemptionLevel)
                hasBeenFiltered = hasBeenFiltered || blacklistFilter.filterMessage(message, filterSettings.get)
        }
        hasBeenFiltered
    }

    @Handler
    def twitchCommandExecuted(commandEvent: TwitchCommandExecutionEvent): Unit = {
        val commandOpt = twitchCommandRegistry.commands.get(commandEvent.getCommand)
        if (commandOpt.isDefined && getUserPermissionLevel(commandEvent) >= commandOpt.get.permissionLevel)
            commandOpt.get.execute(commandEvent)<commit_msg>Sub detection now uses UserNoticeMessageId enum strings<commit_after>package twitch

import java.util.Optional
import javax.inject.{Inject, Provider}

import com.cobble.bot.common.api.PermissionLevel
import com.cobble.bot.common.api.PermissionLevel.PermissionLevel
import com.cobble.bot.common.models.{CustomCommand, FilterSettings}
import com.cobble.bot.common.ref.MtrConfigRef
import net.engio.mbassy.listener.Handler
import org.kitteh.irc.client.library.element.MessageTag
import org.kitteh.irc.client.library.event.channel.ChannelMessageEvent
import org.kitteh.irc.client.library.event.client.ClientConnectedEvent
import org.kitteh.irc.client.library.feature.twitch.event.UserNoticeEvent
import play.api.cache.SyncCacheApi
import play.api.db.Database
import twitch.api.TwitchChatMessageEvent
import twitch.api.usernotice.UserNoticeMessageId
import twitch.events.{TwitchCheerEvent, TwitchCommandExecutionEvent, TwitchMessageEvent, TwitchSubEvent}
import twitch.filters.{TwitchBlacklistFilter, TwitchCapsFilter, TwitchLinksFilter}
import twitch.util.TwitchMessageUtil

class TwitchBotEventListener @Inject()(
                                          implicit twitchBot: Provider[TwitchBot],
                                          mtrConfigRef: MtrConfigRef,
                                          twitchCommandRegistry: TwitchCommandRegistry,
                                          db: Database,
                                          cache: SyncCacheApi,
                                          capsFilter: TwitchCapsFilter,
                                          linksFilter: TwitchLinksFilter,
                                          blacklistFilter: TwitchBlacklistFilter,
                                          twitchMessageUtil: TwitchMessageUtil,
                                          twitchBotCheerEventHandler: TwitchBotCheerEventHandler
                                      ) {

    @Handler
    def connected(event: ClientConnectedEvent): Unit = {
        TwitchLogger.info("Monster Truck Bot connected!")
    }

    @Handler
    def messageReceived(msgEvent: ChannelMessageEvent): Unit =
        if (msgEvent.getActor.getNick != mtrConfigRef.twitchUsername)
            twitchBot.get.client.getEventManager.callEvent(new TwitchMessageEvent(msgEvent))

    @Handler
    def twitchMessageReceived(msgEvent: TwitchMessageEvent): Unit = {
        if (!filterMessage(msgEvent))
            if (msgEvent.getMessage.startsWith(mtrConfigRef.commandPrefix)) {
                val messageSplit: Array[String] = msgEvent.getMessage.split("\\s")
                twitchBot.get.client.getEventManager.callEvent(new TwitchCommandExecutionEvent(
                    msgEvent.getMessageEvent,
                    messageSplit.head.substring(mtrConfigRef.commandPrefix.length).toLowerCase,
                    messageSplit.tail
                ))
            }

        if (msgEvent.getTag("bits").isPresent)
            twitchBot.get.client.getEventManager.callEvent(new TwitchCheerEvent(msgEvent.getMessageEvent, msgEvent.getTag("bits").get().getValue.get().toInt))
    }

    @Handler
    def twitchUserNoticeRecived(userNoticeEvent: UserNoticeEvent): Unit = {
        val msgIdTag: Optional[MessageTag] = userNoticeEvent.getTag("msg-id")
        if (msgIdTag.isPresent && msgIdTag.get().getValue.isPresent) {
            val msgId: String = msgIdTag.get().getValue.get()
            if (msgId == UserNoticeMessageId.SUBSCRIPTION.toString || msgId == UserNoticeMessageId.RESUBSCRIPTION.toString)
                twitchBot.get.client.getEventManager.callEvent(new TwitchSubEvent(userNoticeEvent))
        }
    }

    def filterMessage(message: TwitchMessageEvent): Boolean = {
        var hasBeenFiltered: Boolean = false
        val filterSettings: Option[FilterSettings] = FilterSettings.get(mtrConfigRef.guildId)
        if (filterSettings.isDefined) {
            val userPermissionLevel: PermissionLevel = getUserPermissionLevel(message)
            if (filterSettings.get.capsFilterEnabled && userPermissionLevel < filterSettings.get.getCapsFilterExemptionLevel)
                hasBeenFiltered = hasBeenFiltered || capsFilter.filterMessage(message, filterSettings.get)
            if (filterSettings.get.linksFilterEnabled && userPermissionLevel < filterSettings.get.getLinksFilterExemptionLevel)
                hasBeenFiltered = hasBeenFiltered || linksFilter.filterMessage(message, filterSettings.get)
            if (filterSettings.get.blacklistFilterEnabled && userPermissionLevel < filterSettings.get.getBlackListFilterExemptionLevel)
                hasBeenFiltered = hasBeenFiltered || blacklistFilter.filterMessage(message, filterSettings.get)
        }
        hasBeenFiltered
    }

    @Handler
    def twitchCommandExecuted(commandEvent: TwitchCommandExecutionEvent): Unit = {
        val commandOpt = twitchCommandRegistry.commands.get(commandEvent.getCommand)
        if (commandOpt.isDefined && getUserPermissionLevel(commandEvent) >= commandOpt.get.permissionLevel)
            commandOpt.get.execute(commandEvent)
{'fraction_non_alphanumeric': 0.08027156549520767, 'fraction_numerical': 0.0, 'mean_word_length': 2.7307262569832402, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3014852', 'n_tokens_mistral': 2564, 'n_tokens_neox': 2503, 'n_words': 385}