Skip to content

Commit

Permalink
Allow bulk adding/removing sonhos to multiple users
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPowerGamerBR committed Apr 7, 2024
1 parent 408f353 commit 724e2d2
Showing 1 changed file with 81 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ import net.perfectdreams.loritta.morenitta.dao.GuildProfile
import net.perfectdreams.loritta.morenitta.dao.Payment
import net.perfectdreams.loritta.morenitta.dao.servers.moduleconfigs.EconomyConfig
import net.perfectdreams.loritta.morenitta.messages.LorittaReply
import net.perfectdreams.loritta.morenitta.utils.ClusterOfflineException
import net.perfectdreams.loritta.morenitta.utils.Constants
import net.perfectdreams.loritta.morenitta.utils.gson
import net.perfectdreams.loritta.morenitta.utils.isLorittaSupervisor
import net.perfectdreams.loritta.morenitta.utils.*
import net.perfectdreams.loritta.serializable.StoredDivineInterventionSonhosTransaction
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.sql.*
Expand Down Expand Up @@ -178,62 +175,108 @@ class LoriServerListConfigCommand(loritta: LorittaBot) : AbstractCommand(loritta
}

if (arg0 == "add_dreams" && arg1 != null && arg2 != null) {
val user = context.getUserAt(2)!!
val onlyUsersRawArgs = context.rawArgs.drop(2)

val matchedUsers = onlyUsersRawArgs.map {
it to DiscordUtils.extractUserFromString(
loritta,
it,
context.event.message.mentions.users,
context.guildOrNull
)
}

val nonMatched = matchedUsers.filter { it.second == null }
if (nonMatched.isNotEmpty()) {
context.reply(
LorittaReply(
"Usuários ${nonMatched.joinToString { it.first }} não foram encontrados então os sonhos não foram adicionados para todos..."
)
)
return
}
val matched = matchedUsers.mapNotNull { it.second }

loritta.pudding.transaction {
Profiles.update({ Profiles.id eq user.idLong }) {
with(SqlExpressionBuilder) {
it.update(money, money + arg1.toLong())
for (user in matched) {
Profiles.update({ Profiles.id eq user.idLong }) {
with(SqlExpressionBuilder) {
it.update(money, money + arg1.toLong())
}
}
}

// Cinnamon transaction system
SimpleSonhosTransactionsLogUtils.insert(
user.idLong,
Instant.now(),
TransactionType.DIVINE_INTERVENTION,
arg1.toLong(),
StoredDivineInterventionSonhosTransaction(
DivineInterventionTransactionEntryAction.ADDED_SONHOS,
context.userHandle.idLong,
null
// Cinnamon transaction system
SimpleSonhosTransactionsLogUtils.insert(
user.idLong,
Instant.now(),
TransactionType.DIVINE_INTERVENTION,
arg1.toLong(),
StoredDivineInterventionSonhosTransaction(
DivineInterventionTransactionEntryAction.ADDED_SONHOS,
context.userHandle.idLong,
null
)
)
)
}
}

context.reply(
LorittaReply(
"Sonhos de ${user.asMention} foram editados com sucesso!"
"Sonhos de ${matched.joinToString { it.asMention }} foram editados com sucesso!"
)
)
return
}

if (arg0 == "remove_dreams" && arg1 != null && arg2 != null) {
val user = context.getUserAt(2)!!
val onlyUsersRawArgs = context.rawArgs.drop(2)

val matchedUsers = onlyUsersRawArgs.map {
it to DiscordUtils.extractUserFromString(
loritta,
it,
context.event.message.mentions.users,
context.guildOrNull
)
}

val nonMatched = matchedUsers.filter { it.second == null }
if (nonMatched.isNotEmpty()) {
context.reply(
LorittaReply(
"Usuários ${nonMatched.joinToString { it.first }} não foram encontrados então os sonhos não foram removidos para todos..."
)
)
return
}
val matched = matchedUsers.mapNotNull { it.second }

loritta.pudding.transaction {
Profiles.update({ Profiles.id eq user.idLong }) {
with(SqlExpressionBuilder) {
it.update(money, money - arg1.toLong())
for (user in matched) {
Profiles.update({ Profiles.id eq user.idLong }) {
with(SqlExpressionBuilder) {
it.update(money, money - arg1.toLong())
}
}
}

// Cinnamon transaction system
SimpleSonhosTransactionsLogUtils.insert(
user.idLong,
Instant.now(),
TransactionType.DIVINE_INTERVENTION,
arg1.toLong(),
StoredDivineInterventionSonhosTransaction(
DivineInterventionTransactionEntryAction.REMOVED_SONHOS,
context.userHandle.idLong,
null
// Cinnamon transaction system
SimpleSonhosTransactionsLogUtils.insert(
user.idLong,
Instant.now(),
TransactionType.DIVINE_INTERVENTION,
arg1.toLong(),
StoredDivineInterventionSonhosTransaction(
DivineInterventionTransactionEntryAction.REMOVED_SONHOS,
context.userHandle.idLong,
null
)
)
)
}
}

context.reply(
LorittaReply(
"Sonhos de ${user.asMention} foram editados com sucesso!"
"Sonhos de ${matched.joinToString { it.asMention }} foram editados com sucesso!"
)
)
return
Expand Down

0 comments on commit 724e2d2

Please sign in to comment.