Skip to content

Commit 7427fb7

Browse files
committed
Cache the http client in ApprovalExtension.kt
1 parent 5ccc477 commit 7427fb7

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/main/kotlin/io/github/arkosammy12/compsmpdiscordbot/extensions/ApprovalExtension.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class ApprovalExtension() : Extension() {
4444
val approvalEmojiId: () -> Long = { CompSMPDiscordBot.CONFIG_MANAGER.getRawNumberSettingValue(ConfigUtils.APPROVAL_EMOJI_ID)!! }
4545
val approvalRoleId: () -> Long = { CompSMPDiscordBot.CONFIG_MANAGER.getRawNumberSettingValue(ConfigUtils.APPROVAL_ROLE_ID)!! }
4646
val approvalEmojiName: () -> String = { CompSMPDiscordBot.CONFIG_MANAGER.getRawStringSettingValue(ConfigUtils.APPROVAL_EMOJI_NAME)!! }
47+
val httpClient: HttpClient by lazy {
48+
HttpClient()
49+
}
4750

4851
override suspend fun setup() {
4952
this.event<ReactionAddEvent> {
@@ -87,8 +90,7 @@ class ApprovalExtension() : Extension() {
8790
action {
8891
respond {
8992
val username: String = arguments.username
90-
val client = HttpClient()
91-
val response: HttpResponse = client.get(URLBuilder("https://api.mojang.com/users/profiles/minecraft/${username}").build()) {
93+
val response: HttpResponse = httpClient.get(URLBuilder("https://api.mojang.com/users/profiles/minecraft/${username}").build()) {
9294
method = HttpMethod.Get
9395
timeout {
9496
requestTimeoutMillis = 5_000
@@ -108,13 +110,16 @@ class ApprovalExtension() : Extension() {
108110
} else {
109111
content = "Error fetching game profile: ${response.status}"
110112
}
111-
client.close()
112113
}
113114
}
114115
}
115116

116117
}
117118

119+
override suspend fun unload() {
120+
this.httpClient.close()
121+
}
122+
118123
private suspend fun matchesApprovalContext(eventEmoji: ReactionEmoji, user: UserBehavior, message: MessageBehavior): Boolean {
119124
if (eventEmoji !is ReactionEmoji.Custom) {
120125
return false

0 commit comments

Comments
 (0)