Skip to content

Commit

Permalink
Creating of Giveaway entity on command
Browse files Browse the repository at this point in the history
  • Loading branch information
DxsSucuk committed Sep 26, 2023
1 parent e85365f commit cd0f3a2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
import de.presti.ree6.commands.interfaces.ICommand;
import de.presti.ree6.language.LanguageService;
import de.presti.ree6.main.Main;
import de.presti.ree6.utils.data.RegExUtil;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import net.dv8tion.jda.internal.interactions.CommandDataImpl;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* A Command to manage Giveaways.
*/
Expand All @@ -33,9 +41,42 @@ public void onPerform(CommandEvent commandEvent) {
String prize = commandEvent.getOption("prize").getAsString();
String duration = commandEvent.getOption("duration").getAsString();

long days = 0, hours = 0, minutes = 0;

Matcher matcher = Pattern.compile(RegExUtil.TIME_INPUT_REGEX).matcher(duration);

Set<Character> letters = new HashSet<>();

while (matcher.find()) {
String match = matcher.group();
char letter = match.charAt(match.length() - 1);
if (!letters.contains(letter)) {
long value = Long.parseLong(match.substring(0, match.length() - 1));

if (letter == 'd') {
days = value;
} else if (letter == 'h') {
hours = value;
} else {
minutes = value;
}

letters.add(letter);
}
}

Instant endInstant = Instant.now();
endInstant = endInstant.plusSeconds(minutes * 60);
endInstant = endInstant.plusSeconds(hours * 60 * 60);
endInstant = endInstant.plusSeconds(days * 24 * 60 * 60);

Timestamp endTime = Timestamp.from(endInstant);

// TODO:: create Message

de.presti.ree6.sql.entities.Giveaway giveaway = new de.presti.ree6.sql.entities.Giveaway();
de.presti.ree6.sql.entities.Giveaway giveaway =
new de.presti.ree6.sql.entities.Giveaway(0, commandEvent.getMember().getIdLong(),
commandEvent.getGuild().getIdLong(), prize, winners, endTime);
// TODO:: create.
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/de/presti/ree6/utils/data/RegExUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,19 @@ public class RegExUtil {
*/
public static final String YOUTUBE_REGEX = "^(?:(?:https?:\\/\\/)?(?:www\\.)?youtu\\.be\\/|^(?:(?:(?:https?:\\/\\/)?(?:www\\.)?youtube\\.com\\/watch\\?.*v=)|(?:https?:\\/\\/)?(?:www\\.)?youtube\\.com\\/v\\/))([a-zA-Z0-9_-]{11})";

/**
* The Regex to check if a String only contains numbers.
*/
public static final String NUMBER_ONLY_REGEX = "^[0-9]+$";

/**
* The Regex to detect any number.
*/
public static final String NUMBER_REGEX = "[0-9]+";

/**
* The Regex to detect any dhm based time input.
* For example 1d, 2h, 3m
*/
public static final String TIME_INPUT_REGEX = "\\d+[dhm]";
}

0 comments on commit cd0f3a2

Please sign in to comment.