Skip to content

Commit

Permalink
refactor: give the reputation command a facelift
Browse files Browse the repository at this point in the history
  • Loading branch information
averen committed May 10, 2024
1 parent 5c0e60f commit f976d01
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
48 changes: 34 additions & 14 deletions src/main/kotlin/com/sandrabot/sandra/commands/social/Reputation.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2023 Avery Carroll and Logan Devecka
* Copyright 2017-2024 Avery Carroll and Logan Devecka
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,24 +22,44 @@ import com.sandrabot.sandra.events.CommandEvent
import com.sandrabot.sandra.utils.canReputation
import com.sandrabot.sandra.utils.format
import dev.minn.jda.ktx.coroutines.await
import kotlin.time.Duration.Companion.seconds

@Suppress("unused")
class Reputation : Command(arguments = "[@user]") {

override suspend fun execute(event: CommandEvent) {
val otherUser = event.arguments.user()!!
// don't allow users to give reputation to themselves
if (event.user == otherUser) {
event.replyError(event.get("self")).setEphemeral(true).await()

// only allow users to give reputation points once every 20 hours
if (!event.userConfig.canReputation() && !event.isOwner) {
val nextRep = event.userConfig.reputationLast + 72_000_000 // 20 hours
val remaining = ((nextRep - System.currentTimeMillis()) / 1_000).seconds.format()
event.replyEmote(event.get("cooldown", remaining), Emotes.TIME).setEphemeral(true).queue()
return
}

// user is guaranteed to be non-null since it's required
val targetUser = event.arguments.user()!!
// prevent bots from receiving rep and creating data profiles
if (targetUser.isBot || targetUser.isSystem) {
event.replyError(event.get("no_bots")).setEphemeral(true).queue()
return
}

// don't allow the user to give reputation to themselves either
if (targetUser == event.user) {
event.replyError(event.get("no_self")).setEphemeral(true).await()
return
}
// only allow users to give reputation once every 20 hours
if (event.userConfig.canReputation() || event.isOwner) {
// increment the reputation of the other user
val config = event.sandra.config[otherUser].apply { reputation++ }
// update the last reputation time
event.userConfig.reputationLast = System.currentTimeMillis()
// reply with the new reputation count
event.replyEmote(event.get("reply", otherUser, config.reputation.format()), Emotes.ADD).await()
} else event.replyError(event.get("cooldown")).setEphemeral(true).await() // reply with the cooldown error

// increment the reputation of the target user
val targetConfig = event.sandra.config[targetUser].apply { reputation++ }
// update the current user's reputation timer
event.userConfig.reputationLast = System.currentTimeMillis()

// reply with the target user's updated rep count
val reply = event.get("reply", targetUser, targetConfig.reputation.format())
event.replyEmote(reply, Emotes.ADD).queue()

}

}
7 changes: 4 additions & 3 deletions src/main/resources/content/english.json
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,10 @@
"reputation": {
"name": "reputation",
"description": "send someone a meaningless reputation point",
"cooldown": "you can only send reputation once every **20** hours",
"self": "as hard as you may try, you can't give yourself reputation",
"reply": "you sent %1$s a reputation point, they now have %2$s rep",
"cooldown": "woah there, hold your horses! you still have another %1$s left on the clock",
"no_bots": "sorry pal, bots aren't allowed to receive reputation points. go on now, try someone else",
"no_self": "as hard as you may try, you can't give yourself reputation points. pick someone else",
"reply": "you sent %1$s a reputation point! they now have %2$s rep thanks to you",
"arguments": {
"user": {
"name": "user",
Expand Down

0 comments on commit f976d01

Please sign in to comment.