Skip to content

Commit

Permalink
Added support for multiple user unbans and merged /checkdaily users o…
Browse files Browse the repository at this point in the history
…ptions.
  • Loading branch information
MrPowerGamerBR authored Sep 11, 2024
2 parents 9d3c9e2 + 0db2a99 commit 6c98bc3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ class DailyCheckCommand(val helper: LorittaHelper) : SlashCommandDeclarationWrap

inner class DailyCheckExecutor : LorittaSlashCommandExecutor() {
inner class Options : ApplicationCommandOptions() {
init {
// Register 25 different users
repeat(25) {
optionalUser("user${it + 1}", "Usuário para ver os dailies")
}
}
val userIds = string("user_ids", "ID do usuário que você deseja ver os dailies (pode ser vários)")
}

override val options = Options()
Expand All @@ -81,9 +76,12 @@ class DailyCheckCommand(val helper: LorittaHelper) : SlashCommandDeclarationWrap
context.deferChannelMessage(true)

// Because we did stuff in a... unconventional way, we will get all matched user arguments in a unconventional way: By getting all resolved objects!
val users = context.event.options.map { it.asUser }
val usersIds = args[options.userIds]
.split(" ")
.mapNotNull { it.toLongOrNull() }
.toSet()

if (users.isEmpty()) {
if (usersIds.isEmpty()) {
context.reply(true) {
content = "Nenhum usuário encontrado!"
}
Expand All @@ -96,12 +94,13 @@ class DailyCheckCommand(val helper: LorittaHelper) : SlashCommandDeclarationWrap

val dailies = transaction(helper.databases.lorittaDatabase) {
Dailies.leftJoin(BrowserFingerprints).select {
Dailies.receivedById inList users.map { it.idLong }
Dailies.receivedById inList usersIds
}.orderBy(Dailies.id, SortOrder.DESC)
.toList()
}

val builder = StringBuilder()
val cachedUserData = mutableMapOf<Long, User>()

for (daily in dailies) {
val whenTheTransactionHappened = Instant.ofEpochMilli(daily[Dailies.receivedAt])
Expand All @@ -116,7 +115,14 @@ class DailyCheckCommand(val helper: LorittaHelper) : SlashCommandDeclarationWrap
emote
}

val userData = users.find { it.idLong == daily[Dailies.receivedById] }
val userId = daily[Dailies.receivedById]
val userData = cachedUserData[userId] ?: try {
val user = context.event.jda.retrieveUserById(userId).await()
cachedUserData[userId] = user
user
} catch (e: Exception) {
null
}

builder.append("${userEmote} [${whenTheTransactionHappened.format(Constants.PRETTY_DATE_FORMAT)}] ${userData?.name} [${userData?.globalName}] (${daily[Dailies.receivedById]})")
builder.append("\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,17 +301,22 @@ class LoriToolsCommand(val helper: LorittaHelper) : SlashCommandDeclarationWrapp

class LoriUnbanExecutor(helper: LorittaHelper) : HelperExecutor(helper, PermissionLevel.ADMIN) {
inner class Options : ApplicationCommandOptions() {
val userId = string("user_ids", "ID do usuário que você deseja desbanir")
val userIds = string("user_ids", "ID do usuário que você deseja desbanir (pode ser vários)")

val reason = string("reason", "Motivo que irá aparecer no ban")
val reason = string("reason", "Motivo que irá aparecer no unban")
}

override val options = Options()

override suspend fun executeHelper(context: ApplicationCommandContext, args: SlashCommandArguments) {
context.deferChannelMessage(true)

val userId = args[options.userId].toLongOrNull() ?: run {
val userIds = args[options.userIds]
.split(" ")
.mapNotNull { it.toLongOrNull() }
.toSet()

if (userIds.isEmpty()) {
context.reply(true) {
content = "Você não colocou um ID válido... <:lori_sob:556524143281963008>"
}
Expand All @@ -320,67 +325,86 @@ class LoriToolsCommand(val helper: LorittaHelper) : SlashCommandDeclarationWrapp

val reason = args[options.reason]

val result = transaction(helper.databases.lorittaDatabase) {
val results = mutableListOf<UnbanResult>()
transaction(helper.databases.lorittaDatabase) {
// Checks if the user has any valid bans
BannedUsers.select {
BannedUsers.userId eq userId and
val currentBanStatuses = BannedUsers.select {
BannedUsers.userId inList userIds and
(BannedUsers.valid eq true) and
(
BannedUsers.expiresAt.isNull()
or
(BannedUsers.expiresAt.isNotNull() and (BannedUsers.expiresAt greaterEq System.currentTimeMillis())))
}
.orderBy(BannedUsers.bannedAt, SortOrder.DESC)
.limit(1)
.firstOrNull() ?: return@transaction LoriUnbanExecutor.UserIsNotBannedResult
.toList()

val banId = BannedUsers.update({ BannedUsers.userId eq userId }) {
it[BannedUsers.valid] = false
val bannedUsersIds = currentBanStatuses.map { it[BannedUsers.userId] }
val nonBannedUsers = userIds.filter { it !in bannedUsersIds }

for (userId in nonBannedUsers) {
results.add(UserIsNotBannedResult(userId))
}

UserUnbannedResult
for (userId in bannedUsersIds) {
val banId = BannedUsers.update({ BannedUsers.userId eq userId }) {
it[BannedUsers.valid] = false
}

results.add(UserUnbannedResult(userId, reason))
}
}

when (result) {
is UserUnbannedResult -> {
context.reply(true) {
content = "Usuário $userId (<@$userId>) foi desbanido com sucesso. Obrigada por ter corrigido a cagada de alguém... eu acho né... <:lori_coffee:727631176432484473>"
}
for (result in results) {
when (result) {
is UserUnbannedResult -> {
context.reply(true) {
content =
"Usuário ${result.userId} (<@${result.userId}) foi desbanido com sucesso. Obrigada por ter corrigido a cagada de alguém... eu acho né... <:lori_coffee:727631176432484473>"
}

LoriToolsUtils.logToSaddestOfTheSads(
helper,
context.user,
userId,
"Usuário desbanido de usar a Loritta",
null,
reason,
Color(88, 101, 242)
)
LoriToolsUtils.logToSaddestOfTheSads(
helper,
context.user,
result.userId,
"Usuário desbanido de usar a Loritta",
null,
result.reason,
Color(88, 101, 242)
)

try {
helper.helperRest.guild.modifyGuildMember(
Snowflake(helper.config.guilds.community.id),
Snowflake(userId)
) {
this.communicationDisabledUntil = null
try {
helper.helperRest.guild.modifyGuildMember(
Snowflake(helper.config.guilds.community.id),
Snowflake(result.userId)
) {
this.communicationDisabledUntil = null

this.reason = "User was Loritta Unbanned!"
this.reason = "User was Loritta Unbanned!"
}
} catch (e: KtorRequestException) {
} // Maybe they aren't on the server
}

is UserIsNotBannedResult -> {
context.reply(true) {
content = "O usuário ${result.userId} (<@${result.userId}>) não está banido, bobão!"
}
} catch (e: KtorRequestException) {} // Maybe they aren't on the server
}
is UserIsNotBannedResult -> {
context.reply(true) {
content = "O usuário $userId (<@$userId>) não está banido, bobão!"
}
}
}
}

private sealed class UnbanResult

private object UserUnbannedResult : UnbanResult()
private class UserUnbannedResult(
val userId: Long,
val reason: String,
) : UnbanResult()

private object UserIsNotBannedResult : UnbanResult()
private class UserIsNotBannedResult(
val userId: Long,
) : UnbanResult()
}

class LoriBanRenameExecutor(helper: LorittaHelper) : HelperExecutor(helper, PermissionLevel.ADMIN) {
Expand Down

0 comments on commit 6c98bc3

Please sign in to comment.