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