Skip to content

Commit

Permalink
♻️ 用表情訂閱/解除訂閱機制
Browse files Browse the repository at this point in the history
  • Loading branch information
WayneTsou85 authored and Zouwayne committed Nov 5, 2023
1 parent 55eec6c commit 1d8c3a6
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package tw.waterballsa.utopia.managesubscriberole

import net.dv8tion.jda.api.entities.Guild
import net.dv8tion.jda.api.entities.Role
import net.dv8tion.jda.api.entities.UserSnowflake
import net.dv8tion.jda.api.events.message.react.GenericMessageReactionEvent
import tw.waterballsa.utopia.commons.config.WsaDiscordProperties
import tw.waterballsa.utopia.jda.UtopiaListener

abstract class AbstractManageSubscriberRole(private val wsa: WsaDiscordProperties): UtopiaListener() {

private val emojiToSubscriberRoleIdMap = hashMapOf<String, String>()

/**
* "1️⃣":軟體英文派對訂閱者
* "2️⃣":遊戲微服務計畫訂閱者
* "3️⃣":純函式咖啡訂閱者
* "4️⃣":技術演講吐司會訂閱者
* "7️⃣":CS Lab 訂閱者
*/
init {
emojiToSubscriberRoleIdMap["1️⃣"] = wsa.wsaSECSubsriberMemberRoleId
emojiToSubscriberRoleIdMap["2️⃣"] = wsa.wsaGaaSSubscriberRoleId
emojiToSubscriberRoleIdMap["3️⃣"] = wsa.wsaPurefuncSubscriberRoleId
emojiToSubscriberRoleIdMap["4️⃣"] = wsa.wsaTTMSubscriberRoleId
emojiToSubscriberRoleIdMap["7️⃣"] = wsa.wsaCSLabSubscriberRoleId
}

/**
* 管理訂閱者身份組
*/
protected fun <T: GenericMessageReactionEvent> manageSubscriberRole(event: T,
handleSubscriberRole: (guild: Guild, userSnowflake: UserSnowflake, role: Role) -> Unit) {
with(event) {
if (messageId != wsa.manageSubscriberRoleMessageId) {
return
}
emojiToSubscriberRoleIdMap[emoji.name]?.run {
val user = UserSnowflake.fromId(userId)
val role = jda.getRoleById(this)?: return
handleSubscriberRole(guild, user, role)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tw.waterballsa.utopia.managesubscriberole

import mu.KotlinLogging
import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent
import org.springframework.stereotype.Component
import tw.waterballsa.utopia.commons.config.WsaDiscordProperties

private val logger = KotlinLogging.logger {}
@Component
class AddSubscriberRoleListener(wsa : WsaDiscordProperties) : AbstractManageSubscriberRole(wsa) {

/**
* 移除表情時,新增對應訂閱者身份組
*/
override fun onMessageReactionRemove(event: MessageReactionRemoveEvent) {
manageSubscriberRole(event) { guild, user, role ->
guild.addRoleToMember(user, role)
.queue {
logger.info { "[Add Role] {\"userId\":\"${user.id}\", \"roleName\":\"${role.name}\" }" }
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tw.waterballsa.utopia.managesubscriberole

import mu.KotlinLogging
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent
import org.springframework.stereotype.Component
import tw.waterballsa.utopia.commons.config.WsaDiscordProperties

private val logger = KotlinLogging.logger {}

@Component
class RemoveSubscriberRoleListener(wsa : WsaDiscordProperties) : AbstractManageSubscriberRole(wsa) {

/**
* 新增表情,移除對應訂閱者身份組
*/
override fun onMessageReactionAdd(event: MessageReactionAddEvent) {
manageSubscriberRole(event) { guild, user, role ->
guild.removeRoleFromMember(user, role)
.queue {
logger.info { "[Remove Role] {\"userId\":\"${user.id}\", \"roleName\":\"${role.name}\" }" }
}
}
}
}

0 comments on commit 1d8c3a6

Please sign in to comment.