From f976d01d14d542d28a3afb9c0f15903347eaf13f Mon Sep 17 00:00:00 2001 From: averen Date: Thu, 9 May 2024 19:22:11 -0600 Subject: [PATCH] refactor: give the reputation command a facelift --- .../sandra/commands/social/Reputation.kt | 48 +++++++++++++------ src/main/resources/content/english.json | 7 +-- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/com/sandrabot/sandra/commands/social/Reputation.kt b/src/main/kotlin/com/sandrabot/sandra/commands/social/Reputation.kt index ad991b2f..89cdbab6 100644 --- a/src/main/kotlin/com/sandrabot/sandra/commands/social/Reputation.kt +++ b/src/main/kotlin/com/sandrabot/sandra/commands/social/Reputation.kt @@ -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. @@ -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() + } + } diff --git a/src/main/resources/content/english.json b/src/main/resources/content/english.json index c607d764..2eec0e05 100644 --- a/src/main/resources/content/english.json +++ b/src/main/resources/content/english.json @@ -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",