Skip to content

Commit

Permalink
Merge pull request #24 from the-programmers-hangout/feat/allow-multip…
Browse files Browse the repository at this point in the history
…le-roles

feat: allow reaction roles to only allow 1 role choice
  • Loading branch information
ddivad195 authored Mar 29, 2024
2 parents 86c46fc + 71ef09f commit ba152f9
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 15 deletions.
6 changes: 3 additions & 3 deletions commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
| nick | LowerMemberArg, Theme, [Nickname] | Set a member's nickname |

## ReactionRole
| Commands | Arguments | Description |
|--------------------|-------------------------|------------------------------|
| createReactionRole | Roles, EmbedDescription | Create a reaction role embed |
| Commands | Arguments | Description |
|--------------------|------------------------------------------|------------------------------|
| createReactionRole | Roles, EmbedDescription, [AllowMultiple] | Create a reaction role embed |

## Utility
| Commands | Arguments | Description |
Expand Down
13 changes: 8 additions & 5 deletions src/main/kotlin/me/ddivad/hawk/commands/ReactionRoleCommands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import me.ddivad.hawk.dataclasses.ReactionRole
import me.ddivad.hawk.embeds.createReactionRoleMenu
import me.ddivad.hawk.services.LoggingService
import me.ddivad.hawk.services.buildGuildLogMessage
import me.jakejmattson.discordkt.arguments.BooleanArg
import me.jakejmattson.discordkt.arguments.EveryArg
import me.jakejmattson.discordkt.commands.commands
import me.jakejmattson.discordkt.dsl.edit
Expand All @@ -19,18 +20,20 @@ fun reactionRoleCommands(configuration: Configuration, loggingService: LoggingSe
slash("createReactionRole", "Create a reaction role embed", Permissions.ADMINISTRATOR) {
execute(
EveryArg("Roles", "Role IDs to be added. If using multiple roles, separate IDs with a space"),
EveryArg("EmbedDescription", "Text to be added to reaction role embed")
EveryArg("EmbedDescription", "Text to be added to reaction role embed"),
BooleanArg("AllowMultiple", "yes", "no", "Allow more than 1 role to be chosen. Defualts to yes").optional(true)
) {
val (roleIds, descriptionText) = args
val (roleIds, descriptionText, allowMultiple) = args
try {
val roles = roleIds.split(" ").map { guild.getRole(it.toSnowflake()) }
val guildConfig = configuration[guild.id] ?: return@execute
val roles = roleIds.split(" ").map { guild.getRole(it.toSnowflake()) }
val reactionRole = ReactionRole(
guildConfig.reactionRoles.size + 1,
descriptionText,
roles.map { it.id }.toMutableList(),
null,
channel.id
channel.id,
allowMultiple
)
reactionRole.messageId = respondMenu {
createReactionRoleMenu(discord, guild, reactionRole)
Expand All @@ -40,7 +43,7 @@ fun reactionRoleCommands(configuration: Configuration, loggingService: LoggingSe
}
respond("Reaction role created")
} catch (e: Exception) {
respond("Error parsing roles. Make sure IDs $roleIds are valid roles and separated by ` ` if adding multiple roles.")
respond("Error parsing roles. Make sure role IDs $roleIds are valid roles and separated by ` ` if adding multiple roles.")
logger.error(e) { buildGuildLogMessage(guild, "Failed to parse roles") }
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/me/ddivad/hawk/dataclasses/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,6 @@ data class ReactionRole(
val description: String,
val roles: MutableList<Snowflake>,
var messageId: Snowflake?,
val channel: Snowflake
val channel: Snowflake,
val allowMultiple: Boolean = true
)
13 changes: 10 additions & 3 deletions src/main/kotlin/me/ddivad/hawk/embeds/ReactionRole.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ suspend fun MenuBuilder.createReactionRoleMenu(discord: Discord, guild: Guild, r
}
buttons {
runBlocking {
reactionRole.roles.forEach {
val liveRole = guild.getRole(it)
reactionRole.roles.forEach { role ->
val liveRole = guild.getRole(role)

actionButton(liveRole.name, null) {
val member = guild.getMemberOrNull(this.user.id) ?: return@actionButton
if (!member.roles.toList().contains(liveRole)) {
if (!member.roleIds.contains(role)) {
if (!reactionRole.allowMultiple) {
reactionRole.roles.forEach { role ->
if (member.roleIds.contains(role)) {
member.removeRole(role)
}
}
}
member.addRole(liveRole.id, "Reaction button clicked")
respondEphemeral { content = "Assigned role ${liveRole.name}" }
loggingService.reactionRoleAdded(guild, member, liveRole)
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/me/ddivad/hawk/services/StartupService.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.ddivad.hawk.services

import dev.kord.core.behavior.getChannelOfOrNull
import dev.kord.core.entity.channel.GuildMessageChannel
import dev.kord.core.entity.channel.TextChannel
import me.ddivad.hawk.dataclasses.Configuration
import me.ddivad.hawk.embeds.createReactionRoleMenu
Expand Down Expand Up @@ -29,7 +30,7 @@ class StartupService(
}

guildConfig.reactionRoles.forEach {
val channel = guild.getChannelOfOrNull<TextChannel>(it.channel) ?: return
val channel = guild.getChannelOfOrNull<GuildMessageChannel>(it.channel) ?: return
val message = channel.getMessageOrNull(it.messageId!!) ?: return
logger.info {
buildGuildLogMessage(
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/bot.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Sat Apr 01 18:01:31 IST 2023
#Fri Mar 29 00:53:13 GMT 2024
name=Hawk
description=A bot to add and maintain a symbol as a prefix or suffix in staff names.\n
version=3.0.0-RC1
version=3.1
url=https\://github.com/the-programmers-hangout/Hawk/

0 comments on commit ba152f9

Please sign in to comment.