Skip to content

Commit

Permalink
Konvert AutoOuter & DiscordActivityListener to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
schnapster committed Jan 14, 2024
1 parent 98032a8 commit 6d0ae40
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 129 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2016-2024 the original author or authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package space.npstr.wolfia.domain.setup.lastactive

import io.lettuce.core.pubsub.RedisPubSubAdapter
import net.dv8tion.jda.api.sharding.ShardManager
import org.springframework.stereotype.Component
import space.npstr.wolfia.domain.setup.GameSetupService
import space.npstr.wolfia.system.redis.Redis

/**
* This component listens the keys expiring from [ActivityService] and takes action
*
*
* Since we could lose the redis connection, or restart, or <insert any other reason to miss expiry events>,
* a "manual" check when starting the game should still be performed.
</insert> */
@Component
class AutoOuter(
redis: Redis,
private val gameSetupService: GameSetupService,
private val shardManager: ShardManager,
) : RedisPubSubAdapter<String, String>() {

private val expireChannel = "__keyevent@*__:expired"
private val redisKeyParser = RedisKeyParser()

init {
redis.pubSub.let {
it.addListener(this)
it.sync().psubscribe(expireChannel)
}
}

override fun message(channel: String, message: String) {
if (expireChannel == channel) {
this.expired(message)
}
}

override fun message(pattern: String, channel: String, message: String) {
if (expireChannel == pattern || expireChannel == channel) {
this.expired(message)
}
}

private fun expired(key: String) {
redisKeyParser.fromKey(key)?.let { outUser(it) }
}

private fun outUser(userId: Long) {
gameSetupService.outUserDueToInactivity(userId, this.shardManager)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2016-2024 the original author or authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package space.npstr.wolfia.domain.setup.lastactive

import net.dv8tion.jda.api.entities.User
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
import net.dv8tion.jda.api.events.user.UserTypingEvent
import org.springframework.context.event.EventListener
import org.springframework.core.Ordered
import org.springframework.core.annotation.Order
import org.springframework.stereotype.Component

@Component
class DiscordActivityListener(
private val service: ActivityService
) {

@Order(Ordered.HIGHEST_PRECEDENCE)
@EventListener
fun onUserTyping(event: UserTypingEvent) {
active(event.user)
}

@Order(Ordered.HIGHEST_PRECEDENCE)
@EventListener
fun onMessageReceived(event: MessageReceivedEvent) {
active(event.author)
}

private fun active(user: User) {
service.recordActivity(user)
}
}

0 comments on commit 6d0ae40

Please sign in to comment.