1313import com .diamondfire .helpbot .sys .externalfile .ExternalFileUtil ;
1414import com .diamondfire .helpbot .util .*;
1515import net .dv8tion .jda .api .EmbedBuilder ;
16+ import net .dv8tion .jda .api .entities .*;
1617import net .dv8tion .jda .api .requests .restaction .MessageCreateAction ;
1718import net .dv8tion .jda .api .utils .FileUpload ;
1819
@@ -55,7 +56,9 @@ public Permission getPermission() {
5556
5657 @ Override
5758 protected void execute (CommandEvent event , Player player ) {
58- if (!Permission .MODERATION .hasPermission (event .getMember ()) && !event .getMember ().getEffectiveName ().equals (player .name ())) {
59+ // No player argument means the member's own (linked) account was used.
60+ boolean self = event .getArgument ("player" ) == null || event .getMember ().getEffectiveName ().equalsIgnoreCase (player .name ());
61+ if (!self && !Permission .MODERATION .hasPermission (event .getMember ())) {
5962 PresetBuilder activePunishmentsPreset = new PresetBuilder ()
6063 .withPreset (
6164 new InformativeReply (InformativeReplyType .ERROR , "You must be a moderator inorder to see other peoples history!" )
@@ -73,90 +76,66 @@ protected void execute(CommandEvent event, Player player) {
7376 .fetch ();
7477
7578 event .getMember ().getUser ().openPrivateChannel ().queue ((privateChannel ) -> {
76- // Retrieve active punishments
79+ // Recap: summary counts and any active punishments.
7780 {
78- PresetBuilder activePunishmentsPreset = new PresetBuilder ()
81+ List <Punishment > active = punishments .stream ().filter ((punishment ) -> punishment .active ).toList ();
82+
83+ PresetBuilder recapPreset = new PresetBuilder ()
7984 .withPreset (
8085 new MinecraftUserPreset (player ),
81- new InformativeReply (InformativeReplyType .INFO , "History Recap" , null )
86+ active .isEmpty ()
87+ ? new InformativeReply (InformativeReplyType .INFO , "History Recap" , null )
88+ : new InformativeReply (InformativeReplyType .ERROR , "Active Punishments" , null )
8289 );
83- EmbedBuilder embed = activePunishmentsPreset .getEmbed ();
84-
85- List <String > activePunishments = new ArrayList <>();
86- int warnings = 0 ;
87- int yearlyWarnings = 0 ;
88- for (Punishment punishment : punishments ) {
89- if (punishment .active ) {
90- activePunishments .add (punishment .toString ());
91- }
92- if (punishment .type == PunishmentType .WARN ) {
93- if (ChronoUnit .DAYS .between (punishment .startTime .toInstant (), Instant .now ()) <= 365 ) {
94- yearlyWarnings ++;
95- }
96- warnings ++;
97- }
98- }
99- punishments .removeIf ((punishment ) -> punishment .active );
90+ EmbedBuilder embed = recapPreset .getEmbed ();
10091
101- if (activePunishments .size () > 0 ) {
102- activePunishmentsPreset .withPreset (new InformativeReply (InformativeReplyType .ERROR , "Active Punishments" , null ));
103- EmbedUtil .addFields (embed , activePunishments , "" , true );
104-
105- String duration = null ;
106- int warningReq = 0 ;
107- if (warnings < 4 && warnings > 1 ) {
108- duration = "30 minute" ;
109- warningReq = 4 - warnings ;
110- } else if (warnings < 5 ) {
111- duration = "1 hour" ;
112- warningReq = 5 - warnings ;
113- } else if (warnings < 6 ) {
114- duration = "12 hour" ;
115- warningReq = 6 - warnings ;
92+ if (punishments .isEmpty ()) {
93+ embed .setDescription ("No punishments here, keep up the good work!" );
94+ } else {
95+ StringBuilder description = new StringBuilder (summarize (punishments ));
96+ for (Punishment punishment : active ) {
97+ description .append ("\n \n " ).append (punishment .format ());
11698 }
11799
118- // if (duration != null) {
119- // embed.addField("Tempban", String.format("\u26A0 If you receive **%s** more active %s, you will receive a **%s** tempban!", warningReq, StringUtil.sCheck("warning", warningReq), duration), false);
120- // }
121-
122- } else if (punishments .size () == 0 ) {
123- embed .setDescription ("No punishments here, keep up the good work!" );
100+ embed .setDescription (description .length () > MessageEmbed .DESCRIPTION_MAX_LENGTH
101+ ? description .substring (0 , MessageEmbed .DESCRIPTION_MAX_LENGTH - 1 ) + "…"
102+ : description .toString ());
124103 }
125- //
126- // if (yearlyWarnings > 10) {
127- // embed.setColor(Color.RED);
128- // embed.addField("Tempban", String.format("\u26A0 If you receive **%s** more %s this year, you will receive a **45** day tempban!", 20 - yearlyWarnings, StringUtil.sCheck("warning", 20 - yearlyWarnings)), false);
129- // }
130104
131105 msgs .add (privateChannel .sendMessageEmbeds (embed .build ()));
106+ punishments .removeIf ((punishment ) -> punishment .active );
132107 }
133108
134- // Retrieve normal punishments
135- {
136- PresetBuilder punishmentsPreset = new PresetBuilder ()
137- .withPreset (
138- new InformativeReply (InformativeReplyType .INFO , "Punishment History" , null )
139- );
140- EmbedBuilder presetBuilder = punishmentsPreset .getEmbed ();
141- List <String > punishmentStrings = new ArrayList <>();
109+ // Past punishments, newest first, split over as many embeds as needed.
110+ if (!punishments .isEmpty ()) {
111+ punishments .sort (Comparator .comparing ((Punishment punishment ) -> punishment .startTime ).reversed ());
112+
113+ List <MessageEmbed > embeds = new ArrayList <>();
114+ StringBuilder page = new StringBuilder ();
115+ int total = 0 ;
142116 for (Punishment punishment : punishments ) {
143- punishmentStrings .add (punishment .toString ());
117+ String entry = punishment .format ();
118+ if (page .length () + entry .length () + 2 > MessageEmbed .DESCRIPTION_MAX_LENGTH ) {
119+ embeds .add (historyEmbed (page .toString (), embeds .isEmpty ()));
120+ page .setLength (0 );
121+ }
122+
123+ page .append (page .isEmpty () ? "" : "\n \n " ).append (entry );
124+ total += entry .length () + 2 ;
144125 }
126+ embeds .add (historyEmbed (page .toString (), embeds .isEmpty ()));
145127
146- EmbedUtil .addFields (presetBuilder , punishmentStrings , "" , "" , true );
147- if (punishmentStrings .size () == 0 ) {
148- } else if (presetBuilder .isValidLength ()) {
149- msgs .add (privateChannel .sendMessageEmbeds (presetBuilder .build ()));
128+ if (embeds .size () <= Message .MAX_EMBED_COUNT && total < MessageEmbed .EMBED_MAX_LENGTH_BOT - 100 ) {
129+ msgs .add (privateChannel .sendMessageEmbeds (embeds ));
150130 } else {
151131 try {
152132 File sendFile = ExternalFileUtil .generateFile ("history.txt" );
153- Files .writeString (sendFile .toPath (), String .join ("\n " , punishmentStrings ));
133+ Files .writeString (sendFile .toPath (), String .join ("\n \n " , punishments . stream (). map ( Punishment :: toString ). toList () ));
154134 msgs .add (privateChannel .sendFiles (FileUpload .fromData (sendFile )));
155135 } catch (IOException exception ) {
156136 exception .printStackTrace ();
157137 }
158138 }
159-
160139 }
161140
162141 MessageCreateAction action = msgs .get (0 );
@@ -187,4 +166,29 @@ protected void execute(CommandEvent event, Player player) {
187166 });
188167 }
189168
169+ private static MessageEmbed historyEmbed (String description , boolean first ) {
170+ EmbedBuilder embed = new PresetBuilder ()
171+ .withPreset (new InformativeReply (InformativeReplyType .INFO , first ? "Punishment History" : null , null ))
172+ .getEmbed ();
173+ return embed .setDescription (description ).build ();
174+ }
175+
176+ private static String summarize (List <Punishment > punishments ) {
177+ Map <PunishmentType , Integer > counts = new EnumMap <>(PunishmentType .class );
178+ int yearlyWarnings = 0 ;
179+ for (Punishment punishment : punishments ) {
180+ counts .merge (punishment .type , 1 , Integer ::sum );
181+ if (punishment .type == PunishmentType .WARN && ChronoUnit .DAYS .between (punishment .startTime .toInstant (), Instant .now ()) <= 365 ) {
182+ yearlyWarnings ++;
183+ }
184+ }
185+
186+ int warnings = counts .getOrDefault (PunishmentType .WARN , 0 );
187+ return String .format ("**%s** %s (%s in the past year) · **%s** %s · **%s** %s · **%s** %s" ,
188+ warnings , StringUtil .sCheck ("warning" , warnings ), yearlyWarnings ,
189+ counts .getOrDefault (PunishmentType .MUTE , 0 ), StringUtil .sCheck ("mute" , counts .getOrDefault (PunishmentType .MUTE , 0 )),
190+ counts .getOrDefault (PunishmentType .KICK , 0 ), StringUtil .sCheck ("kick" , counts .getOrDefault (PunishmentType .KICK , 0 )),
191+ counts .getOrDefault (PunishmentType .BAN , 0 ), StringUtil .sCheck ("ban" , counts .getOrDefault (PunishmentType .BAN , 0 )));
192+ }
193+
190194}
0 commit comments