Skip to content

Commit 2c3bf65

Browse files
committed
Implement ApprovalExtension.kt
1 parent 408afe6 commit 2c3bf65

File tree

10 files changed

+264
-342
lines changed

10 files changed

+264
-342
lines changed

build.gradle.kts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ repositories {
2626
maven("https://oss.sonatype.org/content/repositories/snapshots")
2727
maven("https://maven.enjarai.dev/releases")
2828
maven("https://maven.nucleoid.xyz")
29+
maven("https://api.modrinth.com/maven")
2930
}
3031

3132
loom {
@@ -49,6 +50,12 @@ dependencies {
4950

5051
// Monkey Config
5152
include(implementation("io.github.arkosammy12:monkey-config:${property("monkey_config_version")}")!!)
53+
54+
// Monkey Utils
55+
modImplementation("maven.modrinth:uApL7Qhc:${property("monkey_utils_version")}")
56+
57+
58+
include(modImplementation("me.lucko:fabric-permissions-api:0.3.1")!!)
5259
}
5360

5461
kordEx {

gradle.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ maven_group=io.github.arkosammy12
1919
archives_base_name=compsmpdiscordbot
2020

2121
# Dependencies
22-
monkey_config_version=1.0.5
22+
monkey_config_version=1.0.5
23+
monkey_utils_version=1.0.2

src/main/java/io/github/arkosammy12/compsmpdiscordbot/mixin/ExampleMixin.java

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.github.arkosammy12.compsmpdiscordbot
2+
3+
import dev.kord.gateway.Intent
4+
import dev.kord.gateway.PrivilegedIntent
5+
import dev.kordex.core.ExtensibleBot
6+
import io.github.arkosammy12.compsmpdiscordbot.extensions.ApprovalExtension
7+
import kotlinx.coroutines.runBlocking
8+
9+
object Bot {
10+
11+
fun createBot(token: String): ExtensibleBot {
12+
return runBlocking {
13+
ExtensibleBot(token) {
14+
chatCommands {
15+
16+
}
17+
extensions {
18+
add { ApprovalExtension("approval_extension") }
19+
}
20+
about {
21+
22+
}
23+
hooks {
24+
25+
}
26+
@OptIn(PrivilegedIntent::class)
27+
intents {
28+
+Intent.GuildMembers
29+
+Intent.GuildMessageReactions
30+
+Intent.MessageContent
31+
}
32+
cache {
33+
34+
}
35+
}
36+
}
37+
}
38+
39+
}
Lines changed: 51 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,168 +1,80 @@
11
package io.github.arkosammy12.compsmpdiscordbot
22

33
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
94
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
1112
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
1318
import org.slf4j.LoggerFactory
1419

15-
object CompSMPDiscordBot : ModInitializer {
20+
object CompSMPDiscordBot : DedicatedServerModInitializer {
1621

1722
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
1927

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") {
2530

2631
}
2732

33+
ConfigUtils.GUILD_ID = numberSetting("guild_id", 0L) {
2834

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-
}
5035
}
36+
ConfigUtils.COMPSMP_ADMIN_ROLE_ID = numberSetting("compsmp_admin_role_id", 0L) {
5137

52-
extensions {
53-
add(::TestExtension)
5438
}
5539

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) {
6042

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) {
6345

64-
// all clear, give them the pong!
65-
message.channel.createMessage("pong!")
46+
}
47+
ConfigUtils.APPROVAL_EMOJI_ID = numberSetting("approval_emoji_id", 0L) {
6648

67-
}
49+
}
50+
ConfigUtils.APPROVAL_EMOJI_NAME = stringSetting("approval_emoji_name", "Approved") {
6851

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+
}
10753

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-
}
16154
}
16255

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+
}
16471

165-
*/
72+
private fun onServerStopping(server: MinecraftServer) {
73+
runBlocking {
74+
bot.close()
75+
}
16676

16777
}
78+
79+
16880
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.github.arkosammy12.compsmpdiscordbot.config
2+
3+
import io.github.arkosammy12.monkeyconfig.util.ElementPath
4+
5+
object ConfigUtils {
6+
lateinit var BOT_TOKEN: ElementPath
7+
lateinit var GUILD_ID: ElementPath
8+
lateinit var COMPSMP_ADMIN_ROLE_ID: ElementPath
9+
10+
lateinit var APPLICATION_CHANNEL_ID: ElementPath
11+
lateinit var APPROVAL_ROLE_ID: ElementPath
12+
lateinit var APPROVAL_EMOJI_ID: ElementPath
13+
lateinit var APPROVAL_EMOJI_NAME: ElementPath
14+
}

src/main/kotlin/io/github/arkosammy12/compsmpdiscordbot/config/Reference.kt

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)