Skip to content

Commit

Permalink
Ban time accumulates
Browse files Browse the repository at this point in the history
  • Loading branch information
demidko committed Jan 2, 2025
1 parent 90e4d10 commit 45307e1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.demidko.glock

import com.github.kotlintelegrambot.entities.ChatId.Companion.fromId
import com.github.kotlintelegrambot.entities.ChatPermissions
import com.sksamuel.hoplite.ConfigLoaderBuilder
import com.sksamuel.hoplite.ExperimentalHoplite
Expand Down
61 changes: 45 additions & 16 deletions src/main/kotlin/com/github/demidko/glock/ChatOps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.github.kotlintelegrambot.entities.ChatPermissions
import com.github.kotlintelegrambot.entities.Message
import org.apache.commons.collections4.QueueUtils.synchronizedQueue
import org.apache.commons.collections4.queue.CircularFifoQueue
import java.io.Closeable
import java.time.Duration
import java.time.Duration.ofSeconds
import java.time.Instant.now
Expand All @@ -29,6 +28,37 @@ class ChatOps(
private val healingTimeZone: ZoneId
) {

init {
bot.getChat(chatId).getOrNull()?.let {
buildString {
with(it) {
append("id").append(id).append(' ')
if (firstName != null) {
append(firstName).append(' ')
}
if (lastName != null) {
append(lastName).append(' ')
}
if (username != null) {
append('@').append(username).append(' ')
}
if (inviteLink != null) {
append(inviteLink).append(' ')
}
if (bio != null) {
appendLine(bio)
}
if (description != null) {
appendLine(description)
}
if (pinnedMessage != null) {
appendLine(pinnedMessage)
}
}
}.let(::println)
}
}

private val messagesToLifetimes = ConcurrentHashMap<Long, Long>()
private val recentMessages = synchronizedQueue(CircularFifoQueue<Message>(12))
private val statuettes = ConcurrentLinkedQueue<Long>()
Expand Down Expand Up @@ -102,7 +132,7 @@ class ChatOps(
return
}
bot.deleteMessage(chatId, statuette)
mute(message, restrictionsDuration.seconds, "💥")
hurt(message, restrictionsDuration.seconds, "💥")
}

fun buckshot(gunfighterMessage: Message) {
Expand All @@ -114,15 +144,15 @@ class ChatOps(
}
val emoji = setOf("💥", "🗯️", "⚡️")
if (targetMessages.size == 1) {
mute(targetMessages.random(), restrictionsDuration.seconds, emoji.random())
hurt(targetMessages.random(), restrictionsDuration.seconds, emoji.random())
markAsTemp(gunfighterMessage)
return
}
val targetsCount = nextInt(2, targetMessages.size + 1)
for (t in 1..targetsCount) {
val target = targetMessages.random()
val restrictionsDurationSec = nextLong(45, restrictionsDuration.seconds * 2 + 1)
mute(target, restrictionsDurationSec, emoji.random())
hurt(target, restrictionsDurationSec, emoji.random())
}
markAsTemp(gunfighterMessage)
}
Expand All @@ -133,7 +163,7 @@ class ChatOps(
markAsTemp(gunfighterMessage)
return
}
mute(target, restrictionsDuration.seconds, "💥")
hurt(target, restrictionsDuration.seconds, "💥")
markAsTemp(gunfighterMessage)
}

Expand All @@ -144,20 +174,19 @@ class ChatOps(
}
}

private fun isTopic(message: Message): Boolean {
return message.chat.type == "channel"
|| message.authorSignature != null
|| message.forwardSignature != null
private fun hurt(target: Message, restrictionsDurationSec: Long, emoji: String) {
val userId = target.from?.id ?: return
val untilEpochSecond = epochSecond(userId) + restrictionsDurationSec
bot.restrictChatMember(chatId, userId, restrictions, untilEpochSecond)
reply(target, emoji)
}

private fun mute(target: Message, restrictionsDurationSec: Long, shootEmoji: String) {
if (isTopic(target)) {
return
private fun epochSecond(userId: Long): Long {
val chatMember = bot.getChatMember(chatId, userId).getOrNull() ?: return now().epochSecond
if (chatMember.status == "restricted") {
return chatMember.untilDate?.toLong() ?: now().epochSecond
}
val userId = target.from?.id ?: return
val untilEpochSecond = now().epochSecond + restrictionsDurationSec
bot.restrictChatMember(chatId, userId, restrictions, untilEpochSecond)
reply(target, shootEmoji)
return now().epochSecond
}

private fun isLifetimeExceeded(epochSecond: Long): Boolean {
Expand Down
7 changes: 0 additions & 7 deletions src/main/kotlin/com/github/demidko/glock/GlockBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.github.kotlintelegrambot.dispatcher.message
import com.github.kotlintelegrambot.entities.ChatId.Companion.fromId
import com.github.kotlintelegrambot.entities.ChatPermissions
import com.github.kotlintelegrambot.entities.Message
import java.io.Closeable
import java.lang.Thread.startVirtualThread
import java.time.Duration
import java.time.Duration.ofDays
Expand Down Expand Up @@ -48,13 +47,7 @@ class GlockBot(

private val idToChatOps = ConcurrentHashMap<Long, ChatOps>()

private var previousChatsCount = 0

fun cleanTempMessages() {
if(idToChatOps.count() > previousChatsCount) {
previousChatsCount = idToChatOps.count()
println("Bot has $previousChatsCount chats")
}
forEachChat(ChatOps::cleanTempMessages)
}

Expand Down

0 comments on commit 45307e1

Please sign in to comment.