-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55eec6c
commit 1d8c3a6
Showing
4 changed files
with
92 additions
and
70 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...src/main/kotlin/tw/waterballsa/utopia/managesubscriberole/AbstractManageSubscriberRole.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...le/src/main/kotlin/tw/waterballsa/utopia/managesubscriberole/AddSubscriberRoleListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}\" }" } | ||
} | ||
} | ||
} | ||
} |
70 changes: 0 additions & 70 deletions
70
.../src/main/kotlin/tw/waterballsa/utopia/managesubscriberole/ManageSubscribeRoleListener.kt
This file was deleted.
Oops, something went wrong.
24 changes: 24 additions & 0 deletions
24
...src/main/kotlin/tw/waterballsa/utopia/managesubscriberole/RemoveSubscriberRoleListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}\" }" } | ||
} | ||
} | ||
} | ||
} |