Skip to content

Commit 8f12bc1

Browse files
committed
feat: add full support for global interactions
feat: add helpers for shard calculation feat!: change to jemoji
1 parent 8d46aee commit 8f12bc1

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

src/main/kotlin/io/viascom/discord/bot/aluna/bot/DiscordBot.kt

+41-7
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,44 @@ open class DiscordBot(
234234
return alunaProperties.nodeNumber
235235
}
236236

237+
/**
238+
* Get the lower bound of Snowflakes.
239+
*
240+
* This method calculates the lower bound of Snowflakes based on the sharding configuration.
241+
*
242+
* @return The lower bound of Snowflakes as a [Long] value.
243+
*/
244+
fun getLowerBoundOfSnowflake(): Long {
245+
return if (alunaProperties.discord.sharding.type == AlunaDiscordProperties.Sharding.Type.SUBSET) {
246+
alunaProperties.discord.sharding.fromShard * (Long.MAX_VALUE / alunaProperties.discord.sharding.totalShards)
247+
} else {
248+
0L
249+
}
250+
}
251+
252+
/**
253+
* Get the upper bound of Snowflakes.
254+
*
255+
* This method calculates the upper bound ofSnowflakes based on the sharding configuration.
256+
*
257+
* @return The upper bound of Snowflakes as a [Long] value.
258+
*/
259+
fun getUpperBoundOfSnowflake(): Long {
260+
return if (alunaProperties.discord.sharding.type == AlunaDiscordProperties.Sharding.Type.SUBSET) {
261+
(alunaProperties.discord.sharding.fromShard + alunaProperties.discord.sharding.shardAmount) * (Long.MAX_VALUE / alunaProperties.discord.sharding.totalShards) - 1
262+
} else {
263+
Long.MAX_VALUE
264+
}
265+
}
266+
267+
fun getShardBySnowflake(snowflake: Long): Long {
268+
return (snowflake shr 22) % alunaProperties.discord.sharding.totalShards
269+
}
270+
271+
fun getShardOfBotDM(): Long {
272+
return alunaProperties.discord.applicationId?.let { getShardBySnowflake(it.toLong()) } ?: throw IllegalArgumentException("alunaProperties.discord.applicationId is not set")
273+
}
274+
237275
/**
238276
* Register a message for button events. If such an event happens, Aluna will trigger the onButtonInteraction method of the interaction handler.
239277
*
@@ -293,9 +331,7 @@ open class DiscordBot(
293331
authorIds: ArrayList<String>? = null,
294332
interactionUserOnly: Boolean = false
295333
) {
296-
hook.retrieveOriginal()
297-
.queue { registerMessageForButtonEvents(it.id, interaction, multiUse, duration, authorIds, interactionUserOnly) }
298-
334+
hook.retrieveOriginal().queue { registerMessageForButtonEvents(it.id, interaction, multiUse, duration, authorIds, interactionUserOnly) }
299335
}
300336

301337
/**
@@ -357,8 +393,7 @@ open class DiscordBot(
357393
authorIds: ArrayList<String>? = null,
358394
interactionUserOnly: Boolean = false
359395
) {
360-
hook.retrieveOriginal()
361-
.queue { registerMessageForStringSelectEvents(it.id, interaction, multiUse, duration, authorIds, interactionUserOnly) }
396+
hook.retrieveOriginal().queue { registerMessageForStringSelectEvents(it.id, interaction, multiUse, duration, authorIds, interactionUserOnly) }
362397
}
363398

364399
/**
@@ -420,8 +455,7 @@ open class DiscordBot(
420455
authorIds: ArrayList<String>? = null,
421456
interactionUserOnly: Boolean = false
422457
) {
423-
hook.retrieveOriginal()
424-
.queue { registerMessageForEntitySelectEvents(it.id, interaction, multiUse, duration, authorIds, interactionUserOnly) }
458+
hook.retrieveOriginal().queue { registerMessageForEntitySelectEvents(it.id, interaction, multiUse, duration, authorIds, interactionUserOnly) }
425459
}
426460

427461
/**

src/main/kotlin/io/viascom/discord/bot/aluna/bot/InteractionScopedObject.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import java.time.Duration
2626
interface InteractionScopedObject {
2727
/**
2828
* Unique id for this object.
29-
* It's recommended to use NanoId.generate()
29+
* It's recommended to use DiscordContext.newUniqueId()
3030
*/
3131
var uniqueId: String
3232

0 commit comments

Comments
 (0)