Compare commits
	
		
			3 commits
		
	
	
		
			
				64d059dbf9
			
			...
			
				287343b5d3
			
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
							 | 
						
							
							
								
							
							
	
	
	287343b5d3 | 
						
						
							||
| 
							 | 
						
							
							
								
							
							
	
	
	103f6ce538 | 
						
						
							||
| 
							 | 
						
							
							
								
							
							
	
	
	33457e8270 | 
						
						
							
					 7 changed files with 118 additions and 126 deletions
				
			
		| 
						 | 
					@ -49,7 +49,7 @@ public class Doki {
 | 
				
			||||||
            start();
 | 
					            start();
 | 
				
			||||||
            jda = getJDA();
 | 
					            jda = getJDA();
 | 
				
			||||||
            CommandsBuilder.newBuilder(437970062922612737L)
 | 
					            CommandsBuilder.newBuilder(437970062922612737L)
 | 
				
			||||||
                    .textCommandBuilder(textCommandsBuilder -> textCommandsBuilder.addPrefix(getPrefix()))
 | 
					                    .textCommandBuilder(textCommandsBuilder -> textCommandsBuilder.disableHelpCommand(true))
 | 
				
			||||||
                    .build(jda, "net.hypr.doki.commands"); //Registering listeners is taken care of by the lib
 | 
					                    .build(jda, "net.hypr.doki.commands"); //Registering listeners is taken care of by the lib
 | 
				
			||||||
        } catch (Exception e) {
 | 
					        } catch (Exception e) {
 | 
				
			||||||
            log.error("Failed to start the bot", e);
 | 
					            log.error("Failed to start the bot", e);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,48 +0,0 @@
 | 
				
			||||||
package net.hypr.doki.commands.moderation;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import com.freya02.botcommands.api.annotations.CommandMarker;
 | 
					 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.BaseCommandEvent;
 | 
					 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.CommandEvent;
 | 
					 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.TextCommand;
 | 
					 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.annotations.Category;
 | 
					 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.annotations.Description;
 | 
					 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.annotations.JDATextCommand;
 | 
					 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.annotations.TextOption;
 | 
					 | 
				
			||||||
import net.dv8tion.jda.api.Permission;
 | 
					 | 
				
			||||||
import net.dv8tion.jda.api.entities.Member;
 | 
					 | 
				
			||||||
import net.dv8tion.jda.api.entities.Message;
 | 
					 | 
				
			||||||
import net.dv8tion.jda.api.interactions.commands.Command;
 | 
					 | 
				
			||||||
import net.hypr.doki.utils.DurationUtils;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import java.time.Duration;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
@CommandMarker
 | 
					 | 
				
			||||||
@Category("Moderation")
 | 
					 | 
				
			||||||
@Description("Mutes a user for a specified amount of time")
 | 
					 | 
				
			||||||
public class Mute extends TextCommand {
 | 
					 | 
				
			||||||
    @JDATextCommand(name = "mute", order = 1)
 | 
					 | 
				
			||||||
    public void execute(BaseCommandEvent event, @TextOption(example = "<@437970062922612737>") Member member, @TextOption(example = "30m") String duration) {
 | 
					 | 
				
			||||||
        Member commandExecutor = event.getMember();
 | 
					 | 
				
			||||||
        if (!commandExecutor.hasPermission(Permission.KICK_MEMBERS)) {
 | 
					 | 
				
			||||||
            return;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        Duration timeoutDuration;
 | 
					 | 
				
			||||||
        try {
 | 
					 | 
				
			||||||
             timeoutDuration = DurationUtils.parseDuration(duration);
 | 
					 | 
				
			||||||
        } catch (IllegalArgumentException ex) {
 | 
					 | 
				
			||||||
            event.reply("Invalid duration format!").queue();
 | 
					 | 
				
			||||||
            return;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        if (Math.abs(timeoutDuration.toDays()) > 28) {
 | 
					 | 
				
			||||||
            event.reply("Duration must be less than 28 days!").queue();
 | 
					 | 
				
			||||||
            return;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        member.timeoutFor(timeoutDuration).queue();
 | 
					 | 
				
			||||||
        event.reply("Muted " + member.getAsMention() + " (" + member.getEffectiveName() + ") for " + duration).queue();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @JDATextCommand(name = "mute", order = 2)
 | 
					 | 
				
			||||||
    public void execute(CommandEvent event) {
 | 
					 | 
				
			||||||
        event.reply("You must specify a user and duration!").queue();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										45
									
								
								src/main/java/net/hypr/doki/commands/moderation/Timeout.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								src/main/java/net/hypr/doki/commands/moderation/Timeout.java
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,45 @@
 | 
				
			||||||
 | 
					package net.hypr.doki.commands.moderation;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.freya02.botcommands.api.annotations.BotPermissions;
 | 
				
			||||||
 | 
					import com.freya02.botcommands.api.annotations.CommandMarker;
 | 
				
			||||||
 | 
					import com.freya02.botcommands.api.annotations.UserPermissions;
 | 
				
			||||||
 | 
					import com.freya02.botcommands.api.application.ApplicationCommand;
 | 
				
			||||||
 | 
					import com.freya02.botcommands.api.application.annotations.AppOption;
 | 
				
			||||||
 | 
					import com.freya02.botcommands.api.application.slash.GuildSlashEvent;
 | 
				
			||||||
 | 
					import com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand;
 | 
				
			||||||
 | 
					import net.dv8tion.jda.api.Permission;
 | 
				
			||||||
 | 
					import net.dv8tion.jda.api.entities.Member;
 | 
				
			||||||
 | 
					import net.hypr.doki.utils.DurationUtils;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.time.Duration;
 | 
				
			||||||
 | 
					import java.time.OffsetDateTime;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@CommandMarker
 | 
				
			||||||
 | 
					@BotPermissions(Permission.MODERATE_MEMBERS)
 | 
				
			||||||
 | 
					@UserPermissions(Permission.MODERATE_MEMBERS)
 | 
				
			||||||
 | 
					public class Timeout extends ApplicationCommand {
 | 
				
			||||||
 | 
					    @JDASlashCommand(
 | 
				
			||||||
 | 
					            name = "timeout",
 | 
				
			||||||
 | 
					            subcommand = "set",
 | 
				
			||||||
 | 
					            description = "Times out a member"
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    public void mute(GuildSlashEvent event,
 | 
				
			||||||
 | 
					                     @AppOption(name = "member") Member member,
 | 
				
			||||||
 | 
					                     @AppOption(name = "duration", description = "ex: 2h5m, must be between 1h and 7d") String duration) {
 | 
				
			||||||
 | 
					        Duration timeoutDuration = DurationUtils.parseDuration(duration);
 | 
				
			||||||
 | 
					        member.timeoutFor(timeoutDuration).queue();
 | 
				
			||||||
 | 
					        event.replyFormat("Timed out %s for %s", member.getAsMention(), duration).queue();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @JDASlashCommand(
 | 
				
			||||||
 | 
					            name = "timeout",
 | 
				
			||||||
 | 
					            subcommand = "cancel",
 | 
				
			||||||
 | 
					            description = "Cancels the specified users timeout"
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    public void cancelMute(GuildSlashEvent event,
 | 
				
			||||||
 | 
					                           @AppOption(name = "member") Member member) {
 | 
				
			||||||
 | 
					        OffsetDateTime timeoutEnd = member.getTimeOutEnd();
 | 
				
			||||||
 | 
					        member.removeTimeout().queue();
 | 
				
			||||||
 | 
					        event.replyFormat("Removed %s's timeout (%s remaining)", member.getAsMention(), DurationUtils.getTimeDifference(timeoutEnd)).queue();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -1,45 +0,0 @@
 | 
				
			||||||
package net.hypr.doki.commands.utils;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import com.freya02.botcommands.api.annotations.CommandMarker;
 | 
					 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.CommandEvent;
 | 
					 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.TextCommand;
 | 
					 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.annotations.Category;
 | 
					 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.annotations.Description;
 | 
					 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.annotations.JDATextCommand;
 | 
					 | 
				
			||||||
import net.dv8tion.jda.api.EmbedBuilder;
 | 
					 | 
				
			||||||
import net.dv8tion.jda.api.JDA;
 | 
					 | 
				
			||||||
import net.dv8tion.jda.api.entities.SelfUser;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import java.awt.*;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
@CommandMarker
 | 
					 | 
				
			||||||
@Category("Utils")
 | 
					 | 
				
			||||||
@Description("Get about info")
 | 
					 | 
				
			||||||
public class About extends TextCommand {
 | 
					 | 
				
			||||||
    @JDATextCommand(name = "about", aliases = {"doki"})
 | 
					 | 
				
			||||||
    public void execute(CommandEvent event) {
 | 
					 | 
				
			||||||
        JDA jda = event.getJDA();
 | 
					 | 
				
			||||||
        SelfUser selfUser = jda.getSelfUser();
 | 
					 | 
				
			||||||
        String jdaVersion = "";
 | 
					 | 
				
			||||||
        try {
 | 
					 | 
				
			||||||
            Class<?> jdaClass = Class.forName("net.dv8tion.jda.api.JDA");
 | 
					 | 
				
			||||||
            Package jdaPackage = jdaClass.getPackage();
 | 
					 | 
				
			||||||
            if (jdaPackage != null) {
 | 
					 | 
				
			||||||
                // Attempt to get Implementation-Version from the manifest
 | 
					 | 
				
			||||||
                String version = jdaPackage.getImplementationVersion();
 | 
					 | 
				
			||||||
                jdaVersion = version != null ? version : "Unknown";
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        } catch (Exception ex) {
 | 
					 | 
				
			||||||
            jdaVersion = "UNKNOWN";
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        EmbedBuilder aboutEmbed = new EmbedBuilder()
 | 
					 | 
				
			||||||
                .setTitle("About " + selfUser.getName())
 | 
					 | 
				
			||||||
                .setThumbnail(selfUser.getAvatarUrl())
 | 
					 | 
				
			||||||
                .setDescription("Banned from every deli nationwide")
 | 
					 | 
				
			||||||
                .addField("Guilds", String.valueOf(jda.getGuildCache().stream().count()), true)
 | 
					 | 
				
			||||||
                .addField("JDA Version", jdaVersion, true)
 | 
					 | 
				
			||||||
                .addField("Owner", "fwoppydwisk", true)
 | 
					 | 
				
			||||||
                .setColor(new Color(210,138,39));
 | 
					 | 
				
			||||||
            event.reply(aboutEmbed.build()).queue();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1,21 +1,27 @@
 | 
				
			||||||
package net.hypr.doki.commands.utils;
 | 
					package net.hypr.doki.commands.utils;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.freya02.botcommands.api.annotations.CommandMarker;
 | 
					import com.freya02.botcommands.api.annotations.CommandMarker;
 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.CommandEvent;
 | 
					import com.freya02.botcommands.api.application.ApplicationCommand;
 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.TextCommand;
 | 
					import com.freya02.botcommands.api.application.slash.GuildSlashEvent;
 | 
				
			||||||
 | 
					import com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand;
 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.annotations.Category;
 | 
					import com.freya02.botcommands.api.prefixed.annotations.Category;
 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.annotations.Description;
 | 
					import com.freya02.botcommands.api.prefixed.annotations.Description;
 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.annotations.JDATextCommand;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
@CommandMarker
 | 
					@CommandMarker
 | 
				
			||||||
@Category("Utils")
 | 
					@Category("Utils")
 | 
				
			||||||
@Description("Pong!")
 | 
					@Description("Pong!")
 | 
				
			||||||
public class Ping extends TextCommand {
 | 
					public class Ping extends ApplicationCommand {
 | 
				
			||||||
    @JDATextCommand(name = "ping")
 | 
					    @JDASlashCommand(
 | 
				
			||||||
    public void execute(CommandEvent event) {
 | 
					            name = "ping",
 | 
				
			||||||
        final long gatewayPing = event.getJDA().getGatewayPing();
 | 
					            description = "Pong!"
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    public void onPing(GuildSlashEvent event) {
 | 
				
			||||||
 | 
					        event.deferReply().queue();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        final long gatewayPing = event.getJDA().getGatewayPing();
 | 
				
			||||||
        event.getJDA().getRestPing()
 | 
					        event.getJDA().getRestPing()
 | 
				
			||||||
                .queue(restPing -> event.respondFormat("Gateway ping: **%d ms**\nRest ping: **%d ms**", gatewayPing, restPing).queue());
 | 
					                .queue(l -> event.getHook()
 | 
				
			||||||
 | 
					                        .sendMessageFormat("Gateway ping: **%d ms**\nRest ping: **%d ms**", gatewayPing, l)
 | 
				
			||||||
 | 
					                        .queue());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,45 +1,39 @@
 | 
				
			||||||
package net.hypr.doki.commands.utils;
 | 
					package net.hypr.doki.commands.utils;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.freya02.botcommands.api.annotations.CommandMarker;
 | 
					import com.freya02.botcommands.api.annotations.CommandMarker;
 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.BaseCommandEvent;
 | 
					import com.freya02.botcommands.api.annotations.Optional;
 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.CommandEvent;
 | 
					import com.freya02.botcommands.api.application.ApplicationCommand;
 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.TextCommand;
 | 
					import com.freya02.botcommands.api.application.annotations.AppOption;
 | 
				
			||||||
 | 
					import com.freya02.botcommands.api.application.slash.GuildSlashEvent;
 | 
				
			||||||
 | 
					import com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand;
 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.annotations.Category;
 | 
					import com.freya02.botcommands.api.prefixed.annotations.Category;
 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.annotations.Description;
 | 
					import com.freya02.botcommands.api.prefixed.annotations.Description;
 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.annotations.JDATextCommand;
 | 
					 | 
				
			||||||
import com.freya02.botcommands.api.prefixed.annotations.TextOption;
 | 
					 | 
				
			||||||
import net.dv8tion.jda.api.EmbedBuilder;
 | 
					import net.dv8tion.jda.api.EmbedBuilder;
 | 
				
			||||||
import net.dv8tion.jda.api.entities.Member;
 | 
					import net.dv8tion.jda.api.entities.Member;
 | 
				
			||||||
import net.dv8tion.jda.api.entities.Role;
 | 
					import net.dv8tion.jda.api.entities.Role;
 | 
				
			||||||
import net.dv8tion.jda.api.entities.User;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.time.Duration;
 | 
					import java.time.Duration;
 | 
				
			||||||
import java.time.OffsetDateTime;
 | 
					import java.time.OffsetDateTime;
 | 
				
			||||||
 | 
					import java.util.Objects;
 | 
				
			||||||
import java.util.stream.Collectors;
 | 
					import java.util.stream.Collectors;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@CommandMarker
 | 
					@CommandMarker
 | 
				
			||||||
@Category("Utils")
 | 
					@Category("Utils")
 | 
				
			||||||
@Description("Get information about a user")
 | 
					@Description("Get information about a user")
 | 
				
			||||||
public class WhoIs extends TextCommand {
 | 
					public class WhoIs extends ApplicationCommand {
 | 
				
			||||||
    @JDATextCommand(name = "whois", order = 1)
 | 
					    @JDASlashCommand(
 | 
				
			||||||
    public void execute(BaseCommandEvent event, @TextOption(example = "<@437970062922612737>") Member user) {
 | 
					            name = "whois",
 | 
				
			||||||
        EmbedBuilder whoIsEmbed = buildUserEmbed(user, (CommandEvent) event);
 | 
					            description = "Gets information on a user"
 | 
				
			||||||
        event.reply(whoIsEmbed.build()).queue();
 | 
					    )
 | 
				
			||||||
 | 
					    public void whois(GuildSlashEvent event,
 | 
				
			||||||
 | 
					                      @Optional @AppOption(name = "member") Member memberParam) {
 | 
				
			||||||
 | 
					        Member member;
 | 
				
			||||||
 | 
					        member = Objects.requireNonNullElseGet(memberParam, event::getMember);
 | 
				
			||||||
 | 
					        EmbedBuilder whoIsEmbed = buildUserEmbed(member, event);
 | 
				
			||||||
 | 
					        event.replyEmbeds(whoIsEmbed.build()).queue();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @JDATextCommand(name = "whois", order = 2)
 | 
					    private EmbedBuilder buildUserEmbed(Member user, GuildSlashEvent event) {
 | 
				
			||||||
    public void execute(BaseCommandEvent event, @TextOption(example = "<@437970062922612737>") User user) {
 | 
					 | 
				
			||||||
        EmbedBuilder whoIsEmbed = buildUserEmbed((Member) user, (CommandEvent) event);
 | 
					 | 
				
			||||||
        event.reply(whoIsEmbed.build()).queue();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @JDATextCommand(name = "whois", order = 3)
 | 
					 | 
				
			||||||
    public void execute(CommandEvent event) {
 | 
					 | 
				
			||||||
        EmbedBuilder whoIsEmbed = buildUserEmbed(event.getMember(), event);
 | 
					 | 
				
			||||||
        event.reply(whoIsEmbed.build()).queue();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private EmbedBuilder buildUserEmbed(Member user, CommandEvent event) {
 | 
					 | 
				
			||||||
         return new EmbedBuilder()
 | 
					         return new EmbedBuilder()
 | 
				
			||||||
                 .setAuthor(user.getUser().getName(), null, user.getEffectiveAvatarUrl())
 | 
					                 .setAuthor(user.getUser().getName(), null, user.getEffectiveAvatarUrl())
 | 
				
			||||||
                 .setThumbnail(user.getEffectiveAvatarUrl())
 | 
					                 .setThumbnail(user.getEffectiveAvatarUrl())
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,8 @@
 | 
				
			||||||
package net.hypr.doki.utils;
 | 
					package net.hypr.doki.utils;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.time.Duration;
 | 
					import java.time.Duration;
 | 
				
			||||||
 | 
					import java.time.Instant;
 | 
				
			||||||
 | 
					import java.time.OffsetDateTime;
 | 
				
			||||||
import java.util.regex.Matcher;
 | 
					import java.util.regex.Matcher;
 | 
				
			||||||
import java.util.regex.Pattern;
 | 
					import java.util.regex.Pattern;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -24,4 +26,42 @@ public class DurationUtils {
 | 
				
			||||||
            default -> throw new IllegalArgumentException("Invalid unit");
 | 
					            default -> throw new IllegalArgumentException("Invalid unit");
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public static String getTimeDifference(OffsetDateTime offsetDateTime) {
 | 
				
			||||||
 | 
					        // Get the current Instant (current time)
 | 
				
			||||||
 | 
					        Instant now = Instant.now();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Calculate the duration between the current time and the given OffsetDateTime
 | 
				
			||||||
 | 
					        Duration duration = Duration.between(now, offsetDateTime.toInstant());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Get the difference in various units
 | 
				
			||||||
 | 
					        long days = duration.toDays();
 | 
				
			||||||
 | 
					        long hours = duration.toHours() % 24;  // Modulo 24 to get hours within a single day
 | 
				
			||||||
 | 
					        long minutes = duration.toMinutes() % 60;  // Modulo 60 to get minutes within a single hour
 | 
				
			||||||
 | 
					        long seconds = duration.getSeconds() % 60;  // Modulo 60 to get remaining seconds
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Build the formatted time difference string
 | 
				
			||||||
 | 
					        StringBuilder result = new StringBuilder();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Append each time unit if it is non-zero
 | 
				
			||||||
 | 
					        if (days > 0) {
 | 
				
			||||||
 | 
					            result.append(days).append(" day").append(days > 1 ? "s" : "").append(" ");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if (hours > 0) {
 | 
				
			||||||
 | 
					            result.append(hours).append(" hour").append(hours > 1 ? "s" : "").append(" ");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if (minutes > 0) {
 | 
				
			||||||
 | 
					            result.append(minutes).append(" minute").append(minutes > 1 ? "s" : "").append(" ");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if (seconds > 0) {
 | 
				
			||||||
 | 
					            result.append(seconds).append(" second").append(seconds > 1 ? "s" : "");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // If the duration is zero, return "0 seconds"
 | 
				
			||||||
 | 
					        if (result.isEmpty()) {
 | 
				
			||||||
 | 
					            result.append("0 second");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return result.toString().trim();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue