-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
part 3 - ip cache (requires refactor)
- Loading branch information
1 parent
0be4a07
commit 7ece993
Showing
7 changed files
with
244 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
src/main/kotlin/ua/mei/minekord/bot/extension/IPCheckExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package ua.mei.minekord.bot.extension | ||
|
||
import com.mojang.authlib.GameProfile | ||
import dev.kord.common.entity.ButtonStyle | ||
import dev.kord.core.behavior.channel.createMessage | ||
import dev.kord.core.entity.Member | ||
import dev.kord.rest.builder.message.EmbedBuilder | ||
import dev.kord.rest.builder.message.embed | ||
import dev.kordex.core.components.ComponentContainer | ||
import dev.kordex.core.components.components | ||
import dev.kordex.core.components.disabledButton | ||
import dev.kordex.core.components.publicButton | ||
import dev.kordex.core.extensions.Extension | ||
import dev.kordex.core.i18n.toKey | ||
import dev.kordex.core.time.TimestampType | ||
import dev.kordex.core.time.toDiscord | ||
import io.ktor.util.network.address | ||
import kotlinx.coroutines.launch | ||
import kotlinx.datetime.Clock | ||
import ua.mei.minekord.bot.MinekordBot | ||
import ua.mei.minekord.cache.IPCache | ||
import ua.mei.minekord.config.MinekordConfig.Auth | ||
import ua.mei.minekord.config.MinekordConfig.Messages | ||
import ua.mei.minekord.config.spec.MessagesSpec | ||
import ua.mei.minekord.event.IPCheckEvent | ||
import ua.mei.minekord.utils.AuthUtils | ||
import java.net.SocketAddress | ||
|
||
class IPCheckExtension : Extension() { | ||
override val name: String = "minekord.ipcheck" | ||
|
||
override suspend fun setup() { | ||
IPCheckEvent.EVENT.register { socketAddress, profile -> | ||
if (!Auth.ipBasedLogin) return@register | ||
|
||
MinekordBot.launch { | ||
try { | ||
val member: Member? = AuthUtils.findMember(profile.name) | ||
|
||
member?.getDmChannelOrNull()?.createMessage { | ||
embed { | ||
title = Messages.embedTitle | ||
addIpField(socketAddress) | ||
addTimeField() | ||
} | ||
components { | ||
addYesButton(socketAddress, profile) | ||
addNoButton(socketAddress, profile) | ||
} | ||
} | ||
} catch (e: Exception) { | ||
println("Error handling IP check: ${e.message}") | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun EmbedBuilder.addIpField(socketAddress: SocketAddress) { | ||
field { | ||
name = "> IP" | ||
value = "> ${socketAddress.address}" | ||
inline = true | ||
} | ||
} | ||
|
||
private fun EmbedBuilder.addTimeField() { | ||
field { | ||
name = "> ${MessagesSpec.timeLabel}" | ||
value = "> ${Clock.System.now().toDiscord(TimestampType.Default)}" | ||
inline = true | ||
} | ||
} | ||
|
||
private suspend fun ComponentContainer.addYesButton(socketAddress: SocketAddress, profile: GameProfile) { | ||
publicButton { | ||
label = Messages.yesButton.toKey() | ||
style = ButtonStyle.Success | ||
|
||
action { | ||
IPCache.ipCache[profile.name] = socketAddress.address | ||
|
||
edit { | ||
components { | ||
disabledButton { | ||
label = Messages.yesButton.toKey() | ||
style = ButtonStyle.Success | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private suspend fun ComponentContainer.addNoButton(socketAddress: SocketAddress, profile: GameProfile) { | ||
publicButton { | ||
label = Messages.noButton.toKey() | ||
style = ButtonStyle.Danger | ||
|
||
action { | ||
IPCache.blockedIps += socketAddress.address | ||
|
||
edit { | ||
embed { | ||
title = Messages.ipBlockedTitle | ||
addIpField(socketAddress) | ||
addTimeField() | ||
} | ||
components { | ||
addUnblockButton(socketAddress, profile) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private suspend fun ComponentContainer.addUnblockButton(socketAddress: SocketAddress, profile: GameProfile) { | ||
publicButton { | ||
label = Messages.unblockButton.toKey() | ||
style = ButtonStyle.Danger | ||
|
||
action { | ||
IPCache.blockedIps.removeAll { it == socketAddress.address } | ||
IPCache.alreadyRequestedIps[profile.name]?.removeAll { it == socketAddress.address } | ||
|
||
edit { | ||
embed { | ||
title = Messages.ipUnblockedTitle | ||
addIpField(socketAddress) | ||
addTimeField() | ||
} | ||
components { | ||
disabledButton { | ||
label = Messages.unblockButton.toKey() | ||
style = ButtonStyle.Danger | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/ua/mei/minekord/config/spec/MessagesSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package ua.mei.minekord.config.spec | ||
|
||
import com.uchuhimo.konf.ConfigSpec | ||
|
||
object MessagesSpec : ConfigSpec() { | ||
val ipKickMessage by required<String>() | ||
val embedTitle by required<String>() | ||
val timeLabel by required<String>() | ||
val yesButton by required<String>() | ||
val noButton by required<String>() | ||
val unblockButton by required<String>() | ||
val ipBlockedTitle by required<String>() | ||
val ipUnblockedTitle by required<String>() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters