Skip to content

Commit 6ccd78d

Browse files
committed
Cache some values
1 parent 7c173c9 commit 6ccd78d

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

src/main/kotlin/net/azisaba/guildchatdiscord/Main.kt

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import net.azisaba.interchat.api.network.Protocol
1919
import net.azisaba.interchat.api.network.protocol.GuildMessagePacket
2020
import org.slf4j.LoggerFactory
2121
import java.io.File
22-
import java.util.UUID
2322

2423
private val logger = LoggerFactory.getLogger("GuildChatDiscord")
2524

@@ -48,32 +47,13 @@ suspend fun main() {
4847

4948
client.on<MessageCreateEvent> {
5049
if (message.author?.isBot != false) return@on
51-
InterChatDiscord.asyncExecutor.execute {
52-
val minecraftUuid = DatabaseManager.query("SELECT `minecraft_uuid` FROM `users` WHERE `discord_id` = ?") {
53-
it.setLong(1, message.author!!.id.value.toLong())
54-
it.executeQuery().use { rs ->
55-
if (rs.next()) {
56-
UUID.fromString(rs.getString("minecraft_uuid"))
57-
} else {
58-
null
59-
}
60-
}
61-
} ?: return@execute
62-
val guildId = DatabaseManager.query("SELECT `guild_id` FROM `channels` WHERE `channel_id` = ?") {
63-
it.setLong(1, message.channelId.value.toLong())
64-
it.executeQuery().use { rs ->
65-
if (rs.next()) {
66-
rs.getLong("guild_id")
67-
} else {
68-
null
69-
}
70-
}
71-
} ?: return@execute
72-
val isMember = InterChatDiscord.guildManager.getMember(guildId, minecraftUuid).exceptionally { null }.join() != null
73-
if (!isMember) return@execute
74-
val packet = GuildMessagePacket(guildId, "Discord", minecraftUuid, message.content, null)
75-
JedisBoxProvider.get().pubSubHandler.publish(Protocol.GUILD_MESSAGE.name, packet)
76-
}
50+
val guildId = DatabaseManager.getGuildIdByChannelId(message.channelId.value.toLong()) ?: return@on
51+
val minecraftUuid = DatabaseManager.getMinecraftUUIDByDiscordId(message.author!!.id.value.toLong()) ?: return@on
52+
// return if the author is not member of the guild
53+
InterChatDiscord.guildManager.getMember(guildId, minecraftUuid).exceptionally { null }.join() ?: return@on
54+
// send packet
55+
val packet = GuildMessagePacket(guildId, "Discord", minecraftUuid, message.content, null)
56+
JedisBoxProvider.get().pubSubHandler.publish(Protocol.GUILD_MESSAGE.name, packet)
7757
}
7858

7959
client.on<ApplicationCommandInteractionCreateEvent> {

src/main/kotlin/net/azisaba/guildchatdiscord/util/DatabaseManager.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import net.azisaba.interchat.api.util.Functions
55
import net.azisaba.interchat.api.util.QueryExecutor
66
import org.intellij.lang.annotations.Language
77
import java.sql.PreparedStatement
8+
import java.util.UUID
89

910
object DatabaseManager {
1011
val interChatQueryExecutor = QueryExecutor { sql, action -> queryInterChat(sql) { stmt -> action.accept(stmt) } }
@@ -46,6 +47,32 @@ object DatabaseManager {
4647
}
4748
}.toKotlin()
4849

50+
val getGuildIdByChannelId: (Long) -> Long? = Functions.memoize<Long, Long>(1000 * 60) { channelId ->
51+
query("SELECT `guild_id` FROM `channels` WHERE `channel_id` = ?") {
52+
it.setLong(1, channelId)
53+
it.executeQuery().use { rs ->
54+
if (rs.next()) {
55+
rs.getLong("guild_id")
56+
} else {
57+
null
58+
}
59+
}
60+
}
61+
}.toKotlin()
62+
63+
val getMinecraftUUIDByDiscordId: (Long) -> UUID? = Functions.memoize<Long, UUID>(1000 * 30) { discordId ->
64+
query("SELECT `minecraft_uuid` FROM `users` WHERE `discord_id` = ?") {
65+
it.setLong(1, discordId)
66+
it.executeQuery().use { rs ->
67+
if (rs.next()) {
68+
UUID.fromString(rs.getString("minecraft_uuid"))
69+
} else {
70+
null
71+
}
72+
}
73+
}
74+
}.toKotlin()
75+
4976
inline fun <R> query(@Language("SQL") sql: String, block: (PreparedStatement) -> R): R =
5077
dataSource.connection.use { connection ->
5178
connection.prepareStatement(sql).use(block)

0 commit comments

Comments
 (0)