Compare commits

...

3 commits

Author SHA1 Message Date
3a76e62ba9
silly regex 2024-11-19 01:20:38 +00:00
28ca47394b
Correctly name timeout command 2024-11-19 01:20:12 +00:00
02fdfdd532
Poll command!!!! 2024-11-19 01:19:54 +00:00
4 changed files with 59 additions and 3 deletions

View file

@ -1,4 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="EntryPointsManager">
<list size="1">
<item index="0" class="java.lang.String" itemvalue="com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand" />
</list>
</component>
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager"> <component name="MavenProjectsManager">
<option name="originalFiles"> <option name="originalFiles">

View file

@ -23,9 +23,9 @@ public class Timeout extends ApplicationCommand {
subcommand = "set", subcommand = "set",
description = "Times out a member" description = "Times out a member"
) )
public void mute(GuildSlashEvent event, public void timeout(GuildSlashEvent event,
@AppOption(name = "member") Member member, @AppOption(name = "member") Member member,
@AppOption(name = "duration", description = "ex: 2h5m, must be between 1h and 7d") String duration) { @AppOption(name = "duration", description = "ex: 2h5m, must be between 1m and 7d") String duration) {
Duration timeoutDuration = DurationUtils.parseDuration(duration); Duration timeoutDuration = DurationUtils.parseDuration(duration);
member.timeoutFor(timeoutDuration).queue(); member.timeoutFor(timeoutDuration).queue();
event.replyFormat("Timed out %s for %s", member.getAsMention(), duration).queue(); event.replyFormat("Timed out %s for %s", member.getAsMention(), duration).queue();

View file

@ -0,0 +1,48 @@
package net.hypr.doki.commands.utils;
import com.freya02.botcommands.api.annotations.BotPermissions;
import com.freya02.botcommands.api.annotations.CommandMarker;
import com.freya02.botcommands.api.annotations.Optional;
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.utils.messages.MessagePollBuilder;
import net.dv8tion.jda.api.utils.messages.MessagePollData;
import net.hypr.doki.utils.DurationUtils;
import java.time.Duration;
import java.util.Objects;
@CommandMarker
@BotPermissions(Permission.MESSAGE_SEND_POLLS)
@UserPermissions(Permission.MESSAGE_SEND_POLLS)
public class Poll extends ApplicationCommand {
@JDASlashCommand(
name = "poll",
description = "Creates a poll"
)
public void poll(GuildSlashEvent event,
@AppOption(name = "title") String pollTitle,
@AppOption(name = "duration", description = "ex: 2h5m, must be between 1h and 7d") String duration,
@AppOption(name = "options", description = "Comma-seperated poll options") String options,
@Optional @AppOption(name = "multiple-choice", description = "Allow multiple choices? (defaults to false)") Boolean multiChoice) {
Boolean pollMultiChoice = Objects.requireNonNullElse(multiChoice, false);
Duration pollDuration = DurationUtils.parseDuration(duration);
String[] pollOptions = options.split(",");
MessagePollBuilder poll = MessagePollData.builder(pollTitle)
.setDuration(pollDuration);
for (String option: pollOptions) {
poll.addAnswer(option);
}
event.getChannel().sendMessagePoll(poll.build()).queue();
event.replyFormat("Created %s poll \"%s\" in %s which will last %s",
(pollMultiChoice) ? "multi-choice" : "single-choice",
pollTitle,
event.getChannel().getAsMention(),
duration).setEphemeral(true).queue();
}
}

View file

@ -7,7 +7,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class DurationUtils { public class DurationUtils {
private static final Pattern timePattern = Pattern.compile("(\\d+)(?:([dhms]))?"); private static final Pattern timePattern = Pattern.compile("(\\d+)([dhms])?");
public static Duration parseDuration(String input) throws IllegalArgumentException { public static Duration parseDuration(String input) throws IllegalArgumentException {
Matcher matcher = timePattern.matcher(input); Matcher matcher = timePattern.matcher(input);
@ -28,6 +28,8 @@ public class DurationUtils {
} }
public static String getTimeDifference(OffsetDateTime offsetDateTime) { public static String getTimeDifference(OffsetDateTime offsetDateTime) {
if (offsetDateTime == null) return "0 seconds";
// Get the current Instant (current time) // Get the current Instant (current time)
Instant now = Instant.now(); Instant now = Instant.now();