improve urban in discord

This commit is contained in:
Chayapak 2023-07-22 16:39:40 +07:00
parent f038c4f54f
commit 621191103d

View file

@ -7,6 +7,7 @@ import com.google.gson.JsonObject;
import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.command.Command; import land.chipmunk.chayapak.chomens_bot.command.Command;
import land.chipmunk.chayapak.chomens_bot.command.CommandContext; import land.chipmunk.chayapak.chomens_bot.command.CommandContext;
import land.chipmunk.chayapak.chomens_bot.command.DiscordCommandContext;
import land.chipmunk.chayapak.chomens_bot.command.TrustLevel; import land.chipmunk.chayapak.chomens_bot.command.TrustLevel;
import land.chipmunk.chayapak.chomens_bot.util.HttpUtilities; import land.chipmunk.chayapak.chomens_bot.util.HttpUtilities;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -34,6 +35,8 @@ false
public Component execute (CommandContext context, String[] args, String[] fullArgs) { public Component execute (CommandContext context, String[] args, String[] fullArgs) {
final Bot bot = context.bot; final Bot bot = context.bot;
final boolean discord = context instanceof DiscordCommandContext;
final String term = String.join(" ", args); final String term = String.join(" ", args);
final Gson gson = new Gson(); final Gson gson = new Gson();
@ -53,7 +56,12 @@ false
if (list.isEmpty()) context.sendOutput(Component.text("No results found").color(NamedTextColor.RED)); if (list.isEmpty()) context.sendOutput(Component.text("No results found").color(NamedTextColor.RED));
Component discordComponent = Component.text("*Showing only 3 results because Discord*");
int count = 0;
for (JsonElement element : list) { for (JsonElement element : list) {
if (count >= 3) break;
final JsonObject definitionObject = element.getAsJsonObject(); final JsonObject definitionObject = element.getAsJsonObject();
final String word = definitionObject.get("word").getAsString(); final String word = definitionObject.get("word").getAsString();
@ -92,15 +100,31 @@ false
} }
} }
final Component component = Component.translatable( if (discord) {
"[%s] %s - %s", discordComponent = discordComponent
Component.text("Urban").color(NamedTextColor.RED), .append(
Component.text(word).color(NamedTextColor.GRAY), Component.translatable(
definitionComponent "%s - %s",
).color(NamedTextColor.DARK_GRAY); Component.text(word).color(NamedTextColor.GRAY),
definitionComponent
).color(NamedTextColor.DARK_GRAY)
)
.append(Component.newline());
context.sendOutput(component); count++;
} else {
final Component component = Component.translatable(
"[%s] %s - %s",
Component.text("Urban").color(NamedTextColor.RED),
Component.text(word).color(NamedTextColor.GRAY),
definitionComponent
).color(NamedTextColor.DARK_GRAY);
context.sendOutput(component);
}
} }
if (discord) context.sendOutput(discordComponent);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED)); context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED));