From 59cecbaa2e35bd2b997c7a3dd02de3934cdf92e8 Mon Sep 17 00:00:00 2001 From: duncte123 Date: Fri, 28 Jan 2022 13:29:25 +0100 Subject: [PATCH 1/4] Create new class --- .../commands/uncategorized/YesNoCommand.java | 10 +- .../skybot/objects/command/Command.java | 8 + .../ml/duncte123/skybot/utils/AirUtils.java | 2 +- .../ml/duncte123/skybot/extensions/List.kt | 23 +++ .../duncte123/skybot/objects/BaseCommand.kt | 178 ++++++++++++++++++ 5 files changed, 216 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/ml/duncte123/skybot/extensions/List.kt create mode 100644 src/main/kotlin/ml/duncte123/skybot/objects/BaseCommand.kt diff --git a/src/main/java/ml/duncte123/skybot/commands/uncategorized/YesNoCommand.java b/src/main/java/ml/duncte123/skybot/commands/uncategorized/YesNoCommand.java index e81717ad1..e66b2e3f9 100644 --- a/src/main/java/ml/duncte123/skybot/commands/uncategorized/YesNoCommand.java +++ b/src/main/java/ml/duncte123/skybot/commands/uncategorized/YesNoCommand.java @@ -20,18 +20,20 @@ import me.duncte123.botcommons.messaging.EmbedUtils; import me.duncte123.botcommons.web.WebUtils; -import ml.duncte123.skybot.objects.command.Command; +import ml.duncte123.skybot.objects.BaseCommand; import ml.duncte123.skybot.objects.command.CommandContext; import javax.annotation.Nonnull; import static me.duncte123.botcommons.messaging.MessageUtils.sendEmbed; -public class YesNoCommand extends Command { +public class YesNoCommand extends BaseCommand { public YesNoCommand() { - this.name = "yesno"; - this.help = "Chooses between yes or no"; + super( + "yesno", + "Chooses between yes or no" + ); } @Override diff --git a/src/main/java/ml/duncte123/skybot/objects/command/Command.java b/src/main/java/ml/duncte123/skybot/objects/command/Command.java index 1a6ef1a1c..febff2bb3 100644 --- a/src/main/java/ml/duncte123/skybot/objects/command/Command.java +++ b/src/main/java/ml/duncte123/skybot/objects/command/Command.java @@ -18,6 +18,8 @@ package ml.duncte123.skybot.objects.command; +import kotlin.DeprecationLevel; +import kotlin.ReplaceWith; import kotlin.jvm.functions.Function1; import kotlin.jvm.functions.Function2; import me.duncte123.botcommons.messaging.MessageConfig; @@ -38,6 +40,12 @@ import static ml.duncte123.skybot.utils.AirUtils.parsePerms; @SuppressWarnings({"SameParameterValue", "PMD.TooManyFields"}) +@Deprecated // rewrite in kotlin +@kotlin.Deprecated( + message = "Replaced in kotlin", + replaceWith = @ReplaceWith(expression = "BaseCommand()", imports = {"ml.duncte123.skybot.objects.BaseCommand"}), + level = DeprecationLevel.WARNING +) public abstract class Command implements ICommand { protected static final Logger LOGGER = LoggerFactory.getLogger(Command.class); // The size should match the usage for stability but not more than 4. diff --git a/src/main/java/ml/duncte123/skybot/utils/AirUtils.java b/src/main/java/ml/duncte123/skybot/utils/AirUtils.java index bf38bac4b..c237dc330 100644 --- a/src/main/java/ml/duncte123/skybot/utils/AirUtils.java +++ b/src/main/java/ml/duncte123/skybot/utils/AirUtils.java @@ -328,7 +328,7 @@ public static void handleExpiredReminders(List reminders, DatabaseAdap final List extraRemoval = reminders.stream() .filter((reminder) -> reminder.getReminder_date().isAfter(plusTwoDays)) .map(Reminder::getId) - .collect(Collectors.toList()); + .toList(); toPurge.addAll(extraRemoval); diff --git a/src/main/kotlin/ml/duncte123/skybot/extensions/List.kt b/src/main/kotlin/ml/duncte123/skybot/extensions/List.kt new file mode 100644 index 000000000..ac3381f24 --- /dev/null +++ b/src/main/kotlin/ml/duncte123/skybot/extensions/List.kt @@ -0,0 +1,23 @@ +/* + * Skybot, a multipurpose discord bot + * Copyright (C) 2017 Duncan "duncte123" Sterken & Ramid "ramidzkh" Khan & Maurice R S "Sanduhr32" + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package ml.duncte123.skybot.extensions + +import net.dv8tion.jda.api.Permission + +fun List.toHuman() = this.joinToString(separator = "`, `", postfix = "` and `") { it.name } diff --git a/src/main/kotlin/ml/duncte123/skybot/objects/BaseCommand.kt b/src/main/kotlin/ml/duncte123/skybot/objects/BaseCommand.kt new file mode 100644 index 000000000..42cbe8a33 --- /dev/null +++ b/src/main/kotlin/ml/duncte123/skybot/objects/BaseCommand.kt @@ -0,0 +1,178 @@ +/* + * Skybot, a multipurpose discord bot + * Copyright (C) 2017 Duncan "duncte123" Sterken & Ramid "ramidzkh" Khan & Maurice R S "Sanduhr32" + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package ml.duncte123.skybot.objects + +import me.duncte123.botcommons.messaging.MessageConfig +import me.duncte123.botcommons.messaging.MessageUtils +import me.duncte123.botcommons.messaging.MessageUtils.sendMsg +import ml.duncte123.skybot.extensions.toHuman +import ml.duncte123.skybot.objects.command.CommandCategory +import ml.duncte123.skybot.objects.command.CommandContext +import ml.duncte123.skybot.objects.command.Flag +import ml.duncte123.skybot.objects.command.ICommand +import net.dv8tion.jda.api.Permission +import java.util.concurrent.TimeUnit + +abstract class BaseCommand( + private val name: String, + val help: String, + val extraInfo: String? = null, + + private val aliases: Array = arrayOf(), + private val displayAliasesInHelp: Boolean = false, + + val usage: String = "", + val requiresArgs: Boolean = false, + val requiredArgCount: Int = 1, + + private val category: CommandCategory = CommandCategory.MAIN, + val userPermissions: List = listOf(), + val botPermissions: List = listOf(), + val flags: Array = arrayOf(), + + val cooldown: Int = 0, + val cooldownScope: CooldownScope = CooldownScope.USER, + val cooldownKey: (String, CommandContext) -> String = cooldownScope::formatKey, + val overridesCooldown: (CommandContext) -> Boolean = { false } +) : ICommand { + // I love mixing java into this :D + constructor(name: String, help: String) : this(name, help, extraInfo = null) + + abstract fun execute(ctx: CommandContext) + + override fun executeCommand(ctx: CommandContext) { + if (userPermissions.any() && !ctx.member.hasPermission(ctx.channel, userPermissions)) { + val word = "permission${if (userPermissions.size > 1) "s" else ""}" + + sendMsg( + MessageConfig.Builder.fromCtx(ctx) + .setMessageFormat( + "You need the `%s` %s for this command\nPlease contact your server administrator if this is incorrect.", + userPermissions.toHuman(), word + ) + .build() + ) + + return + } + + if (botPermissions.any() && !ctx.selfMember.hasPermission(ctx.channel, botPermissions)) { + val word = "permission${if (botPermissions.size > 1) "s" else ""}" + + sendMsg( + MessageConfig.Builder.fromCtx(ctx) + .setMessageFormat( + "I need the `%s` %s for this command to work\nPlease contact your server administrator about this.", + botPermissions.toHuman(), word + ) + .build() + ) + + return + } + + if (requiresArgs) { + val args = ctx.args + + if (args.isEmpty() || args.size < requiredArgCount) { + sendMsg(ctx, "Missing arguments, usage: ${getUsageInstructions(ctx)}") + return + } + } + + if (cooldown > 0 && !overridesCooldown(ctx) && checkCooldown(ctx)) { + return + } + + execute(ctx) + } + + fun getUsageInstructions(prefix: String, invoke: String) = "`$prefix$invoke${" $usage".replace("{prefix}", prefix).trim { it <= ' ' }}`" + + fun getUsageInstructions(ctx: CommandContext) = this.getUsageInstructions(ctx.prefix, ctx.invoke) + + protected fun sendUsageInstructions(ctx: CommandContext) = sendMsg(ctx, "Usage: " + this.getUsageInstructions(ctx)) + + override fun getHelp(invoke: String, prefix: String) = help.replace("{prefix}", prefix) + + override fun shouldDisplayAliasesInHelp() = displayAliasesInHelp + + override fun getName() = name + + override fun getCategory() = category + + override fun getAliases() = aliases + + private fun checkCooldown(ctx: CommandContext): Boolean { + // Get the cooldown key for this command + val cooldownKey = cooldownKey(name, ctx) + val commandManager = ctx.commandManager + val remainingCooldown = commandManager.getRemainingCooldown(cooldownKey) + + if (remainingCooldown > 0) { + sendMsg( + MessageConfig.Builder.fromCtx(ctx) + .setMessageFormat( + "This command is on cooldown for %s more seconds%s!", + remainingCooldown, + cooldownScope.extraErrorMsg + ) + .setSuccessAction { + it.delete().queueAfter(10, TimeUnit.SECONDS) + } + .build() + ) + MessageUtils.sendError(ctx.message) + return true + } + + // Set the cooldown for the command + commandManager.setCooldown(cooldownKey, cooldown) + + return false + } + + override fun equals(other: Any?): Boolean { + if (other !is BaseCommand) { + return false + } + + return name == other.name + } + + override fun hashCode(): Int { + var result = requiresArgs.hashCode() + result = 31 * result + requiredArgCount + result = 31 * result + displayAliasesInHelp.hashCode() + result = 31 * result + category.hashCode() + result = 31 * result + name.hashCode() + result = 31 * result + aliases.contentHashCode() + result = 31 * result + help.hashCode() + result = 31 * result + usage.hashCode() + result = 31 * result + (extraInfo?.hashCode() ?: 0) + result = 31 * result + userPermissions.hashCode() + result = 31 * result + botPermissions.hashCode() + result = 31 * result + flags.contentHashCode() + result = 31 * result + cooldown + result = 31 * result + cooldownScope.hashCode() + result = 31 * result + cooldownKey.hashCode() + result = 31 * result + overridesCooldown.hashCode() + return result + } +} From 2193444daf0b2245b35dc56feccb90e4c20222d1 Mon Sep 17 00:00:00 2001 From: duncte123 Date: Fri, 28 Jan 2022 14:39:02 +0100 Subject: [PATCH 2/4] Convert the first few commands --- build.gradle.kts | 3 +- .../commands/animals/AlpacaCommand.java | 13 ++--- .../skybot/commands/animals/DogCommand.java | 12 +++-- .../skybot/commands/animals/KittyCommand.java | 19 ++++--- .../skybot/commands/animals/LlamaCommand.java | 12 +++-- .../skybot/commands/animals/SealCommand.java | 14 ++--- .../commands/essentials/RemindmeCommand.java | 51 +++++++++++-------- .../commands/essentials/ShardInfoCommand.java | 32 ++++++------ .../commands/essentials/TestTagCommand.java | 22 ++++---- .../commands/essentials/TokenCommand.java | 19 ++++--- .../skybot/objects/command/Command.java | 2 +- .../objects/command/CommandContext.java | 10 ++++ .../duncte123/skybot/objects/BaseCommand.kt | 14 +++-- 13 files changed, 133 insertions(+), 90 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 019f26338..1c34daa51 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -29,7 +29,7 @@ plugins { idea application - kotlin("jvm") version "1.6.0" + kotlin("jvm") version "1.6.10" id("com.github.johnrengelman.shadow") version "7.1.2" id("com.github.breadmoirai.github-release") version "2.2.12" id("org.jmailen.kotlinter") version "3.5.1" @@ -104,7 +104,6 @@ dependencies { // kotlin implementation(kotlin("stdlib-jdk8")) -// implementation(kotlin("kotlinx-coroutines-core")) implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0") // JDA utils diff --git a/src/main/java/ml/duncte123/skybot/commands/animals/AlpacaCommand.java b/src/main/java/ml/duncte123/skybot/commands/animals/AlpacaCommand.java index 8f976c874..310fda452 100644 --- a/src/main/java/ml/duncte123/skybot/commands/animals/AlpacaCommand.java +++ b/src/main/java/ml/duncte123/skybot/commands/animals/AlpacaCommand.java @@ -19,7 +19,7 @@ package ml.duncte123.skybot.commands.animals; import com.fasterxml.jackson.databind.JsonNode; -import ml.duncte123.skybot.objects.command.Command; +import ml.duncte123.skybot.objects.BaseCommand; import ml.duncte123.skybot.objects.command.CommandCategory; import ml.duncte123.skybot.objects.command.CommandContext; @@ -28,12 +28,13 @@ import static me.duncte123.botcommons.messaging.EmbedUtils.embedImage; import static me.duncte123.botcommons.messaging.MessageUtils.sendEmbed; -public class AlpacaCommand extends Command { - +public class AlpacaCommand extends BaseCommand { public AlpacaCommand() { - this.category = CommandCategory.ANIMALS; - this.name = "alpaca"; - this.help = "Shows an alpaca"; + super( + "alpaca", + "Shows a random alpaca", + CommandCategory.ANIMALS + ); } @Override diff --git a/src/main/java/ml/duncte123/skybot/commands/animals/DogCommand.java b/src/main/java/ml/duncte123/skybot/commands/animals/DogCommand.java index f381533f2..2698aadb7 100644 --- a/src/main/java/ml/duncte123/skybot/commands/animals/DogCommand.java +++ b/src/main/java/ml/duncte123/skybot/commands/animals/DogCommand.java @@ -21,7 +21,7 @@ import io.sentry.Sentry; import me.duncte123.botcommons.messaging.EmbedUtils; import me.duncte123.botcommons.web.WebUtils; -import ml.duncte123.skybot.objects.command.Command; +import ml.duncte123.skybot.objects.BaseCommand; import ml.duncte123.skybot.objects.command.CommandCategory; import ml.duncte123.skybot.objects.command.CommandContext; @@ -29,12 +29,14 @@ import static me.duncte123.botcommons.messaging.MessageUtils.sendEmbed; -public class DogCommand extends Command { +public class DogCommand extends BaseCommand { public DogCommand() { - this.category = CommandCategory.ANIMALS; - this.name = "dog"; - this.help = "Shows a dog"; + super( + "Dog", + "Shows a random dog", + CommandCategory.ANIMALS + ); } @Override diff --git a/src/main/java/ml/duncte123/skybot/commands/animals/KittyCommand.java b/src/main/java/ml/duncte123/skybot/commands/animals/KittyCommand.java index 3225dce84..18ea41953 100644 --- a/src/main/java/ml/duncte123/skybot/commands/animals/KittyCommand.java +++ b/src/main/java/ml/duncte123/skybot/commands/animals/KittyCommand.java @@ -20,7 +20,7 @@ import me.duncte123.botcommons.messaging.EmbedUtils; import me.duncte123.botcommons.web.WebUtils; -import ml.duncte123.skybot.objects.command.Command; +import ml.duncte123.skybot.objects.BaseCommand; import ml.duncte123.skybot.objects.command.CommandCategory; import ml.duncte123.skybot.objects.command.CommandContext; @@ -28,15 +28,18 @@ import static me.duncte123.botcommons.messaging.MessageUtils.sendEmbed; -public class KittyCommand extends Command { +public class KittyCommand extends BaseCommand { public KittyCommand() { - this.category = CommandCategory.ANIMALS; - this.name = "cat"; - this.aliases = new String[]{ - "kitty", - }; - this.help = "Shows a cat"; + super( + "cat", + "Shows a random cat", + CommandCategory.ANIMALS, + null, + new String[]{ + "kitty", + } + ); } @Override diff --git a/src/main/java/ml/duncte123/skybot/commands/animals/LlamaCommand.java b/src/main/java/ml/duncte123/skybot/commands/animals/LlamaCommand.java index 2696f6b16..3689c91b5 100644 --- a/src/main/java/ml/duncte123/skybot/commands/animals/LlamaCommand.java +++ b/src/main/java/ml/duncte123/skybot/commands/animals/LlamaCommand.java @@ -20,7 +20,7 @@ import com.fasterxml.jackson.databind.JsonNode; import me.duncte123.botcommons.messaging.EmbedUtils; -import ml.duncte123.skybot.objects.command.Command; +import ml.duncte123.skybot.objects.BaseCommand; import ml.duncte123.skybot.objects.command.CommandCategory; import ml.duncte123.skybot.objects.command.CommandContext; @@ -28,12 +28,14 @@ import static me.duncte123.botcommons.messaging.MessageUtils.sendEmbed; -public class LlamaCommand extends Command { +public class LlamaCommand extends BaseCommand { public LlamaCommand() { - this.category = CommandCategory.ANIMALS; - this.name = "llama"; - this.help = "Shows a llama"; + super( + "llama", + "Shows a random llama", + CommandCategory.ANIMALS + ); } @Override diff --git a/src/main/java/ml/duncte123/skybot/commands/animals/SealCommand.java b/src/main/java/ml/duncte123/skybot/commands/animals/SealCommand.java index b5a7e796c..544fa0eee 100644 --- a/src/main/java/ml/duncte123/skybot/commands/animals/SealCommand.java +++ b/src/main/java/ml/duncte123/skybot/commands/animals/SealCommand.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.JsonNode; import me.duncte123.botcommons.messaging.EmbedUtils; +import ml.duncte123.skybot.objects.BaseCommand; import ml.duncte123.skybot.objects.command.Command; import ml.duncte123.skybot.objects.command.CommandCategory; import ml.duncte123.skybot.objects.command.CommandContext; @@ -28,15 +29,14 @@ import static me.duncte123.botcommons.messaging.MessageUtils.sendEmbed; -public class SealCommand extends Command { +public class SealCommand extends BaseCommand { public SealCommand() { - this.category = CommandCategory.ANIMALS; - this.name = "seal"; - this.aliases = new String[]{ - "zeehond", - }; - this.help = "Shows a seal"; + super( + "seal", + "Shows a random seal", + CommandCategory.ANIMALS + ); } @Override diff --git a/src/main/java/ml/duncte123/skybot/commands/essentials/RemindmeCommand.java b/src/main/java/ml/duncte123/skybot/commands/essentials/RemindmeCommand.java index 6343e0c1e..7a1fd3ad1 100644 --- a/src/main/java/ml/duncte123/skybot/commands/essentials/RemindmeCommand.java +++ b/src/main/java/ml/duncte123/skybot/commands/essentials/RemindmeCommand.java @@ -20,7 +20,7 @@ import me.duncte123.durationparser.DurationParser; import me.duncte123.durationparser.ParsedDuration; -import ml.duncte123.skybot.objects.command.Command; +import ml.duncte123.skybot.objects.BaseCommand; import ml.duncte123.skybot.objects.command.CommandCategory; import ml.duncte123.skybot.objects.command.CommandContext; import ml.duncte123.skybot.objects.command.Flag; @@ -34,29 +34,36 @@ import static me.duncte123.botcommons.messaging.MessageUtils.sendMsg; -public class RemindmeCommand extends Command { +public class RemindmeCommand extends BaseCommand { public RemindmeCommand() { - this.category = CommandCategory.UTILS; - this.name = "remind"; - this.aliases = new String[]{ - "remindme", - }; - this.help = "Creates a reminder for you, add `--channel` to remind you in the current channel"; - this.usage = " -t "; - this.extraInfo = "Example: `{prefix}remind Clean your room :/ -t 1d5m"; - this.flags = new Flag[]{ - new Flag( - 't', - "time", - "Sets the time for the reminder" - ), - new Flag( - 'c', - "channel", - "When this flag is set you will be reminded in the channel where you executed the command (reminder is in dms by default)" - ), - }; + super( + "remind", + "Creates a reminder for you, add `--channel` to remind you in the current channel", + CommandCategory.UTILS, + "Example: `{prefix}remind Clean your room :/ -t 1d5m", + new String[]{ + "remindme", + }, + false, + " -t ", + true, + 2, + List.of(), + List.of(), + new Flag[]{ + new Flag( + 't', + "time", + "Sets the time for the reminder" + ), + new Flag( + 'c', + "channel", + "When this flag is set you will be reminded in the channel where you executed the command (reminder is in your DMs by default)" + ), + } + ); } @Override diff --git a/src/main/java/ml/duncte123/skybot/commands/essentials/ShardInfoCommand.java b/src/main/java/ml/duncte123/skybot/commands/essentials/ShardInfoCommand.java index ad835753e..667999ce1 100644 --- a/src/main/java/ml/duncte123/skybot/commands/essentials/ShardInfoCommand.java +++ b/src/main/java/ml/duncte123/skybot/commands/essentials/ShardInfoCommand.java @@ -19,7 +19,7 @@ package ml.duncte123.skybot.commands.essentials; import me.duncte123.botcommons.messaging.MessageUtils; -import ml.duncte123.skybot.objects.command.Command; +import ml.duncte123.skybot.objects.BaseCommand; import ml.duncte123.skybot.objects.command.CommandCategory; import ml.duncte123.skybot.objects.command.CommandContext; import ml.duncte123.skybot.objects.command.Flag; @@ -40,22 +40,24 @@ import static me.duncte123.botcommons.messaging.MessageUtils.sendEmbed; @SuppressWarnings("ConstantConditions") -public class ShardInfoCommand extends Command { +public class ShardInfoCommand extends BaseCommand { public ShardInfoCommand() { - this.category = CommandCategory.UTILS; - this.name = "shardinfo"; - this.aliases = new String[]{ - "shards", - }; - this.help = "Get information about all things shards"; - this.flags = new Flag[]{ - new Flag( - 'm', - "mobile", - "Shows a mobile friendly embed instead" - ), - }; + super( + "shardinfo", + "Get information about all things shards!", + CommandCategory.UTILS, + new String[]{ + "shards", + }, + new Flag[]{ + new Flag( + 'm', + "mobile", + "Shows a mobile friendly embed instead" + ), + } + ); } @Override diff --git a/src/main/java/ml/duncte123/skybot/commands/essentials/TestTagCommand.java b/src/main/java/ml/duncte123/skybot/commands/essentials/TestTagCommand.java index f6e64e4f5..2171187d9 100644 --- a/src/main/java/ml/duncte123/skybot/commands/essentials/TestTagCommand.java +++ b/src/main/java/ml/duncte123/skybot/commands/essentials/TestTagCommand.java @@ -19,7 +19,7 @@ package ml.duncte123.skybot.commands.essentials; import me.duncte123.botcommons.messaging.EmbedUtils; -import ml.duncte123.skybot.objects.command.Command; +import ml.duncte123.skybot.objects.BaseCommand; import ml.duncte123.skybot.objects.command.CommandCategory; import ml.duncte123.skybot.objects.command.CommandContext; import ml.duncte123.skybot.utils.CommandUtils; @@ -30,16 +30,20 @@ import static me.duncte123.botcommons.messaging.MessageUtils.sendEmbed; import static me.duncte123.botcommons.messaging.MessageUtils.sendMsg; -public class TestTagCommand extends Command { +public class TestTagCommand extends BaseCommand { public TestTagCommand() { - this.category = CommandCategory.UTILS; - this.name = "testtag"; - this.aliases = new String[]{ - "tt", - }; - this.help = "Test your jagtag format before you save it as custom command etc."; - this.usage = ""; + super( + "testtag", + "Test your jagtag format before you save it as custom command etc.", + CommandCategory.UTILS, + null, + new String[]{ + "tt", + }, + false, + "" + ); } @Override diff --git a/src/main/java/ml/duncte123/skybot/commands/essentials/TokenCommand.java b/src/main/java/ml/duncte123/skybot/commands/essentials/TokenCommand.java index 9692d3db6..54e8e0a7e 100644 --- a/src/main/java/ml/duncte123/skybot/commands/essentials/TokenCommand.java +++ b/src/main/java/ml/duncte123/skybot/commands/essentials/TokenCommand.java @@ -20,8 +20,8 @@ import com.fasterxml.jackson.databind.JsonNode; import me.duncte123.botcommons.messaging.MessageConfig; +import ml.duncte123.skybot.objects.BaseCommand; import ml.duncte123.skybot.objects.api.DuncteApis; -import ml.duncte123.skybot.objects.command.Command; import ml.duncte123.skybot.objects.command.CommandCategory; import ml.duncte123.skybot.objects.command.CommandContext; @@ -37,7 +37,7 @@ import static me.duncte123.botcommons.messaging.MessageUtils.sendMsg; -public class TokenCommand extends Command { +public class TokenCommand extends BaseCommand { private static final Pattern TOKEN_REGEX = Pattern.compile("([a-zA-Z0-9]+)\\.([a-zA-Z0-9\\-_]+)\\.([a-zA-Z0-9\\-_]+)"); private static final String STRING_FORMAT = "Deconstruction results for token: `%s`%n%n" + @@ -46,11 +46,16 @@ public class TokenCommand extends Command { "Keep in mind that verifying if the token is valid by making a request to discord is against the TOS"; public TokenCommand() { - this.requiresArgs = true; - this.category = CommandCategory.UTILS; - this.name = "token"; - this.help = "Deconstructs a token to get as much information as possible from it"; - this.usage = ""; + super( + "token", + "Deconstructs a bot token to get as much information as possible from it", + CommandCategory.UTILS, + null, + new String[0], + false, + "", + true + ); } @Override diff --git a/src/main/java/ml/duncte123/skybot/objects/command/Command.java b/src/main/java/ml/duncte123/skybot/objects/command/Command.java index febff2bb3..5e9cbb65f 100644 --- a/src/main/java/ml/duncte123/skybot/objects/command/Command.java +++ b/src/main/java/ml/duncte123/skybot/objects/command/Command.java @@ -44,7 +44,7 @@ @kotlin.Deprecated( message = "Replaced in kotlin", replaceWith = @ReplaceWith(expression = "BaseCommand()", imports = {"ml.duncte123.skybot.objects.BaseCommand"}), - level = DeprecationLevel.WARNING + level = DeprecationLevel.ERROR ) public abstract class Command implements ICommand { protected static final Logger LOGGER = LoggerFactory.getLogger(Command.class); diff --git a/src/main/java/ml/duncte123/skybot/objects/command/CommandContext.java b/src/main/java/ml/duncte123/skybot/objects/command/CommandContext.java index c9210ed39..ed8f8684b 100644 --- a/src/main/java/ml/duncte123/skybot/objects/command/CommandContext.java +++ b/src/main/java/ml/duncte123/skybot/objects/command/CommandContext.java @@ -24,6 +24,7 @@ import ml.duncte123.skybot.*; import ml.duncte123.skybot.adapters.DatabaseAdapter; import ml.duncte123.skybot.entities.jda.DunctebotGuild; +import ml.duncte123.skybot.objects.BaseCommand; import ml.duncte123.skybot.objects.api.DuncteApis; import ml.duncte123.skybot.objects.apis.BlargBot; import ml.duncte123.skybot.objects.apis.alexflipnote.Alexflipnote; @@ -113,6 +114,7 @@ public DuncteApis getApis() { return variables.getApis(); } + @Deprecated public Map> getParsedFlags(Command cmd) { if (this.cachedFlags == null) { this.cachedFlags = CommandUtils.parseInput(cmd.flags, this.getArgs()); @@ -121,6 +123,14 @@ public Map> getParsedFlags(Command cmd) { return this.cachedFlags; } + public Map> getParsedFlags(BaseCommand cmd) { + if (this.cachedFlags == null) { + this.cachedFlags = CommandUtils.parseInput(cmd.getFlags(), this.getArgs()); + } + + return this.cachedFlags; + } + // --------------- Normal methods --------------- // public String getInvoke() { diff --git a/src/main/kotlin/ml/duncte123/skybot/objects/BaseCommand.kt b/src/main/kotlin/ml/duncte123/skybot/objects/BaseCommand.kt index 42cbe8a33..e74226c3d 100644 --- a/src/main/kotlin/ml/duncte123/skybot/objects/BaseCommand.kt +++ b/src/main/kotlin/ml/duncte123/skybot/objects/BaseCommand.kt @@ -29,9 +29,10 @@ import ml.duncte123.skybot.objects.command.ICommand import net.dv8tion.jda.api.Permission import java.util.concurrent.TimeUnit -abstract class BaseCommand( +abstract class BaseCommand @JvmOverloads constructor ( private val name: String, val help: String, + private val category: CommandCategory = CommandCategory.MAIN, val extraInfo: String? = null, private val aliases: Array = arrayOf(), @@ -41,7 +42,6 @@ abstract class BaseCommand( val requiresArgs: Boolean = false, val requiredArgCount: Int = 1, - private val category: CommandCategory = CommandCategory.MAIN, val userPermissions: List = listOf(), val botPermissions: List = listOf(), val flags: Array = arrayOf(), @@ -52,7 +52,15 @@ abstract class BaseCommand( val overridesCooldown: (CommandContext) -> Boolean = { false } ) : ICommand { // I love mixing java into this :D - constructor(name: String, help: String) : this(name, help, extraInfo = null) + // TODO: might try to optimize this a bit instead of using JvmOverloads + // constructor(name: String, help: String) : this(name, help, extraInfo = null) + constructor( + name: String, + help: String, + category: CommandCategory, + aliases: Array, + flags: Array + ) : this(name, help, category = category, null, aliases = aliases, flags = flags) abstract fun execute(ctx: CommandContext) From e54df1e8ec8b2b92150476baabf26ac553303a0b Mon Sep 17 00:00:00 2001 From: duncte123 Date: Mon, 7 Feb 2022 10:18:06 +0100 Subject: [PATCH 3/4] Convert a few more commands --- .../ml/duncte123/skybot/CommandManager.java | 2 +- .../commands/essentials/TranslateCommand.java | 21 ++++++++------ .../essentials/WolframAlphaCommand.java | 28 +++++++++++-------- .../skybot/commands/fun/ColorCommand.java | 21 ++++++++------ .../skybot/commands/fun/CrashCommand.java | 1 + .../skybot/objects/command/Command.java | 2 +- .../duncte123/skybot/objects/BaseCommand.kt | 5 ++-- 7 files changed, 48 insertions(+), 32 deletions(-) diff --git a/src/main/java/ml/duncte123/skybot/CommandManager.java b/src/main/java/ml/duncte123/skybot/CommandManager.java index 7eab4b5ff..7f3977446 100644 --- a/src/main/java/ml/duncte123/skybot/CommandManager.java +++ b/src/main/java/ml/duncte123/skybot/CommandManager.java @@ -165,7 +165,7 @@ public CommandManager(Variables variables) { this.addCommand(new CoinCommand()); this.addCommand(new ColorCommand()); this.addCommand(new CookieCommand()); - this.addCommand(new CrashCommand()); + // this.addCommand(new CrashCommand()); this.addCommand(new CSShumorCommand()); this.addCommand(new CustomCommandCommand()); this.addCommand(new DanceCommand()); diff --git a/src/main/java/ml/duncte123/skybot/commands/essentials/TranslateCommand.java b/src/main/java/ml/duncte123/skybot/commands/essentials/TranslateCommand.java index fcdb2f2bb..21a8ae909 100644 --- a/src/main/java/ml/duncte123/skybot/commands/essentials/TranslateCommand.java +++ b/src/main/java/ml/duncte123/skybot/commands/essentials/TranslateCommand.java @@ -20,7 +20,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import me.duncte123.botcommons.web.WebUtils; -import ml.duncte123.skybot.objects.command.Command; +import ml.duncte123.skybot.objects.BaseCommand; import ml.duncte123.skybot.objects.command.CommandCategory; import ml.duncte123.skybot.objects.command.CommandContext; @@ -29,15 +29,20 @@ import static me.duncte123.botcommons.messaging.MessageUtils.sendMsg; -public class TranslateCommand extends Command { +public class TranslateCommand extends BaseCommand { public TranslateCommand() { - this.requiresArgs = true; - this.requiredArgCount = 2; - this.category = CommandCategory.UTILS; - this.name = "translate"; - this.help = "Translate a text from English to another language"; - this.usage = " "; + super( + "translate", + "Translate a text from English to another language", + CommandCategory.UTILS, + null, + new String[0], + false, + " ", + true, + 2 + ); } @Override diff --git a/src/main/java/ml/duncte123/skybot/commands/essentials/WolframAlphaCommand.java b/src/main/java/ml/duncte123/skybot/commands/essentials/WolframAlphaCommand.java index d3184951b..4430dbcd4 100644 --- a/src/main/java/ml/duncte123/skybot/commands/essentials/WolframAlphaCommand.java +++ b/src/main/java/ml/duncte123/skybot/commands/essentials/WolframAlphaCommand.java @@ -25,7 +25,7 @@ import me.duncte123.botcommons.messaging.EmbedUtils; import me.duncte123.botcommons.messaging.MessageConfig; import me.duncte123.botcommons.messaging.MessageUtils; -import ml.duncte123.skybot.objects.command.Command; +import ml.duncte123.skybot.objects.BaseCommand; import ml.duncte123.skybot.objects.command.CommandCategory; import ml.duncte123.skybot.objects.command.CommandContext; import net.dv8tion.jda.api.EmbedBuilder; @@ -42,21 +42,25 @@ import static ml.duncte123.skybot.utils.AirUtils.shortenUrl; import static ml.duncte123.skybot.utils.CommandUtils.isUserOrGuildPatron; -public class WolframAlphaCommand extends Command { +public class WolframAlphaCommand extends BaseCommand { private WAEngine waEngine = null; public WolframAlphaCommand() { - this.requiresArgs = true; - this.category = CommandCategory.UTILS; - this.name = "alpha"; - this.aliases = new String[]{ - "wolfram", - "wa", - "wolframalpha", - }; - this.help = "Ask Wolfram|Alpha all your geeky questions"; - this.usage = ""; + super( + "alpha", + "Ask Wolfram|Alpha all your geeky questions", + CommandCategory.UTILS, + null, + new String[]{ + "wolfram", + "wa", + "wolframalpha", + }, + false, + "", + true + ); } private MessageEmbed generateEmbed(CommandContext ctx, WAQueryResult result, String googleKey, ObjectMapper mapper) { diff --git a/src/main/java/ml/duncte123/skybot/commands/fun/ColorCommand.java b/src/main/java/ml/duncte123/skybot/commands/fun/ColorCommand.java index 7e6d238e8..d2c0ddd2e 100644 --- a/src/main/java/ml/duncte123/skybot/commands/fun/ColorCommand.java +++ b/src/main/java/ml/duncte123/skybot/commands/fun/ColorCommand.java @@ -19,7 +19,7 @@ package ml.duncte123.skybot.commands.fun; import me.duncte123.botcommons.messaging.EmbedUtils; -import ml.duncte123.skybot.objects.command.Command; +import ml.duncte123.skybot.objects.BaseCommand; import ml.duncte123.skybot.objects.command.CommandCategory; import ml.duncte123.skybot.objects.command.CommandContext; import net.dv8tion.jda.api.EmbedBuilder; @@ -33,15 +33,20 @@ import static ml.duncte123.skybot.commands.guild.owner.settings.SettingsCommand.COLOR_REGEX; import static ml.duncte123.skybot.utils.AirUtils.colorToInt; -public class ColorCommand extends Command { +public class ColorCommand extends BaseCommand { public ColorCommand() { - this.category = CommandCategory.FUN; - this.name = "color"; - this.aliases = new String[]{ - "colour", - }; - this.help = "Shows a random color"; + super( + "color", + "Look up information about a color", + CommandCategory.FUN, + null, + new String[]{ + "colour", + }, + false, + "[color]" + ); } @Override diff --git a/src/main/java/ml/duncte123/skybot/commands/fun/CrashCommand.java b/src/main/java/ml/duncte123/skybot/commands/fun/CrashCommand.java index a610a9914..6b30d8520 100644 --- a/src/main/java/ml/duncte123/skybot/commands/fun/CrashCommand.java +++ b/src/main/java/ml/duncte123/skybot/commands/fun/CrashCommand.java @@ -27,6 +27,7 @@ import static me.duncte123.botcommons.messaging.MessageUtils.sendEmbed; +// TODO: remove? public class CrashCommand extends Command { private final String[] errors = { diff --git a/src/main/java/ml/duncte123/skybot/objects/command/Command.java b/src/main/java/ml/duncte123/skybot/objects/command/Command.java index 5e9cbb65f..febff2bb3 100644 --- a/src/main/java/ml/duncte123/skybot/objects/command/Command.java +++ b/src/main/java/ml/duncte123/skybot/objects/command/Command.java @@ -44,7 +44,7 @@ @kotlin.Deprecated( message = "Replaced in kotlin", replaceWith = @ReplaceWith(expression = "BaseCommand()", imports = {"ml.duncte123.skybot.objects.BaseCommand"}), - level = DeprecationLevel.ERROR + level = DeprecationLevel.WARNING ) public abstract class Command implements ICommand { protected static final Logger LOGGER = LoggerFactory.getLogger(Command.class); diff --git a/src/main/kotlin/ml/duncte123/skybot/objects/BaseCommand.kt b/src/main/kotlin/ml/duncte123/skybot/objects/BaseCommand.kt index e74226c3d..dfd9f9432 100644 --- a/src/main/kotlin/ml/duncte123/skybot/objects/BaseCommand.kt +++ b/src/main/kotlin/ml/duncte123/skybot/objects/BaseCommand.kt @@ -33,14 +33,14 @@ abstract class BaseCommand @JvmOverloads constructor ( private val name: String, val help: String, private val category: CommandCategory = CommandCategory.MAIN, - val extraInfo: String? = null, + private val extraInfo: String? = null, private val aliases: Array = arrayOf(), private val displayAliasesInHelp: Boolean = false, val usage: String = "", val requiresArgs: Boolean = false, - val requiredArgCount: Int = 1, + private val requiredArgCount: Int = 1, val userPermissions: List = listOf(), val botPermissions: List = listOf(), @@ -62,6 +62,7 @@ abstract class BaseCommand @JvmOverloads constructor ( flags: Array ) : this(name, help, category = category, null, aliases = aliases, flags = flags) + // TODO: coroutines abstract fun execute(ctx: CommandContext) override fun executeCommand(ctx: CommandContext) { From 9a062928cfbbf5a1d32f904a9301788a5bd81aa8 Mon Sep 17 00:00:00 2001 From: duncte123 Date: Mon, 7 Feb 2022 10:32:29 +0100 Subject: [PATCH 4/4] Lint code --- build.gradle.kts | 2 +- .../java/ml/duncte123/skybot/commands/animals/SealCommand.java | 1 - src/main/kotlin/ml/duncte123/skybot/objects/BaseCommand.kt | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 1c34daa51..6bfab0587 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -278,7 +278,7 @@ shadowJar.apply { } tasks.withType { - gradleVersion = "7.2" + gradleVersion = "7.3.3" distributionType = DistributionType.ALL } diff --git a/src/main/java/ml/duncte123/skybot/commands/animals/SealCommand.java b/src/main/java/ml/duncte123/skybot/commands/animals/SealCommand.java index 544fa0eee..41d5c3759 100644 --- a/src/main/java/ml/duncte123/skybot/commands/animals/SealCommand.java +++ b/src/main/java/ml/duncte123/skybot/commands/animals/SealCommand.java @@ -21,7 +21,6 @@ import com.fasterxml.jackson.databind.JsonNode; import me.duncte123.botcommons.messaging.EmbedUtils; import ml.duncte123.skybot.objects.BaseCommand; -import ml.duncte123.skybot.objects.command.Command; import ml.duncte123.skybot.objects.command.CommandCategory; import ml.duncte123.skybot.objects.command.CommandContext; diff --git a/src/main/kotlin/ml/duncte123/skybot/objects/BaseCommand.kt b/src/main/kotlin/ml/duncte123/skybot/objects/BaseCommand.kt index dfd9f9432..abd844183 100644 --- a/src/main/kotlin/ml/duncte123/skybot/objects/BaseCommand.kt +++ b/src/main/kotlin/ml/duncte123/skybot/objects/BaseCommand.kt @@ -145,7 +145,7 @@ abstract class BaseCommand @JvmOverloads constructor ( .setSuccessAction { it.delete().queueAfter(10, TimeUnit.SECONDS) } - .build() + .build() ) MessageUtils.sendError(ctx.message) return true