Skip to content

Commit

Permalink
feat: add full support for global interactions
Browse files Browse the repository at this point in the history
feat: add helpers for shard calculation
feat!: change to jemoji
  • Loading branch information
itsmefox committed Mar 17, 2024
1 parent 8d46aee commit 8f12bc1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
48 changes: 41 additions & 7 deletions src/main/kotlin/io/viascom/discord/bot/aluna/bot/DiscordBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,44 @@ open class DiscordBot(
return alunaProperties.nodeNumber
}

/**
* Get the lower bound of Snowflakes.
*
* This method calculates the lower bound of Snowflakes based on the sharding configuration.
*
* @return The lower bound of Snowflakes as a [Long] value.
*/
fun getLowerBoundOfSnowflake(): Long {
return if (alunaProperties.discord.sharding.type == AlunaDiscordProperties.Sharding.Type.SUBSET) {
alunaProperties.discord.sharding.fromShard * (Long.MAX_VALUE / alunaProperties.discord.sharding.totalShards)
} else {
0L
}
}

/**
* Get the upper bound of Snowflakes.
*
* This method calculates the upper bound ofSnowflakes based on the sharding configuration.
*
* @return The upper bound of Snowflakes as a [Long] value.
*/
fun getUpperBoundOfSnowflake(): Long {
return if (alunaProperties.discord.sharding.type == AlunaDiscordProperties.Sharding.Type.SUBSET) {
(alunaProperties.discord.sharding.fromShard + alunaProperties.discord.sharding.shardAmount) * (Long.MAX_VALUE / alunaProperties.discord.sharding.totalShards) - 1
} else {
Long.MAX_VALUE
}
}

fun getShardBySnowflake(snowflake: Long): Long {
return (snowflake shr 22) % alunaProperties.discord.sharding.totalShards
}

fun getShardOfBotDM(): Long {
return alunaProperties.discord.applicationId?.let { getShardBySnowflake(it.toLong()) } ?: throw IllegalArgumentException("alunaProperties.discord.applicationId is not set")
}

/**
* Register a message for button events. If such an event happens, Aluna will trigger the onButtonInteraction method of the interaction handler.
*
Expand Down Expand Up @@ -293,9 +331,7 @@ open class DiscordBot(
authorIds: ArrayList<String>? = null,
interactionUserOnly: Boolean = false
) {
hook.retrieveOriginal()
.queue { registerMessageForButtonEvents(it.id, interaction, multiUse, duration, authorIds, interactionUserOnly) }

hook.retrieveOriginal().queue { registerMessageForButtonEvents(it.id, interaction, multiUse, duration, authorIds, interactionUserOnly) }
}

/**
Expand Down Expand Up @@ -357,8 +393,7 @@ open class DiscordBot(
authorIds: ArrayList<String>? = null,
interactionUserOnly: Boolean = false
) {
hook.retrieveOriginal()
.queue { registerMessageForStringSelectEvents(it.id, interaction, multiUse, duration, authorIds, interactionUserOnly) }
hook.retrieveOriginal().queue { registerMessageForStringSelectEvents(it.id, interaction, multiUse, duration, authorIds, interactionUserOnly) }
}

/**
Expand Down Expand Up @@ -420,8 +455,7 @@ open class DiscordBot(
authorIds: ArrayList<String>? = null,
interactionUserOnly: Boolean = false
) {
hook.retrieveOriginal()
.queue { registerMessageForEntitySelectEvents(it.id, interaction, multiUse, duration, authorIds, interactionUserOnly) }
hook.retrieveOriginal().queue { registerMessageForEntitySelectEvents(it.id, interaction, multiUse, duration, authorIds, interactionUserOnly) }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import java.time.Duration
interface InteractionScopedObject {
/**
* Unique id for this object.
* It's recommended to use NanoId.generate()
* It's recommended to use DiscordContext.newUniqueId()
*/
var uniqueId: String

Expand Down

0 comments on commit 8f12bc1

Please sign in to comment.