1+ package io.github.arkosammy12.compsmpdiscordbot
2+
3+ import dev.kord.common.entity.Snowflake
4+ import dev.kord.core.entity.Member
5+ import dev.kord.core.entity.ReactionEmoji
6+ import dev.kord.core.event.message.MessageCreateEvent
7+ import dev.kord.core.event.message.ReactionAddEvent
8+ import dev.kord.core.event.message.ReactionRemoveEvent
9+ import dev.kordex.core.ExtensibleBot
10+ import kotlinx.coroutines.flow.filter
11+ import kotlinx.coroutines.runBlocking
12+ import net.fabricmc.api.ModInitializer
13+ import org.slf4j.LoggerFactory
14+
15+ object CompSMPDiscordBot : ModInitializer {
16+
17+ const val MOD_ID : String = " compsmpdiscordbot"
18+ val LOGGER = LoggerFactory .getLogger(MOD_ID )
19+
20+ override fun onInitialize () {
21+
22+ runBlocking {
23+
24+ setupBot()
25+
26+ }
27+
28+
29+ }
30+ suspend fun setupBot () {
31+ /*
32+ val guildIdNum: Long = CONFIG_MANAGER.getSettingValue<Long, NumberSetting<Long>>(ConfigSettings.GUILD_ID.settingLocation)
33+ val guildSnowFlake: Snowflake = Snowflake(guildIdNum)
34+ val token: String = CONFIG_MANAGER.getSettingValue<String, StringSetting>(ConfigSettings.BOT_TOKEN.settingLocation)
35+ val bot: ExtensibleBot = ExtensibleBot(token) {
36+ chatCommands {
37+ defaultPrefix = "?"
38+ enabled = true
39+
40+ prefix { default ->
41+
42+ if (guildId == guildSnowFlake) {
43+ // For the test server, we use ! as the command prefix
44+ "!"
45+ } else {
46+ // For other servers, we use the configured default prefix
47+ default
48+ }
49+ }
50+ }
51+
52+ extensions {
53+ add(::TestExtension)
54+ }
55+
56+ }
57+ bot.on<MessageCreateEvent> {
58+ // ignore other bots, even ourselves. We only serve humans here!
59+ if (message.author?.isBot != false) return@on
60+
61+ // check if our command is being invoked
62+ if (message.content != "!ping") return@on
63+
64+ // all clear, give them the pong!
65+ message.channel.createMessage("pong!")
66+
67+ }
68+
69+ bot.on<ReactionAddEvent> {
70+
71+ println("Testing reacting add event")
72+
73+ val reactionEmoji: ReactionEmoji = this.emoji
74+ if (reactionEmoji !is ReactionEmoji.Custom) {
75+ return@on
76+ }
77+
78+ // Check if the reactor is an admin
79+ val reactor: Member = this.user.asMember(guildSnowFlake)
80+ val roles: Set<Snowflake> = reactor.roleIds
81+ val adminRoleId: Long =
82+ CompSMPDiscordBot.CONFIG_MANAGER.getSettingValue<Long, NumberSetting<Long>>(ConfigSettings.ADMIN_ROLE_ID.settingLocation)
83+ if (!roles.any { roleId -> roleId.value.toLong() == adminRoleId }) {
84+ return@on
85+ }
86+
87+ // Check if the message is in the applications channel
88+ val applicationChannelId: Long =
89+ CompSMPDiscordBot.CONFIG_MANAGER.getSettingValue<Long, NumberSetting<Long>>(ConfigSettings.APPROVAL_CHANNEL_ID.settingLocation)
90+ if (this.message.channelId.value.toLong() != applicationChannelId) {
91+ return@on
92+ }
93+
94+ // Check if the reacted emoji is the approved emoji
95+ val approvedEmojiId: Long =
96+ CompSMPDiscordBot.CONFIG_MANAGER.getSettingValue<Long, NumberSetting<Long>>(ConfigSettings.APPROVAL_EMOJI_ID.settingLocation)
97+ if (reactionEmoji.id.value.toLong() != approvedEmojiId) {
98+ return@on
99+ }
100+
101+ val applicant: Member = this.messageAuthor?.asMember(guildSnowFlake) ?: return@on
102+ val approvalRoleId: Long = CONFIG_MANAGER.getSettingValue<Long, NumberSetting<Long>>(ConfigSettings.APPROVAL_ROLE_ID.settingLocation)
103+
104+ CompSMPDiscordBot.LOGGER.info("Giving approval role to user: ${applicant.nickname}")
105+ applicant.addRole(Snowflake(approvalRoleId))
106+ }
107+
108+ bot.on<ReactionRemoveEvent> {
109+ println("Testing reaction remove event")
110+
111+ val reactionEmoji: ReactionEmoji = this.emoji
112+ if (reactionEmoji !is ReactionEmoji.Custom) {
113+ return@on
114+ }
115+
116+ val reactor: Member = this.user.asMember(guildSnowFlake)
117+ val roles: Set<Snowflake> = reactor.roleIds
118+ val adminRoleId: Long =
119+ CompSMPDiscordBot.CONFIG_MANAGER.getSettingValue<Long, NumberSetting<Long>>(ConfigSettings.ADMIN_ROLE_ID.settingLocation)
120+ if (!roles.any { roleId -> roleId.value.toLong() == adminRoleId }) {
121+ return@on
122+ }
123+
124+ val applicationChannelId: Long =
125+ CompSMPDiscordBot.CONFIG_MANAGER.getSettingValue<Long, NumberSetting<Long>>(ConfigSettings.APPROVAL_CHANNEL_ID.settingLocation)
126+ if (this.message.channelId.value.toLong() != applicationChannelId) {
127+ return@on
128+ }
129+
130+ val approvedEmojiId: Long =
131+ CompSMPDiscordBot.CONFIG_MANAGER.getSettingValue<Long, NumberSetting<Long>>(ConfigSettings.APPROVAL_EMOJI_ID.settingLocation)
132+ if (reactionEmoji.id.value.toLong() != approvedEmojiId) {
133+ return@on
134+ }
135+
136+ val applicant = this.message.asMessage().author?.asMember(guildSnowFlake) ?: return@on
137+ val applicantRoles: Set<Snowflake> = applicant.roleIds
138+ val approvalRoleId: Long = CONFIG_MANAGER.getSettingValue<Long, NumberSetting<Long>>(ConfigSettings.APPROVAL_ROLE_ID.settingLocation)
139+ val hasApprovedRole: Boolean = applicantRoles.any { role -> role.value.toLong() == approvalRoleId }
140+
141+ if (!hasApprovedRole) return@on
142+
143+ var hasApprovedRoleByOtherAdmin = false
144+
145+ runBlocking<Unit> {
146+ this@on.message.getReactors(ReactionEmoji.Custom(Snowflake(approvedEmojiId), "Approved", false))
147+ .filter { user ->
148+ user.id != this@on.userId
149+ }.collect { user ->
150+ val member = user.asMember(guildSnowFlake)
151+ if (member.roleIds.any { roleId -> roleId.value.toLong() == adminRoleId }) {
152+ hasApprovedRoleByOtherAdmin = true
153+ return@collect
154+ }
155+ }
156+ }
157+
158+ if (!hasApprovedRoleByOtherAdmin) {
159+ applicant.removeRole(Snowflake(approvalRoleId))
160+ }
161+ }
162+
163+ bot.startAsync()
164+
165+ */
166+
167+ }
168+ }
0 commit comments