Skip to content

Commit a354134

Browse files
authored
Merge pull request #6 from Java-Discord/andrew/time_cleanup
Cleanup of Time Diffs and Formatting
2 parents e128837 + dc3e553 commit a354134

File tree

9 files changed

+112
-122
lines changed

9 files changed

+112
-122
lines changed

src/main/java/com/javadiscord/javabot/commands/moderation/Report.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package com.javadiscord.javabot.commands.moderation;
22

3-
import com.javadiscord.javabot.other.Database;
4-
import com.javadiscord.javabot.other.Embeds;
53
import com.jagrosh.jdautilities.command.Command;
64
import com.jagrosh.jdautilities.command.CommandEvent;
5+
import com.javadiscord.javabot.other.Database;
6+
import com.javadiscord.javabot.other.Embeds;
7+
import com.javadiscord.javabot.other.TimeUtils;
78
import net.dv8tion.jda.api.EmbedBuilder;
89
import net.dv8tion.jda.api.entities.Member;
910
import net.dv8tion.jda.api.entities.MessageChannel;
1011

1112
import java.awt.*;
1213
import java.time.LocalDateTime;
13-
import java.time.format.DateTimeFormatter;
1414
import java.util.Arrays;
1515
import java.util.Date;
16-
import java.util.Locale;
1716

1817
public class Report extends Command {
1918

@@ -25,10 +24,6 @@ protected void execute(CommandEvent event) {
2524
Member member = null;
2625
String reason = null;
2726

28-
29-
30-
31-
3227
if (event.getMessage().getReferencedMessage() == null) {
3328

3429
try {
@@ -70,8 +65,6 @@ protected void execute(CommandEvent event) {
7065

7166
}
7267

73-
DateTimeFormatter dtw = DateTimeFormatter.ofPattern("EEE',' dd/MM/yyyy',' HH:mm", new Locale("en"));
74-
LocalDateTime now = LocalDateTime.now();
7568
MessageChannel reportChannel = Database.configChannel(event, "report_cid");
7669

7770
EmbedBuilder eb = new EmbedBuilder()
@@ -81,7 +74,7 @@ protected void execute(CommandEvent event) {
8174
.addField("ID", "```" + member.getId() + "```", true)
8275
.addField("Reported by", "```" + event.getMessage().getAuthor().getAsTag() + "```", true)
8376
.addField("Channel", "```#" + event.getMessage().getChannel().getName() + "```", true)
84-
.addField("Reported on", "```" + dtw.format(now) + "```", true)
77+
.addField("Reported on", "```" + LocalDateTime.now().format(TimeUtils.STANDARD_FORMATTER) + "```", true)
8578
.addField("Reason", "```" + reason + "```", false)
8679
.setFooter(event.getAuthor().getAsTag(), event.getAuthor().getEffectiveAvatarUrl())
8780
.setTimestamp(new Date().toInstant());
Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.javadiscord.javabot.commands.other;
22

3-
import com.javadiscord.javabot.other.Embeds;
43
import com.google.gson.JsonObject;
54
import com.google.gson.JsonParser;
65
import com.jagrosh.jdautilities.command.Command;
76
import com.jagrosh.jdautilities.command.CommandEvent;
7+
import com.javadiscord.javabot.other.Embeds;
88
import com.mongodb.client.MongoCollection;
99
import com.mongodb.client.MongoDatabase;
1010
import org.bson.Document;
@@ -39,44 +39,30 @@ public Version () {
3939
}
4040

4141
protected void execute(CommandEvent event) {
42-
43-
MongoDatabase database = mongoClient.getDatabase("other");
44-
MongoCollection<Document> collection = database.getCollection("config");
45-
4642
String[] args = event.getArgs().split("\\s+");
43+
String input;
44+
if (args.length > 0 && args[0].equalsIgnoreCase("now")) {
45+
input = LocalDate.now().format(DateTimeFormatter.ofPattern("YYYY-MM.dd"));
46+
} else if (args.length > 0) {
47+
input = args[0];
48+
} else {
49+
event.reply(Embeds.syntaxError("version now|Text", event));
50+
return;
51+
}
4752

48-
try {
49-
50-
String input;
51-
52-
if (args[0].equalsIgnoreCase("now")) {
53-
54-
LocalDate currentdate = LocalDate.now();
55-
String currentDay = currentdate.format(DateTimeFormatter.ofPattern("dd"));
56-
String currentMonth = currentdate.format(DateTimeFormatter.ofPattern("MM"));
57-
String currentYear = currentdate.format(DateTimeFormatter.ofPattern("YYYY")).substring(2);
58-
59-
input = currentYear + "-" + currentMonth + "." + currentDay;
60-
61-
} else {
62-
input = args[0];
63-
}
64-
65-
event.reply(Embeds.configEmbed(event, "Version", "Version succesfully changed to", null, input, true));
66-
67-
Document Query = new Document();
68-
Query.append("name", "Java#9523");
53+
event.reply(Embeds.configEmbed(event, "Version", "Version succesfully changed to", null, input, true));
6954

70-
Document SetData = new Document();
71-
SetData.append("version", input);
55+
Document Query = new Document();
56+
Query.append("name", "Java#9523");
7257

73-
Document update = new Document();
74-
update.append("$set", SetData);
58+
Document SetData = new Document();
59+
SetData.append("version", input);
7560

76-
collection.updateOne(Query, update);
61+
Document update = new Document();
62+
update.append("$set", SetData);
7763

78-
} catch (ArrayIndexOutOfBoundsException e) {
79-
event.reply(Embeds.syntaxError("version now|Text", event));
80-
}
64+
MongoDatabase database = mongoClient.getDatabase("other");
65+
MongoCollection<Document> collection = database.getCollection("config");
66+
collection.updateOne(Query, update);
8167
}
8268
}

src/main/java/com/javadiscord/javabot/commands/user_commands/IDCalc.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.jagrosh.jdautilities.command.Command;
44
import com.jagrosh.jdautilities.command.CommandEvent;
5+
import com.javadiscord.javabot.other.TimeUtils;
56
import net.dv8tion.jda.api.EmbedBuilder;
67
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
78

@@ -32,8 +33,7 @@ public static void exCommand (CommandEvent event) {
3233
long unixTimeStampMilliseconds = Input / 4194304 + 1420070400000L;
3334
long unixTimeStamp = unixTimeStampMilliseconds / 1000;
3435

35-
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE',' dd/MM/yyyy',' HH:mm", new Locale("en"));
36-
String Date = Instant.ofEpochMilli(unixTimeStampMilliseconds).atZone(ZoneId.of("GMT")).format(dtf);
36+
String Date = Instant.ofEpochMilli(unixTimeStampMilliseconds).atZone(ZoneId.of("GMT")).format(TimeUtils.STANDARD_FORMATTER);
3737

3838
EmbedBuilder eb = new EmbedBuilder()
3939
.setAuthor("ID-Calculator")
@@ -51,8 +51,7 @@ public static void exCommand (SlashCommandEvent event, long id) {
5151
long unixTimeStampMilliseconds = id / 4194304 + 1420070400000L;
5252
long unixTimeStamp = unixTimeStampMilliseconds / 1000;
5353

54-
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE',' dd/MM/yyyy',' HH:mm", new Locale("en"));
55-
String Date = Instant.ofEpochMilli(unixTimeStampMilliseconds).atZone(ZoneId.of("GMT")).format(dtf);
54+
String Date = Instant.ofEpochMilli(unixTimeStampMilliseconds).atZone(ZoneId.of("GMT")).format(TimeUtils.STANDARD_FORMATTER);
5655

5756
EmbedBuilder eb = new EmbedBuilder()
5857
.setAuthor("ID-Calculator")

src/main/java/com/javadiscord/javabot/commands/user_commands/Profile.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.javadiscord.javabot.commands.user_commands;
22

3-
import com.javadiscord.javabot.commands.other.qotw.Leaderboard;
4-
import com.javadiscord.javabot.other.Database;
5-
import com.javadiscord.javabot.other.Misc;
63
import com.jagrosh.jdautilities.command.Command;
74
import com.jagrosh.jdautilities.command.CommandEvent;
5+
import com.javadiscord.javabot.commands.other.qotw.Leaderboard;
6+
import com.javadiscord.javabot.other.Database;
7+
import com.javadiscord.javabot.other.TimeUtils;
88
import com.mongodb.client.MongoCollection;
99
import com.mongodb.client.MongoDatabase;
1010
import net.dv8tion.jda.api.EmbedBuilder;
@@ -15,10 +15,9 @@
1515
import org.bson.Document;
1616

1717
import java.awt.*;
18-
import java.time.format.DateTimeFormatter;
19-
import java.util.Date;
18+
import java.time.Duration;
19+
import java.time.OffsetDateTime;
2020
import java.util.List;
21-
import java.util.Locale;
2221

2322
import static com.javadiscord.javabot.events.Startup.mongoClient;
2423

@@ -48,8 +47,8 @@ public static void exCommand(CommandEvent event) {
4847
highestRole = "everyone";
4948
}
5049

51-
String timeJoined = member.getTimeJoined().format(DateTimeFormatter.ofPattern("EEE',' dd/MM/yyyy',' HH:mm", new Locale("en")));
52-
String timeCreated = member.getTimeCreated().format(DateTimeFormatter.ofPattern("EEE',' dd/MM/yyyy',' HH:mm", new Locale("en")));
50+
String timeJoined = member.getTimeJoined().format(TimeUtils.STANDARD_FORMATTER);
51+
String timeCreated = member.getTimeCreated().format(TimeUtils.STANDARD_FORMATTER);
5352

5453
Color color = null;
5554
String colorText;
@@ -149,9 +148,11 @@ public static void exCommand(CommandEvent event) {
149148
int qotwCount = Database.getMemberInt(collection, member, "qotwpoints");
150149
int warnCount = Database.getMemberInt(collection, member, "warns");
151150

152-
String joinDiff = " (" + Misc.getDateDiff(Date.from(member.getTimeJoined().toInstant()), Date.from(new Date().toInstant())) + " ago)";
153-
String createDiff = " (" + Misc.getDateDiff(Date.from(member.getUser().getTimeCreated().toInstant()), Date.from(new Date().toInstant())) + " ago)";
151+
Duration memberDuration = Duration.between(OffsetDateTime.now(member.getTimeJoined().getOffset()), member.getTimeJoined());
152+
Duration createDuration = Duration.between(OffsetDateTime.now(member.getTimeCreated().getOffset()), member.getTimeCreated());
154153

154+
String joinDiff = TimeUtils.formatDuration(memberDuration);
155+
String createDiff = TimeUtils.formatDuration(createDuration);
155156

156157
EmbedBuilder eb = new EmbedBuilder()
157158
.setTitle(statusEmote + " " + member.getUser().getAsTag() + " " + botBadge + boostBadge + badges)
@@ -184,8 +185,8 @@ public static void exCommand(SlashCommandEvent event, Member member) {
184185
highestRole = "everyone";
185186
}
186187

187-
String timeJoined = member.getTimeJoined().format(DateTimeFormatter.ofPattern("EEE',' dd/MM/yyyy',' HH:mm", new Locale("en")));
188-
String timeCreated = member.getTimeCreated().format(DateTimeFormatter.ofPattern("EEE',' dd/MM/yyyy',' HH:mm", new Locale("en")));
188+
String timeJoined = member.getTimeJoined().format(TimeUtils.STANDARD_FORMATTER);
189+
String timeCreated = member.getTimeCreated().format(TimeUtils.STANDARD_FORMATTER);
189190

190191
Color color = null;
191192
String colorText;
@@ -285,9 +286,8 @@ public static void exCommand(SlashCommandEvent event, Member member) {
285286
int qotwCount = Database.getMemberInt(collection, member, "qotwpoints");
286287
int warnCount = Database.getMemberInt(collection, member, "warns");
287288

288-
String joinDiff = " (" + Misc.getDateDiff(Date.from(member.getTimeJoined().toInstant()), Date.from(new Date().toInstant())) + " ago)";
289-
String createDiff = " (" + Misc.getDateDiff(Date.from(member.getUser().getTimeCreated().toInstant()), Date.from(new Date().toInstant())) + " ago)";
290-
289+
String joinDiff = TimeUtils.formatDurationToNow(member.getTimeJoined());
290+
String createDiff = TimeUtils.formatDurationToNow(member.getTimeCreated());
291291

292292
EmbedBuilder eb = new EmbedBuilder()
293293
.setTitle(statusEmote + " " + member.getUser().getAsTag() + " " + botBadge + boostBadge + badges)

src/main/java/com/javadiscord/javabot/commands/user_commands/ServerInfo.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package com.javadiscord.javabot.commands.user_commands;
22

3-
import com.javadiscord.javabot.other.Constants;
4-
import com.javadiscord.javabot.other.Misc;
53
import com.jagrosh.jdautilities.command.Command;
64
import com.jagrosh.jdautilities.command.CommandEvent;
5+
import com.javadiscord.javabot.other.Constants;
6+
import com.javadiscord.javabot.other.TimeUtils;
77
import net.dv8tion.jda.api.EmbedBuilder;
88
import net.dv8tion.jda.api.MessageBuilder;
99
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
1010
import net.dv8tion.jda.api.interactions.ActionRow;
1111
import net.dv8tion.jda.api.interactions.button.Button;
1212

1313
import java.awt.*;
14-
import java.time.format.DateTimeFormatter;
1514
import java.util.Date;
16-
import java.util.Locale;
1715

1816
public class ServerInfo extends Command {
1917

@@ -25,8 +23,8 @@ public static void exCommand (CommandEvent event) {
2523
long voiceChannelCount = event.getGuild().getVoiceChannels().stream().count();
2624
long channelCount = event.getGuild().getChannels().stream().count() - catCount;
2725

28-
String guildDate = event.getGuild().getTimeCreated().format(DateTimeFormatter.ofPattern("EEE',' dd/MM/yyyy',' HH:mm", new Locale("en")));
29-
String createdDiff = " (" + Misc.getDateDiff(Date.from(event.getGuild().getTimeCreated().toInstant()), Date.from(new Date().toInstant())) + " ago)";
26+
String guildDate = event.getGuild().getTimeCreated().format(TimeUtils.STANDARD_FORMATTER);
27+
String createdDiff = TimeUtils.formatDurationToNow(event.getGuild().getTimeCreated());
3028

3129
EmbedBuilder eb = new EmbedBuilder()
3230
.setColor(new Color(0x2F3136))
@@ -60,8 +58,8 @@ public static void exCommand (SlashCommandEvent event) {
6058
long voiceChannelCount = event.getGuild().getVoiceChannels().stream().count();
6159
long channelCount = event.getGuild().getChannels().stream().count() - catCount;
6260

63-
String guildDate = event.getGuild().getTimeCreated().format(DateTimeFormatter.ofPattern("EEE',' dd/MM/yyyy',' HH:mm", new Locale("en")));
64-
String createdDiff = " (" + Misc.getDateDiff(Date.from(event.getGuild().getTimeCreated().toInstant()), Date.from(new Date().toInstant())) + " ago)";
61+
String guildDate = event.getGuild().getTimeCreated().format(TimeUtils.STANDARD_FORMATTER);
62+
String createdDiff = TimeUtils.formatDurationToNow(event.getGuild().getTimeCreated());
6563

6664
EmbedBuilder eb = new EmbedBuilder()
6765
.setColor(new Color(0x2F3136))

src/main/java/com/javadiscord/javabot/events/UserJoin.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.javadiscord.javabot.events;
22

3-
import com.javadiscord.javabot.other.Database;
4-
import com.javadiscord.javabot.other.Misc;
5-
import com.javadiscord.javabot.other.ServerLock;
6-
import com.javadiscord.javabot.other.StatsCategory;
3+
import com.javadiscord.javabot.other.*;
74
import com.jagrosh.jdautilities.command.CommandEvent;
85
import net.dv8tion.jda.api.entities.Guild;
96
import net.dv8tion.jda.api.entities.Member;
@@ -201,7 +198,7 @@ public void onGuildMemberJoin(GuildMemberJoinEvent event) {
201198
user.openPrivateChannel().complete().sendMessage(ServerLock.lockEmbed(event.getGuild())).queue();
202199
event.getMember().kick().complete();
203200

204-
String diff = Misc.getDateDiff(Date.from(user.getTimeCreated().toInstant()), Date.from(new Date().toInstant()));
201+
String diff = TimeUtils.formatDurationToNow(event.getMember().getTimeCreated());
205202
welcomeChannel.sendMessage("**" + event.getMember().getUser().getAsTag() + "**" + " (" + diff + " old) tried to join this server.").queue();
206203

207204
}

src/main/java/com/javadiscord/javabot/other/Misc.java

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
import java.math.BigDecimal;
1212
import java.math.RoundingMode;
1313
import java.net.URL;
14-
import java.util.Date;
1514
import java.util.List;
16-
import java.util.concurrent.TimeUnit;
1715

1816
import static com.javadiscord.javabot.events.Startup.iae;
1917

@@ -27,45 +25,6 @@ public static double round(double value, int places) {
2725
return i.doubleValue();
2826
}
2927

30-
public static String getDateDiff(Date date1, Date date2) {
31-
TimeUnit timeUnit = null;
32-
String ex = " ";
33-
long diffInMillies = date2.getTime() - date1.getTime();
34-
35-
if (TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS) > 365) {
36-
double i = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS) / 365.0;
37-
double yrs = round(i, 1);
38-
ex += "years";
39-
return yrs + ex;
40-
}
41-
42-
if (TimeUnit.HOURS.convert(diffInMillies, TimeUnit.MILLISECONDS) > 48) {
43-
timeUnit = TimeUnit.DAYS;
44-
ex += "days";
45-
return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS) + ex;
46-
}
47-
48-
if (TimeUnit.MINUTES.convert(diffInMillies, TimeUnit.MILLISECONDS) > 120) {
49-
timeUnit = TimeUnit.HOURS;
50-
ex += "hours";
51-
return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS) + ex;
52-
}
53-
54-
if (TimeUnit.SECONDS.convert(diffInMillies, TimeUnit.MILLISECONDS) > 120) {
55-
timeUnit = TimeUnit.MINUTES;
56-
ex += "minutes";
57-
return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS) + ex;
58-
}
59-
60-
if (TimeUnit.MILLISECONDS.convert(diffInMillies, TimeUnit.MILLISECONDS) > 2000) {
61-
timeUnit = TimeUnit.SECONDS;
62-
ex += "seconds";
63-
return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS) + ex;
64-
}
65-
66-
return null;
67-
}
68-
6928
public static int parseInt (String input) {
7029

7130
int i;

src/main/java/com/javadiscord/javabot/other/ServerLock.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
import org.bson.Document;
1414

1515
import java.time.OffsetDateTime;
16-
import java.time.format.DateTimeFormatter;
1716
import java.util.Date;
18-
import java.util.Locale;
1917

2018
import static com.javadiscord.javabot.events.Startup.mongoClient;
2119
import static com.mongodb.client.model.Filters.eq;
@@ -52,8 +50,8 @@ public static void incrementLock(GuildMemberJoinEvent event, User user) {
5250
lockCount = lockCount + 1;
5351
Database.queryConfigInt(event.getGuild().getId(), "lockcount", lockCount);
5452

55-
String timeCreated = user.getTimeCreated().format(DateTimeFormatter.ofPattern("EEE',' dd/MM/yyyy',' HH:mm", new Locale("en")));
56-
String createDiff = " (" + Misc.getDateDiff(Date.from(user.getTimeCreated().toInstant()), Date.from(new Date().toInstant())) + " ago)";
53+
String timeCreated = user.getTimeCreated().format(TimeUtils.STANDARD_FORMATTER);
54+
String createDiff = " (" + TimeUtils.formatDurationToNow(user.getTimeCreated()) + " ago)";
5755

5856
EmbedBuilder eb = new EmbedBuilder()
5957
.setColor(Constants.GRAY)

0 commit comments

Comments
 (0)