1
1
package io.github.arkosammy12.compsmpdiscordbot
2
2
3
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
4
import dev.kordex.core.ExtensibleBot
10
- import kotlinx.coroutines.flow.filter
5
+ import io.github.arkosammy12.compsmpdiscordbot.config.ConfigUtils
6
+ import io.github.arkosammy12.monkeyconfig.base.ConfigManager
7
+ import io.github.arkosammy12.monkeyconfig.builders.tomlConfigManager
8
+ import io.github.arkosammy12.monkeyconfig.managers.getRawNumberSettingValue
9
+ import io.github.arkosammy12.monkeyconfig.managers.getRawStringSettingValue
10
+ import io.github.arkosammy12.monkeyutils.registrars.DefaultConfigRegistrar
11
+ import kotlinx.coroutines.Job
11
12
import kotlinx.coroutines.runBlocking
12
- import net.fabricmc.api.ModInitializer
13
+ import net.fabricmc.api.DedicatedServerModInitializer
14
+ import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents
15
+ import net.fabricmc.loader.api.FabricLoader
16
+ import net.minecraft.server.MinecraftServer
17
+ import org.slf4j.Logger
13
18
import org.slf4j.LoggerFactory
14
19
15
- object CompSMPDiscordBot : ModInitializer {
20
+ object CompSMPDiscordBot : DedicatedServerModInitializer {
16
21
17
22
const val MOD_ID : String = " compsmpdiscordbot"
18
- val LOGGER = LoggerFactory .getLogger(MOD_ID )
23
+ val LOGGER : Logger = LoggerFactory .getLogger(MOD_ID )
24
+ lateinit var guildSnowFlake: Snowflake
25
+ lateinit var bot: ExtensibleBot
26
+ lateinit var botJob: Job
19
27
20
- override fun onInitialize () {
21
-
22
- runBlocking {
23
-
24
- setupBot()
28
+ val CONFIG_MANAGER : ConfigManager = tomlConfigManager(MOD_ID , FabricLoader .getInstance().configDir.resolve(" compsmpdiscordbot.toml" )) {
29
+ ConfigUtils .BOT_TOKEN = stringSetting(" bot_token" , " 0" ) {
25
30
26
31
}
27
32
33
+ ConfigUtils .GUILD_ID = numberSetting(" guild_id" , 0L ) {
28
34
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
35
}
36
+ ConfigUtils .COMPSMP_ADMIN_ROLE_ID = numberSetting(" compsmp_admin_role_id" , 0L ) {
51
37
52
- extensions {
53
- add(::TestExtension)
54
38
}
55
39
56
- }
57
- bot.on<MessageCreateEvent> {
58
- // ignore other bots, even ourselves. We only serve humans here!
59
- if (message.author?.isBot != false) return@on
40
+ section(" application_approval" ) {
41
+ ConfigUtils .APPLICATION_CHANNEL_ID = numberSetting(" application_channel_id" , 0L ) {
60
42
61
- // check if our command is being invoked
62
- if (message.content != "!ping") return@on
43
+ }
44
+ ConfigUtils . APPROVAL_ROLE_ID = numberSetting( " approval_role_id " , 0L ) {
63
45
64
- // all clear, give them the pong!
65
- message.channel.createMessage("pong!")
46
+ }
47
+ ConfigUtils . APPROVAL_EMOJI_ID = numberSetting( " approval_emoji_id " , 0L ) {
66
48
67
- }
49
+ }
50
+ ConfigUtils .APPROVAL_EMOJI_NAME = stringSetting(" approval_emoji_name" , " Approved" ) {
68
51
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
- }
52
+ }
107
53
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
54
}
162
55
163
- bot.startAsync()
56
+ }
57
+
58
+ override fun onInitializeServer () {
59
+ DefaultConfigRegistrar .registerConfigManager(CONFIG_MANAGER )
60
+ ServerLifecycleEvents .SERVER_STARTING .register(::onServerStarting)
61
+ ServerLifecycleEvents .SERVER_STOPPING .register(::onServerStopping)
62
+ val guildId: Long = CONFIG_MANAGER .getRawNumberSettingValue(ConfigUtils .GUILD_ID )!!
63
+ guildSnowFlake = Snowflake (guildId)
64
+ val token: String = CONFIG_MANAGER .getRawStringSettingValue(ConfigUtils .BOT_TOKEN )!!
65
+ bot = Bot .createBot(token)
66
+ }
67
+
68
+ private fun onServerStarting (server : MinecraftServer ) {
69
+ botJob = bot.startAsync()
70
+ }
164
71
165
- */
72
+ private fun onServerStopping (server : MinecraftServer ) {
73
+ runBlocking {
74
+ bot.close()
75
+ }
166
76
167
77
}
78
+
79
+
168
80
}
0 commit comments