Skip to content

Commit 790df1b

Browse files
DaRacciRacci
authored andcommitted
feat: Added autoRemoveOnLimit for role selectors
This will remove the users current roles before giving them a new one so they don't receive a fail message and instead can continue on with their nice little peaceful life on discord
1 parent 7a7c87f commit 790df1b

1 file changed

Lines changed: 27 additions & 9 deletions

File tree

src/main/kotlin/dev/racci/elixir/extensions/RoleSelector.kt

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import java.nio.file.Files
3232
import java.nio.file.Paths
3333
import java.util.*
3434
import kotlinx.coroutines.flow.count
35+
import kotlinx.coroutines.flow.filter
36+
import kotlinx.coroutines.flow.toSet
3537
import org.jetbrains.exposed.sql.deleteWhere
3638
import org.jetbrains.exposed.sql.insertIgnore
3739
import org.jetbrains.exposed.sql.select
@@ -79,6 +81,7 @@ class RoleSelector: Extension() {
7981
addRoleSelector(
8082
rsl["name"] as String,
8183
rsl["channel"] as Long,
84+
rsl["autoRemoveOnLimit", false] as Boolean,
8285
rsl.getOrDefault("attachment", "") as String,
8386
(rsl.getOrDefault("limit", -1) as Long).toInt(),
8487
rsl.getOrDefault("removable", true) as Boolean,
@@ -141,17 +144,26 @@ class RoleSelector: Extension() {
141144

142145
private suspend fun roleLimitCheck(
143146
limit: Int,
147+
autoRemoveOnLimit: Boolean,
144148
member: MemberBehavior,
145149
selectorRoles: List<RoleBehavior>,
146150
context: PublicInteractionButtonContext,
147151
): String? {
148-
return if(limit != -1 && member.asMember().roles.count(selectorRoles::contains) >= limit) {
152+
if(limit == -1) return ""
153+
val conflictingRoles = member.asMember().roles.filter(selectorRoles::contains)
154+
if(conflictingRoles.count() < limit) return ""
155+
return if(autoRemoveOnLimit) {
156+
for(role in conflictingRoles.toSet()) {
157+
member.removeRole(role.id, "Maximum Amount of roles for role selector, auto removing to give new role.")
158+
}
159+
""
160+
} else {
149161
context.interactionResponse.followUpEphemeral {
150162
ephemeral = true
151163
content = "Sorry, You already have the maximum amount of roles from this selector."
152164
}
153165
null
154-
} else ""
166+
}
155167
}
156168

157169
private suspend fun roleIncompatibleWithCheck(
@@ -191,6 +203,7 @@ class RoleSelector: Extension() {
191203
private suspend fun addRoleSelector(
192204
name: String,
193205
channel: Long,
206+
autoRemoveOnLimit: Boolean,
194207
attachment: String,
195208
limit: Int,
196209
removable: Boolean,
@@ -210,14 +223,16 @@ class RoleSelector: Extension() {
210223

211224
publicButton {
212225
label = role["name"] as String
213-
val emoji = (role["emoji"] as String).split(':', limit = 3)
226+
if((role["emoji"] as String).isNotEmpty()) {
227+
val emoji = (role["emoji"] as String).split(':', limit = 3)
214228

215-
partialEmoji = DiscordPartialEmoji.dsl {
216-
id = Snowflake(emoji[0])
217-
this.name = emoji[1]
218-
animated = OptionalBoolean.Value(emoji[2].parseBoolean(Locale.ENGLISH) ?: false)
229+
partialEmoji = DiscordPartialEmoji.dsl {
230+
id = Snowflake(emoji[0])
231+
this.name = emoji[1]
232+
animated = OptionalBoolean.Value(emoji[2].parseBoolean(Locale.ENGLISH) ?: false)
233+
}
219234
}
220-
style = ButtonStyle.Primary
235+
style = ButtonStyle.Secondary
221236

222237
val incompatibleWith = (role["incompatibleWith"] as TomlArray).map {
223238
RoleBehavior(GUILD_ID, Snowflake(it.toString()), kord)
@@ -235,6 +250,7 @@ class RoleSelector: Extension() {
235250

236251
roleLimitCheck(
237252
limit,
253+
autoRemoveOnLimit,
238254
member,
239255
selectorRoles,
240256
this,
@@ -292,4 +308,6 @@ inline fun DiscordPartialEmoji.Companion.dsl(block: DiscordPartialEmojiDSL.() ->
292308
val dsl = DiscordPartialEmojiDSL()
293309
dsl.block()
294310
return dsl.build()
295-
}
311+
}
312+
313+
operator fun TomlTable.get(key: String, defaultValue: Any): Any = getOrDefault(key, defaultValue)

0 commit comments

Comments
 (0)