impl a EmbedDescriptionBuilder

This commit is contained in:
Lee 2024-07-02 01:51:13 +01:00
parent 4f975ab07a
commit 317eaaec8a
4 changed files with 62 additions and 29 deletions

@ -0,0 +1,40 @@
package cc.fascinated.bat.common;
import lombok.NonNull;
/**
* @author Fascinated (fascinated7)
*/
public class EmbedDescriptionBuilder {
/**
* Where the description is stored
*/
private final StringBuilder builder = new StringBuilder();
public EmbedDescriptionBuilder(String title) {
builder.append("**").append(title).append("**").append("\n");
}
@NonNull
public EmbedDescriptionBuilder appendLine(@NonNull String line, boolean arrow) {
builder.append(arrow ? "" : "").append(line).append("\n");
return this;
}
@NonNull
public EmbedDescriptionBuilder appendSubtitle(@NonNull String title) {
builder.append("**").append(title).append("**").append("\n");
return this;
}
@NonNull
public EmbedDescriptionBuilder emptyLine() {
builder.append("\n");
return this;
}
@NonNull
public String build() {
return builder.toString();
}
}

@ -2,6 +2,7 @@ package cc.fascinated.bat.features.base.commands.general;
import cc.fascinated.bat.command.BatCommand; import cc.fascinated.bat.command.BatCommand;
import cc.fascinated.bat.command.CommandInfo; import cc.fascinated.bat.command.CommandInfo;
import cc.fascinated.bat.common.EmbedDescriptionBuilder;
import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.common.NumberFormatter; import cc.fascinated.bat.common.NumberFormatter;
import cc.fascinated.bat.common.TimeUtils; import cc.fascinated.bat.common.TimeUtils;
@ -42,15 +43,16 @@ public class BotStatsCommand extends BatCommand {
JDA jda = DiscordService.JDA; JDA jda = DiscordService.JDA;
event.replyEmbeds(EmbedUtils.genericEmbed().setDescription( event.replyEmbeds(EmbedUtils.genericEmbed().setDescription(
"**Bot Statistics**\n" + new EmbedDescriptionBuilder("Bat Statistics")
"➜ Guilds: **%s**\n".formatted(NumberFormatter.format(jda.getGuilds().size())) + .appendLine("Guilds: **%s**".formatted(NumberFormatter.format(jda.getGuilds().size())), true)
"➜ Users: **%s**\n".formatted(NumberFormatter.format(jda.getUsers().size())) + .appendLine("Users: **%s**".formatted(NumberFormatter.format(jda.getUsers().size())), true)
"➜ Gateway Ping: **%sms**\n".formatted(jda.getGatewayPing()) + .appendLine("Gateway Ping: **%sms**".formatted(jda.getGatewayPing()), true)
"\n" + .emptyLine()
"**Bat Statistics**\n" + .appendSubtitle("Bot Statistics")
"➜ Uptime: **%s**\n".formatted(TimeUtils.format(bean.getUptime())) + .appendLine("Uptime: **%s**".formatted(TimeUtils.format(bean.getUptime())), true)
"➜ Cached Guilds: **%s**\n".formatted(NumberFormatter.format(guildService.getGuilds().size())) + .appendLine("Cached Guilds: **%s**".formatted(NumberFormatter.format(guildService.getGuilds().size())), true)
"➜ Cached Users: **%s**".formatted(NumberFormatter.format(userService.getUsers().size())) .appendLine("Cached Users: **%s**".formatted(NumberFormatter.format(userService.getUsers().size())), true)
.build()
).build()).queue(); ).build()).queue();
} }
} }

@ -2,6 +2,7 @@ package cc.fascinated.bat.features.messagesnipe.command;
import cc.fascinated.bat.command.BatSubCommand; import cc.fascinated.bat.command.BatSubCommand;
import cc.fascinated.bat.command.CommandInfo; import cc.fascinated.bat.command.CommandInfo;
import cc.fascinated.bat.common.EmbedDescriptionBuilder;
import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.common.PasteUtils; import cc.fascinated.bat.common.PasteUtils;
import cc.fascinated.bat.features.messagesnipe.MessageSnipeFeature; import cc.fascinated.bat.features.messagesnipe.MessageSnipeFeature;
@ -32,23 +33,13 @@ public class DeletedSubCommand extends BatSubCommand {
} }
User author = message.getMessage().getAuthor(); User author = message.getMessage().getAuthor();
String content = message.getMessage().getContentDisplay(); String content = message.getMessage().getContentStripped();
String formattedContent = content.length() > 512 ? PasteUtils.uploadPaste(content).getUrl() : "```\n%s\n```".formatted(content);
event.replyEmbeds(EmbedUtils.genericEmbed() event.replyEmbeds(EmbedUtils.genericEmbed()
.setDescription(""" .setDescription(new EmbedDescriptionBuilder("Deleted Message Snipe")
**Deleted Message Snipe** .appendLine("Author: **%s** (%s)".formatted(author.getAsMention(), author.getId()), true)
Author: **%s** (%s) .appendLine("Deleted: <t:%d:R>".formatted(message.getDeletedDate().getTime() / 1000), true)
Deleted: <t:%d:R> .appendLine("Content: %s".formatted(formattedContent), true)
Content: %s .build()).build()).queue();
""".formatted(
author.getAsMention(),
author.getId(),
message.getDeletedDate().getTime() / 1000,
content.length() > 512 ? PasteUtils.uploadPaste(content).getUrl() :
"""
```
%s
```
""".formatted(content)
)).build()).queue();
} }
} }

@ -58,7 +58,7 @@ public class DiscordMessageService extends ListenerAdapter {
event.getChannel().getId(), event.getChannel().getId(),
event.getGuild().getId(), event.getGuild().getId(),
event.getAuthor().getId(), event.getAuthor().getId(),
event.getMessage().getContentRaw(), event.getMessage().getContentStripped(),
false false
)); ));
} }
@ -68,10 +68,10 @@ public class DiscordMessageService extends ListenerAdapter {
Optional<DiscordMessage> message = discordMessageRepository.findById(event.getMessageId()); Optional<DiscordMessage> message = discordMessageRepository.findById(event.getMessageId());
if (message.isPresent()) { if (message.isPresent()) {
DiscordMessage discordMessage = message.get(); DiscordMessage discordMessage = message.get();
if (discordMessage.getContent().equals(event.getMessage().getContentRaw())) { if (discordMessage.getContent().equals(event.getMessage().getContentStripped())) {
return; return;
} }
discordMessage.setContent(event.getMessage().getContentRaw()); discordMessage.setContent(event.getMessage().getContentStripped());
discordMessageRepository.save(discordMessage); discordMessageRepository.save(discordMessage);
} }