Skip to content

Commit 5accf91

Browse files
committed
Add kickUser and unbanUser to AdminUtils
1 parent 2d63477 commit 5accf91

File tree

2 files changed

+129
-23
lines changed

2 files changed

+129
-23
lines changed

foxy/src/main/kotlin/net/cakeyfox/foxy/interactions/vanilla/moderation/BanExecutor.kt

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,33 @@ class BanExecutor : UnleashedCommandExecutor() {
3232
} else emptyList()
3333

3434
val userAsSnowflakes = validUsers.map { UserSnowflake.fromId(it.idLong) }
35-
val validUserUsernames = validUsers.map { "`${it.effectiveName} (${it.id})`" }
35+
val validUserUsernames = validUsers.map { "`@${it.name} (${it.id})`" }
36+
val durationToDiscordTimestamp = context.utils.convertISOToExtendedDiscordTimestamp(
37+
Instant.fromEpochMilliseconds(
38+
Clock.System.now().toEpochMilliseconds() + durationInMs
39+
)
40+
)
41+
3642

3743
if (!skipConfirmation) {
3844
context.reply {
39-
embed {
40-
color = Colors.FOXY_DEFAULT
41-
description = pretty(
45+
content = if (durationInMs > 0) {
46+
pretty(
4247
FoxyEmotes.FoxyBan,
4348
context.locale[
4449
"ban.confirmPunishment",
50+
validUserUsernames.joinToString(", "),
51+
durationToDiscordTimestamp
52+
]
53+
)
54+
} else {
55+
pretty(
56+
FoxyEmotes.FoxyBan,
57+
context.locale[
58+
"ban.confirmPermanentPunishment",
4559
validUserUsernames.joinToString(", ")
4660
]
4761
)
48-
footer(context.locale["ban.canTakeALongTimeToBan"])
4962
}
5063

5164
actionRow(
@@ -67,6 +80,16 @@ class BanExecutor : UnleashedCommandExecutor() {
6780
)
6881
}
6982

83+
it.reply(true) {
84+
content = pretty(
85+
FoxyEmotes.FoxyBan,
86+
context.locale[
87+
"ban.bannedUsers",
88+
validUserUsernames.joinToString(", ")
89+
]
90+
)
91+
}
92+
7093
banUsers(
7194
context.foxy,
7295
context.guildId!!,
@@ -80,17 +103,13 @@ class BanExecutor : UnleashedCommandExecutor() {
80103
}
81104
} else {
82105
context.reply {
83-
embed {
84-
color = Colors.FOXY_DEFAULT
85-
description = pretty(
86-
FoxyEmotes.FoxyBan,
87-
context.locale[
88-
"ban.banWithoutConfirmation",
89-
validUserUsernames.joinToString(", ")
90-
]
91-
)
92-
footer(context.locale["ban.canTakeALongTimeToBan"])
93-
}
106+
content = pretty(
107+
FoxyEmotes.FoxyBan,
108+
context.locale[
109+
"ban.bannedUsers",
110+
validUserUsernames.joinToString(", ")
111+
]
112+
)
94113
}
95114

96115
banUsers(

foxy/src/main/kotlin/net/cakeyfox/foxy/utils/AdminUtils.kt

Lines changed: 94 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import net.cakeyfox.common.FoxyEmotes
1111
import net.cakeyfox.foxy.FoxyInstance
1212
import net.cakeyfox.foxy.database.data.guild.Guild
1313
import net.cakeyfox.foxy.database.data.guild.TempBan
14+
import net.cakeyfox.foxy.interactions.commands.CommandContext
1415
import net.cakeyfox.foxy.interactions.pretty
1516
import net.cakeyfox.foxy.utils.PlaceholderUtils.getModerationPlaceholders
1617
import net.cakeyfox.foxy.utils.discord.DiscordMessageUtils.getMessageFromJson
@@ -24,6 +25,7 @@ import net.dv8tion.jda.api.entities.UserSnowflake
2425
import net.dv8tion.jda.api.exceptions.ErrorResponseException
2526
import net.dv8tion.jda.api.exceptions.RateLimitedException
2627
import net.dv8tion.jda.api.requests.ErrorResponse
28+
import kotlin.math.log
2729

2830
object 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

Comments
 (0)