diff --git a/manage-subscriber-role/src/main/kotlin/tw/waterballsa/utopia/managesubscriberole/AbstractManageSubscriberRole.kt b/manage-subscriber-role/src/main/kotlin/tw/waterballsa/utopia/managesubscriberole/AbstractManageSubscriberRole.kt new file mode 100644 index 00000000..2527664c --- /dev/null +++ b/manage-subscriber-role/src/main/kotlin/tw/waterballsa/utopia/managesubscriberole/AbstractManageSubscriberRole.kt @@ -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() + + /** + * "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 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) + } + } + } +} diff --git a/manage-subscriber-role/src/main/kotlin/tw/waterballsa/utopia/managesubscriberole/AddSubscriberRoleListener.kt b/manage-subscriber-role/src/main/kotlin/tw/waterballsa/utopia/managesubscriberole/AddSubscriberRoleListener.kt new file mode 100644 index 00000000..a7c57cb4 --- /dev/null +++ b/manage-subscriber-role/src/main/kotlin/tw/waterballsa/utopia/managesubscriberole/AddSubscriberRoleListener.kt @@ -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}\" }" } + } + } + } +} diff --git a/manage-subscriber-role/src/main/kotlin/tw/waterballsa/utopia/managesubscriberole/ManageSubscribeRoleListener.kt b/manage-subscriber-role/src/main/kotlin/tw/waterballsa/utopia/managesubscriberole/ManageSubscribeRoleListener.kt deleted file mode 100644 index d59dd71a..00000000 --- a/manage-subscriber-role/src/main/kotlin/tw/waterballsa/utopia/managesubscriberole/ManageSubscribeRoleListener.kt +++ /dev/null @@ -1,70 +0,0 @@ -package tw.waterballsa.utopia.managesubscriberole - -import mu.KotlinLogging -import net.dv8tion.jda.api.entities.UserSnowflake -import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent -import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent -import org.springframework.stereotype.Component -import tw.waterballsa.utopia.commons.config.WsaDiscordProperties -import tw.waterballsa.utopia.jda.UtopiaListener - -private val logger = KotlinLogging.logger {} -@Component -class ManageSubscribeRoleListener(private val wsa: WsaDiscordProperties) : UtopiaListener() { - - private val emojiToSubscriberRoleIdMap = hashMapOf() - - /** - * "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 - } - - /** - * 新增表情,移除對應訂閱者身份組 - */ - override fun onMessageReactionAdd(event: MessageReactionAddEvent) { - with(event) { - if (messageId != wsa.manageSubscriberRoleMessageId) { - return - } - emojiToSubscriberRoleIdMap[emoji.name]?.run { - val user = UserSnowflake.fromId(userId) - val role = jda.getRoleById(this)?: return - guild.removeRoleFromMember(user, role) - .queue { - logger.info { "[Remove Role] {\"userId\":\"${user.id}\", \"roleName\":\"${role.name}\" }" } - } - } - } - } - - /** - * 移除表情,新增對應訂閱者身份組 - */ - override fun onMessageReactionRemove(event: MessageReactionRemoveEvent) { - with(event) { - if (messageId != wsa.manageSubscriberRoleMessageId) { - return - } - emojiToSubscriberRoleIdMap[emoji.name]?.run { - val user = UserSnowflake.fromId(userId) - val role = jda.getRoleById(this)?: return - guild.addRoleToMember(user, role) - .queue { - logger.info { "[Add Role] {\"userId\":\"${user.id}\", \"roleName\":\"${role.name}\" }" } - } - } - } - } - -} diff --git a/manage-subscriber-role/src/main/kotlin/tw/waterballsa/utopia/managesubscriberole/RemoveSubscriberRoleListener.kt b/manage-subscriber-role/src/main/kotlin/tw/waterballsa/utopia/managesubscriberole/RemoveSubscriberRoleListener.kt new file mode 100644 index 00000000..12890147 --- /dev/null +++ b/manage-subscriber-role/src/main/kotlin/tw/waterballsa/utopia/managesubscriberole/RemoveSubscriberRoleListener.kt @@ -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}\" }" } + } + } + } +}