Remove String.format() overload in Context

This commit is contained in:
anthorob committed 2024-12-12 21:22:11 -05:00
1 parent bae37fc8b1
commit 6b453bb7e2
7 files changed
+40 -33

No files matched your search

@@ -67,7 +67,7 @@ public class CryptoPriceCommand extends AbstractCommand {
NumberFormat dollarFormat = NumberFormat.getCurrencyInstance(usa); NumberFormat dollarFormat = NumberFormat.getCurrencyInstance(usa);
dollarFormat.setMaximumFractionDigits(24); dollarFormat.setMaximumFractionDigits(24);
String symbol = context.getOption("symbol", CommandOption::getAsString); String symbol = context.getOption("symbol", CommandOption::getAsString).toUpperCase();
String currency = context.getOption("currency", "USD", CommandOption::getAsString).toUpperCase(); String currency = context.getOption("currency", "USD", CommandOption::getAsString).toUpperCase();
try { try {
@@ -91,10 +91,10 @@ public class CryptoPriceCommand extends AbstractCommand {
double value = json.getDouble(finalCurrency); double value = json.getDouble(finalCurrency);
String price = dollarFormat.format(value); String price = dollarFormat.format(value);
asyncMessage.edit("The current price of %s in %s is %s", symbol, finalCurrency, price); asyncMessage.edit(String.format("The current price of %s in %s is %s", symbol, finalCurrency, price));
} catch (JSONException | IllegalArgumentException | ArithmeticException e) { } catch (Exception e) {
asyncMessage.edit("Error getting price of %s!", symbol); asyncMessage.edit(String.format("Error getting price of %s!", symbol));
LogHandler.log(e); LogHandler.log(e);
} }
}); });
@@ -7,6 +7,7 @@ package me.bobert.netneutrality.command.git;
import me.bobert.netneutrality.command.AbstractCommand; import me.bobert.netneutrality.command.AbstractCommand;
import me.bobert.netneutrality.command.CommandType; import me.bobert.netneutrality.command.CommandType;
import me.bobert.netneutrality.context.SlashCommandContext;
import me.bobert.netneutrality.core.command.CommandExecutionResult; import me.bobert.netneutrality.core.command.CommandExecutionResult;
import me.bobert.netneutrality.core.command.builder.CommandOptionsBuilder; import me.bobert.netneutrality.core.command.builder.CommandOptionsBuilder;
import me.bobert.netneutrality.core.context.CommandContext; import me.bobert.netneutrality.core.context.CommandContext;
@@ -60,7 +61,7 @@ public class ReportIssueCommand extends AbstractCommand {
return CommandExecutionResult.error("Command is not configured, failed to create issue."); return CommandExecutionResult.error("Command is not configured, failed to create issue.");
} }
TaskManager.getInstance().insert(context, new ReportIssueTask()); TaskManager.getInstance().insert(context, new ReportIssueTask(context instanceof SlashCommandContext));
return CommandExecutionResult.none(); return CommandExecutionResult.none();
} }
@@ -113,14 +113,14 @@ public class CommandProcessor {
String[] possibleCommands = Arrays.stream(context.getFullCommand().split(" ")).skip(1).toArray(String[]::new); String[] possibleCommands = Arrays.stream(context.getFullCommand().split(" ")).skip(1).toArray(String[]::new);
if (possibleCommands.length == 0) { if (possibleCommands.length == 0) {
if (!commandOptions.getSubcommandGroups().isEmpty()) { if (!commandOptions.getSubcommandGroups().isEmpty()) {
context.reply("Invalid usage for command: missing subcommand.\n" + context.reply(String.format("Invalid usage for command: missing subcommand.\n" +
"Use **/help %s** for information on how to use this command", context.getCommand().getCommandName()); "Use **/help %s** for information on how to use this command", context.getCommand().getCommandName()));
return false; return false;
} }
if (!commandOptions.getSubcommands().isEmpty()) { if (!commandOptions.getSubcommands().isEmpty()) {
context.reply("Invalid usage for command: missing subcommand.\n" + context.reply(String.format("Invalid usage for command: missing subcommand.\n" +
"Use **/help %s** for information on how to use this command", context.getCommand().getCommandName()); "Use **/help %s** for information on how to use this command", context.getCommand().getCommandName()));
return false; return false;
} }
@@ -130,8 +130,8 @@ public class CommandProcessor {
SubcommandGroupData group = commandOptions.getSubcommandGroups().stream().filter(data -> data.getName().equals(possibleCommands[0])).findFirst().orElse(null); SubcommandGroupData group = commandOptions.getSubcommandGroups().stream().filter(data -> data.getName().equals(possibleCommands[0])).findFirst().orElse(null);
if (group != null) { if (group != null) {
if (possibleCommands.length < 2) { if (possibleCommands.length < 2) {
context.reply("Invalid usage for command: missing subcommand.\n" + context.reply(String.format("Invalid usage for command: missing subcommand.\n" +
"Use **/help %s** for information on how to use this command", context.getCommand().getCommandName()); "Use **/help %s** for information on how to use this command", context.getCommand().getCommandName()));
return false; // missing subcommand return false; // missing subcommand
} }
@@ -163,8 +163,8 @@ public class CommandProcessor {
int optional = options.stream().filter(not(OptionData::isRequired)).toList().size(); int optional = options.stream().filter(not(OptionData::isRequired)).toList().size();
if (arguments.size() < required) { if (arguments.size() < required) {
context.reply("Invalid usage for command: missing one or more required arguments\n" + context.reply(String.format("Invalid usage for command: missing one or more required arguments\n" +
"Use **/help %s** for information on how to use this command", context.getCommand().getCommandName()); "Use **/help %s** for information on how to use this command", context.getCommand().getCommandName()));
return false; return false;
} }
@@ -180,8 +180,8 @@ public class CommandProcessor {
} }
if (arguments.size() > required + optional) { if (arguments.size() > required + optional) {
context.reply("Invalid usage for command: too many arguments\n" + context.reply(String.format("Invalid usage for command: too many arguments\n" +
"Use **/help %s** for information on how to use this command", context.getCommand().getCommandName()); "Use **/help %s** for information on how to use this command", context.getCommand().getCommandName()));
return false; return false;
} }
@@ -73,10 +73,18 @@ public interface Context {
reply(messageCreateData, false, components, asyncMessage -> {}); reply(messageCreateData, false, components, asyncMessage -> {});
} }
default void reply(MessageCreateData messageCreateData, Consumer<AsyncContext> consumer) {
reply(messageCreateData, false, List.of(), consumer);
}
default void reply(String message) { default void reply(String message) {
replyWithLimit(message); replyWithLimit(message);
} }
default void reply(String message, Consumer<AsyncContext> consumer) {
reply(MessageCreateData.fromContent(message), consumer);
}
default void reply(MessageEmbed embed) { default void reply(MessageEmbed embed) {
reply(List.of(embed)); reply(List.of(embed));
} }
@@ -85,10 +93,6 @@ public interface Context {
reply(MessageCreateData.fromEmbeds(embeds)); reply(MessageCreateData.fromEmbeds(embeds));
} }
default void reply(String format, Object... arguments) {
replyWithLimit(String.format(format, arguments));
}
default void edit(MessageEditData messageEditData, boolean ephemeral, List<LayoutComponent> components, Consumer<AsyncContext> consumer) { default void edit(MessageEditData messageEditData, boolean ephemeral, List<LayoutComponent> components, Consumer<AsyncContext> consumer) {
if (isAcknowledged() && getInteractionHook() != null) { if (isAcknowledged() && getInteractionHook() != null) {
getInteractionHook().editOriginal(messageEditData).setComponents(components).queue(message1 -> consumer.accept(new AsyncContext(message1))); getInteractionHook().editOriginal(messageEditData).setComponents(components).queue(message1 -> consumer.accept(new AsyncContext(message1)));
@@ -122,10 +126,6 @@ public interface Context {
edit(List.of(embed), components); edit(List.of(embed), components);
} }
default void edit(String format, Object... arguments) {
edit(String.format(format, arguments));
}
default void async(MessageCreateData messageCreateData, Consumer<AsyncContext> consumer) { default void async(MessageCreateData messageCreateData, Consumer<AsyncContext> consumer) {
reply(messageCreateData, false, List.of(), consumer); reply(messageCreateData, false, List.of(), consumer);
} }
@@ -146,10 +146,6 @@ public interface Context {
replyEphemeral(MessageCreateData.fromContent(message)); replyEphemeral(MessageCreateData.fromContent(message));
} }
default void replyEphemeral(String format, Object... arguments) {
replyEphemeral(String.format(format, arguments));
}
default void replyEphemeral(MessageEmbed... embeds) { default void replyEphemeral(MessageEmbed... embeds) {
replyEphemeral(MessageCreateData.fromEmbeds(embeds)); replyEphemeral(MessageCreateData.fromEmbeds(embeds));
} }
@@ -80,16 +80,12 @@ public class TaskManager {
String id = task.getId(); String id = task.getId();
if (tasks.containsKey(id)) { if (tasks.containsKey(id)) {
context.reply("A task with the same id already exists!"); context.reply("A task with the same ID already exists!");
return; return;
} }
task.replyToInteraction(context); task.replyToInteraction(context);
if (task instanceof Deletable) {
deletableTasksContext.put(task.getId(), context);
}
if (task.isInsertable()) { if (task.isInsertable()) {
logger.info("Inserting task: {}", task); logger.info("Inserting task: {}", task);
@@ -100,6 +96,10 @@ public class TaskManager {
tasks.put(id, task); tasks.put(id, task);
} }
if (task instanceof Deletable) {
deletableTasksContext.put(task.getId(), context);
}
if (task instanceof WithChildren taskWithChildren) { if (task instanceof WithChildren taskWithChildren) {
List<Task> childrens = taskWithChildren.getChildTasks(); List<Task> childrens = taskWithChildren.getChildTasks();
@@ -25,12 +25,22 @@ import org.gitlab4j.api.models.Issue;
public class ReportIssueTask extends ModalTask { public class ReportIssueTask extends ModalTask {
private static final Logger logger = LogManager.getLogger(ReportIssueTask.class); private static final Logger logger = LogManager.getLogger(ReportIssueTask.class);
private final boolean insertable;
public ReportIssueTask(boolean insertable) {
this.insertable = insertable;
}
@Override @Override
public String getName() { public String getName() {
return "report-issue"; return "report-issue";
} }
@Override
public boolean isInsertable() {
return insertable;
}
@Override @Override
public void replyToInteraction(CommandContext context) { public void replyToInteraction(CommandContext context) {
TextInput title = TextInput.create("title", "Title", TextInputStyle.SHORT) TextInput title = TextInput.create("title", "Title", TextInputStyle.SHORT)
@@ -92,7 +92,7 @@ public class ChannelMessagePurger {
int tracker = toPurge; int tracker = toPurge;
int iteration = tracker / 100; int iteration = tracker / 100;
boolean olderThan2Weeks = false; boolean olderThan2Weeks = false;
context.edit("Purging iterations to do: %d", iteration); context.edit(String.format("Purging iterations to do: %d", iteration));
try { try {
for (int i = 0; i < iteration; i++) { for (int i = 0; i < iteration; i++) {