Migrate to slash commands

This commit is contained in:
floppydiskette 2024-11-17 18:49:12 +00:00
parent 64d059dbf9
commit 33457e8270
Signed by: fwoppydwisk
SSH key fingerprint: SHA256:Hqn452XQ1ETzUt/FthJu6+OFkS4NBxCv5VQSEvuk7CE
5 changed files with 75 additions and 125 deletions

View file

@ -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();
}
}

View file

@ -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();
}
}

View file

@ -1,21 +1,29 @@
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.CommandScope;
import com.freya02.botcommands.api.application.slash.GlobalSlashEvent;
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());
} }
} }

View file

@ -1,45 +1,40 @@
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.CommandScope;
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())

View file

@ -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();
}
} }