@@ -11,6 +11,7 @@ import net.cakeyfox.common.FoxyEmotes
1111import net.cakeyfox.foxy.FoxyInstance
1212import net.cakeyfox.foxy.database.data.guild.Guild
1313import net.cakeyfox.foxy.database.data.guild.TempBan
14+ import net.cakeyfox.foxy.interactions.commands.CommandContext
1415import net.cakeyfox.foxy.interactions.pretty
1516import net.cakeyfox.foxy.utils.PlaceholderUtils.getModerationPlaceholders
1617import net.cakeyfox.foxy.utils.discord.DiscordMessageUtils.getMessageFromJson
@@ -24,6 +25,7 @@ import net.dv8tion.jda.api.entities.UserSnowflake
2425import net.dv8tion.jda.api.exceptions.ErrorResponseException
2526import net.dv8tion.jda.api.exceptions.RateLimitedException
2627import net.dv8tion.jda.api.requests.ErrorResponse
28+ import kotlin.math.log
2729
2830object AdminUtils {
2931 private val logger = KotlinLogging .logger {}
@@ -68,19 +70,100 @@ object AdminUtils {
6870 } catch (e: RateLimitedException ) {
6971 logger.warn { " Rate limited while unbanning ${expiredBan.userId} . Retrying in ${e.retryAfter} ms" }
7072 delay(e.retryAfter)
71- } catch (e: Exception ) {
72- if (e.message?.startsWith(" 10026" ) == true ) {
73- logger.warn { " Ban not found for ${expiredBan.userId} " }
74- foxy.database.guild.removeTempBanFromGuild(guildId, expiredBan.userId)
75- } else {
76- logger.error(e) { " Error while unbanning ${expiredBan.userId} " }
73+ } catch (e: ErrorResponseException ) {
74+ when (e.errorResponse) {
75+ ErrorResponse .UNKNOWN_BAN -> {
76+ logger.warn { " Ban not found for ${expiredBan.userId} on guild ${guildId} " }
77+ foxy.database.guild.removeTempBanFromGuild(guildId, expiredBan.userId)
78+ }
79+
80+ else -> logger.error(e) { " Error while unbanning ${expiredBan.userId} " }
7781 }
82+ } catch (e: Exception ) {
83+ logger.error(e) { " Error while unbanning ${expiredBan.userId} " }
7884 }
7985
8086 delay(50L )
8187 }
8288 }
8389
90+ suspend fun unbanUser (
91+ context : CommandContext ,
92+ user : User ,
93+ reason : String ,
94+ staff : User
95+ ): Boolean {
96+ val guildId = context.guildId!!
97+ val foxy = context.foxy
98+ val guild = foxy.shardManager.getGuildById(guildId) ? : return false
99+ val guildData = foxy.database.guild.getGuild(guildId)
100+ val isTempBan = guildData.tempBans?.any { it.userId == user.id } ? : false
101+
102+ if (isTempBan) context.database.guild.removeTempBanFromGuild(guildId, user.id)
103+
104+ try {
105+ guild.unban(user)
106+ .reason(reason + " - ${staff.name} (${staff.id} )" )
107+ .await()
108+
109+ val placeholders = getModerationPlaceholders(
110+ foxy,
111+ staff,
112+ user,
113+ guild,
114+ duration = null ,
115+ reason,
116+ foxy.locale[" KICK" ]
117+ )
118+
119+ sendMessage(foxy, guildData, placeholders, guild)
120+
121+ return true
122+ } catch (e: ErrorResponseException ) {
123+ when (e.errorResponse) {
124+ ErrorResponse .UNKNOWN_BAN -> {
125+ return false
126+ }
127+
128+ else -> {
129+ logger.error(e) { " Error while unbanning ${user.id} " }
130+ return false
131+ }
132+ }
133+ }
134+ }
135+
136+ suspend fun kickUser (
137+ foxy : FoxyInstance ,
138+ guildId : String ,
139+ user : User ,
140+ staff : User ,
141+ reason : String
142+ ) {
143+ val guild = foxy.shardManager.getGuildById(guildId) ? : return
144+ val guildData = foxy.database.guild.getGuild(guildId)
145+
146+ try {
147+ guild.kick(user)
148+ .reason(reason)
149+ .await()
150+
151+ val placeholders = getModerationPlaceholders(
152+ foxy,
153+ staff,
154+ user,
155+ guild,
156+ duration = null ,
157+ reason,
158+ foxy.locale[" KICK" ]
159+ )
160+
161+ sendMessage(foxy, guildData, placeholders, guild)
162+ } catch (e: Exception ) {
163+ logger.error(e) { " Error while kicking user ${user.id} " }
164+ }
165+ }
166+
84167 suspend fun banUsers (
85168 foxy : FoxyInstance ,
86169 guildId : String ,
@@ -139,9 +222,13 @@ object AdminUtils {
139222 // Already banned users can catch at this
140223 }
141224
225+ ErrorResponse .SERVER_ERROR -> {
226+ logger.warn { " The ban was processed by Discord, but I received a 5xx error from the server" }
227+ }
228+
142229 else -> {
143230 logger.error(e) {
144- " Mass ban failed | guild=${guild.id} | users=${userAsSnowflakes.size} | staff=${staff.id} "
231+ " Mass ban failed | guild=${guild.id} | users=${userAsSnowflakes.size} | staff=${staff.id} | ${e.errorResponse} "
145232 }
146233 }
147234 }
0 commit comments