Skip to content

Commit 32bd009

Browse files
committed
feat: add user cooldown types
1 parent 7562621 commit 32bd009

File tree

6 files changed

+165
-26
lines changed

6 files changed

+165
-26
lines changed

bot/src/main/kotlin/me/melijn/bot/Melijn.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ object Melijn {
132132
enabled = true
133133

134134
if (settings.process.environment == Environment.TESTING)
135-
defaultGuild(settings.process.testingServerId.toULong())
135+
defaultGuild(settings.process.testingServerId?.toULong())
136136

137137
useLimiter {
138138
cooldownHandler = MelijnCooldownHandler()
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,38 @@
11
package me.melijn.bot.database.manager
22

33
import me.melijn.ap.injector.Inject
4+
import me.melijn.gen.UserCommandCooldownData
5+
import me.melijn.gen.UserCooldownData
46
import me.melijn.gen.database.manager.AbstractUserCommandCooldownManager
7+
import me.melijn.gen.database.manager.AbstractUserCooldownManager
58
import me.melijn.kordkommons.database.DriverManager
69

710
@Inject
811
class UserCommandCooldownManager(override val driverManager: DriverManager) : AbstractUserCommandCooldownManager(driverManager)
912
@Inject
10-
class UserCooldownManager(override val driverManager: DriverManager) : AbstractUserCooldownManager(driverManager)
13+
class UserCooldownManager(override val driverManager: DriverManager) : AbstractUserCooldownManager(driverManager)
14+
15+
/** Provides a centralized api for storing, retrieving and updating cooldown types. **/
16+
@Inject
17+
class CooldownManager(
18+
private val userCooldownManager: UserCooldownManager,
19+
private val userCommandCooldownManager: UserCommandCooldownManager,
20+
) {
21+
22+
/** User Command Cooldown context **/
23+
fun getUserCmdCd(userId: ULong, commandId: Int): UserCommandCooldownData? {
24+
return userCommandCooldownManager.getById(userId, commandId)
25+
}
26+
fun storeUserCmdCd(userCommandCooldownData: UserCommandCooldownData) {
27+
userCommandCooldownManager.store(userCommandCooldownData)
28+
}
29+
30+
/** User Cooldown context **/
31+
fun getUserCd(userId: ULong): UserCooldownData? {
32+
return userCooldownManager.getById(userId)
33+
}
34+
35+
fun storeUserCd(userCommandCooldownData: UserCooldownData) {
36+
userCooldownManager.store(userCommandCooldownData)
37+
}
38+
}

bot/src/main/kotlin/me/melijn/bot/database/manager/UsageHistoryManager.kt

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,45 @@ import com.kotlindiscord.kord.extensions.usagelimits.ratelimits.UsageHistoryImpl
77
import me.melijn.ap.injector.Inject
88
import me.melijn.bot.utils.KoinUtil
99
import me.melijn.gen.UserCommandUsageHistoryData
10+
import me.melijn.gen.UserUsageHistoryData
1011
import me.melijn.gen.database.manager.AbstractUserCommandUsageHistoryManager
12+
import me.melijn.gen.database.manager.AbstractUserUsageHistoryManager
1113
import me.melijn.kordkommons.database.DriverManager
1214
inline val Int.b get() = this.toByte()
1315

1416
@Inject
15-
class UsageHistoryManager(override val driverManager: DriverManager) :
16-
AbstractUserCommandUsageHistoryManager(driverManager) {
17+
class UserCommandUsageHistoryManager(override val driverManager: DriverManager) : AbstractUserCommandUsageHistoryManager(driverManager)
18+
@Inject
19+
class UserUsageHistoryManager(override val driverManager: DriverManager) : AbstractUserUsageHistoryManager(driverManager)
20+
21+
@Inject
22+
class UsageHistoryManager(
23+
private val userCommandUsageHistoryManager: UserCommandUsageHistoryManager,
24+
private val userUsageHistoryManager: UserUsageHistoryManager,
25+
) {
1726
private val objectMapper by KoinUtil.inject<ObjectMapper>()
1827

19-
fun getDeserialized(userId: ULong, commandId: Int): UsageHistory {
20-
val byId = getById(userId, commandId)
28+
fun getUserCmdHistDeserialized(userId: ULong, commandId: Int): UsageHistory {
29+
val byId = userCommandUsageHistoryManager.getById(userId, commandId)
30+
val barr = byId?.usageHistory ?: return UsageHistoryImpl()
31+
return objectMapper.readValue<UsageHistoryImpl>(String(barr))
32+
}
33+
34+
fun setUserCmdHistSerialized(userId: ULong, commandId: Int, usageHistory: UsageHistory) {
35+
userCommandUsageHistoryManager.store(
36+
UserCommandUsageHistoryData(userId, commandId, objectMapper.writeValueAsBytes(usageHistory))
37+
)
38+
}
39+
40+
fun getUserHistDeserialized(userId: ULong): UsageHistory {
41+
val byId = userUsageHistoryManager.getById(userId)
2142
val barr = byId?.usageHistory ?: return UsageHistoryImpl()
2243
return objectMapper.readValue<UsageHistoryImpl>(String(barr))
2344
}
2445

25-
fun setSerialized(userId: ULong, commandId: Int, usageHistory: UsageHistory) {
26-
store(UserCommandUsageHistoryData(userId, commandId, objectMapper.writeValueAsBytes(usageHistory)))
46+
fun setUserHistSerialized(userId: ULong, usageHistory: UsageHistory) {
47+
userUsageHistoryManager.store(
48+
UserUsageHistoryData(userId, objectMapper.writeValueAsBytes(usageHistory))
49+
)
2750
}
2851
}
Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package me.melijn.bot.database.model
22

3-
import me.melijn.ap.cacheable.Cacheable
4-
import me.melijn.ap.createtable.CreateTable
3+
import me.melijn.apredgres.cacheable.Cacheable
4+
import me.melijn.apredgres.createtable.CreateTable
55
import org.jetbrains.exposed.sql.Table
66

7+
8+
/** A cooldown in context of (user, commandId) **/
79
@CreateTable
810
@Cacheable
911
object UserCommandCooldown : Table("user_command_cooldown") {
@@ -14,9 +16,7 @@ object UserCommandCooldown : Table("user_command_cooldown") {
1416

1517
override val primaryKey: PrimaryKey = PrimaryKey(userId, commandId)
1618

17-
init {
18-
index(true, userId, commandId)
19-
}
19+
2020
}
2121
@CreateTable
2222
@Cacheable
@@ -28,8 +28,95 @@ object UserCommandUsageHistory : Table("user_command_usage_history") {
2828

2929
override val primaryKey: PrimaryKey = PrimaryKey(userId, commandId)
3030

31-
init {
32-
index("user_cmd_idx", true, userId, commandId)
33-
}
3431
}
3532

33+
/** A cooldown in context of (user) **/
34+
@CreateTable
35+
@Cacheable
36+
object UserCooldown : Table("user_cooldown") {
37+
38+
val userId = ulong("user_id")
39+
val until = long("until")
40+
41+
override val primaryKey: PrimaryKey = PrimaryKey(userId)
42+
}
43+
@CreateTable
44+
@Cacheable
45+
object UserUsageHistory : Table("user_usage_history") {
46+
47+
val userId = ulong("user_id")
48+
val usageHistory = binary("usage_history")
49+
50+
override val primaryKey: PrimaryKey = PrimaryKey(userId)
51+
52+
}
53+
54+
/** A cooldown in context of (messageChannelId) **/
55+
@CreateTable
56+
@Cacheable
57+
object ChannelCooldown : Table("channel_cooldown") {
58+
59+
val channelId = ulong("user_id")
60+
val guildId = ulong("guild_id")
61+
val until = long("until")
62+
63+
override val primaryKey: PrimaryKey = PrimaryKey(channelId)
64+
}
65+
@CreateTable
66+
@Cacheable
67+
object ChannelUsageHistory : Table("channel_usage_history") {
68+
69+
val channelId = ulong("channel_id")
70+
val guildId = ulong("guild_id")
71+
val usageHistory = binary("usage_history")
72+
73+
override val primaryKey: PrimaryKey = PrimaryKey(channelId)
74+
}
75+
76+
/** A cooldown in context of (guildId) **/
77+
@CreateTable
78+
@Cacheable
79+
object GuildCooldown : Table("guild_cooldown") {
80+
81+
val guildId = ulong("guild_id")
82+
val until = long("until")
83+
84+
override val primaryKey: PrimaryKey = PrimaryKey(guildId)
85+
}
86+
87+
@CreateTable
88+
@Cacheable
89+
object GuildUsageHistory : Table("guild_usage_history") {
90+
91+
val guildId = ulong("guild_id")
92+
val usageHistory = binary("usage_history")
93+
94+
override val primaryKey: PrimaryKey = PrimaryKey(guildId)
95+
}
96+
97+
98+
/** A cooldown in context of (guildId) **/
99+
@CreateTable
100+
@Cacheable
101+
object GuildUserCooldown : Table("guild_cooldown") {
102+
103+
val guildId = ulong("guild_id")
104+
val userId = ulong("user_id")
105+
val until = long("until")
106+
107+
override val primaryKey: PrimaryKey = PrimaryKey(guildId)
108+
}
109+
110+
@CreateTable
111+
@Cacheable
112+
object GuildUserUsageHistory : Table("guild_usage_history") {
113+
114+
val guildId = ulong("guild_id")
115+
val userId = ulong("user_id")
116+
val usageHistory = binary("usage_history")
117+
118+
override val primaryKey: PrimaryKey = PrimaryKey(guildId)
119+
}
120+
121+
122+

bot/src/main/kotlin/me/melijn/bot/model/kordex/PersistentUsageLimitType.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,42 @@ import me.melijn.bot.database.manager.CooldownManager
77
import me.melijn.bot.database.manager.UsageHistoryManager
88
import me.melijn.bot.utils.KoinUtil
99
import me.melijn.gen.UserCommandCooldownData
10+
import me.melijn.gen.UserCooldownData
1011

1112
enum class PersistentUsageLimitType : UsageLimitType {
1213

1314
COMMAND_USER {
1415
override fun getCooldown(context: DiscriminatingContext): Long {
15-
return cooldownManager.getById(context.userId, context.commandId)?.until ?: 0
16+
return cooldownManager.getUserCmdCd(context.userId, context.commandId)?.until ?: 0
1617
}
1718

1819
override fun setCooldown(context: DiscriminatingContext, until: Long) {
19-
cooldownManager.store(UserCommandCooldownData(context.userId, context.commandId, until))
20+
cooldownManager.storeUserCmdCd(UserCommandCooldownData(context.userId, context.commandId, until))
2021
}
2122

2223
override fun getUsageHistory(context: DiscriminatingContext): UsageHistory {
23-
return usageHistoryManager.getDeserialized(context.userId, context.commandId)
24+
return usageHistoryManager.getUserCmdHistDeserialized(context.userId, context.commandId)
2425
}
2526

2627
override fun setUsageHistory(context: DiscriminatingContext, usageHistory: UsageHistory) {
27-
usageHistoryManager.setSerialized(context.userId, context.commandId, usageHistory)
28+
usageHistoryManager.setUserCmdHistSerialized(context.userId, context.commandId, usageHistory)
2829
}
2930
},
3031
USER {
3132
override fun getCooldown(context: DiscriminatingContext): Long {
32-
return cooldownManager.getById(context.userId)?.until ?: 0
33+
return cooldownManager.getUserCd(context.userId)?.until ?: 0
3334
}
3435

35-
override fun getUsageHistory(context: DiscriminatingContext): UsageHistory {
36-
TODO("Not yet implemented")
36+
override fun setCooldown(context: DiscriminatingContext, until: Long) {
37+
cooldownManager.storeUserCd(UserCooldownData(context.userId, until))
3738
}
3839

39-
override fun setCooldown(context: DiscriminatingContext, until: Long) {
40-
TODO("Not yet implemented")
40+
override fun getUsageHistory(context: DiscriminatingContext): UsageHistory {
41+
return usageHistoryManager.getUserHistDeserialized(context.userId)
4142
}
4243

4344
override fun setUsageHistory(context: DiscriminatingContext, usageHistory: UsageHistory) {
44-
TODO("Not yet implemented")
45+
usageHistoryManager.setUserHistSerialized(context.userId, usageHistory)
4546
}
4647

4748
};

gradlew

100755100644
File mode changed.

0 commit comments

Comments
 (0)