Skip to content
Merged
Changes from 8 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8fbb2c2
Added: MAX_AGE_DURATION - This converts seconds to days (7) Being max…
Budbomber Jan 13, 2022
3e718a7
Added: MAX_AGE_DURATION_ONE_WEEK sets the duration the max duration i…
Budbomber Jan 14, 2022
903fcf6
Update application/src/main/java/org/togetherjava/tjbot/commands/basi…
Budbomber Jan 14, 2022
2e97eda
Update application/src/main/java/org/togetherjava/tjbot/commands/basi…
Budbomber Jan 14, 2022
ff8f11c
Added: MAX_AGE_DURATION_ONE_WEEK sets the duration the max duration i…
Budbomber Jan 14, 2022
0756745
Merge remote-tracking branch 'origin/develop' into develop
Budbomber Jan 14, 2022
9ccf518
Cleaned up code, Added MAX_USES_MAX VALUE, reformatted MAX_AGE_MAX_VA…
Budbomber Jan 14, 2022
4d015aa
Cleaned up code, Added MAX_VALUE_MAX_USES, reformatted MAX_VALUE_MAX_…
Budbomber Jan 14, 2022
93b9927
Update application/src/main/java/org/togetherjava/tjbot/commands/basi…
Budbomber Jan 14, 2022
d760bc0
Taken into account, all the suggestions cleaned up code
Budbomber Jan 15, 2022
505da9b
Merge remote-tracking branch 'origin/develop' into develop
Budbomber Jan 15, 2022
2c7567c
Update VcActivityCommand.java
Budbomber Jan 15, 2022
f26b4f2
Update application/src/main/java/org/togetherjava/tjbot/commands/basi…
Budbomber Jan 15, 2022
6f5cbbd
Update VcActivityCommand.java
Budbomber Jan 15, 2022
825ec48
Update application/src/main/java/org/togetherjava/tjbot/commands/basi…
Budbomber Jan 15, 2022
5104e73
Update VcActivityCommand.java
Budbomber Jan 15, 2022
2ae2a87
Taken into account, all the suggestions cleaned up code
Budbomber Jan 15, 2022
b57c07b
Merge remote-tracking branch 'origin/develop' into develop
Budbomber Jan 15, 2022
f7d0640
Update application/src/main/java/org/togetherjava/tjbot/commands/basi…
Budbomber Jan 15, 2022
a49095c
Update application/src/main/java/org/togetherjava/tjbot/commands/basi…
Budbomber Jan 15, 2022
4ddbd6c
Update application/src/main/java/org/togetherjava/tjbot/commands/basi…
Budbomber Jan 15, 2022
29d217f
Update application/src/main/java/org/togetherjava/tjbot/commands/basi…
Budbomber Jan 15, 2022
a4ccedb
Update application/src/main/java/org/togetherjava/tjbot/commands/basi…
Budbomber Jan 15, 2022
aeb8e79
Update application/src/main/java/org/togetherjava/tjbot/commands/basi…
Budbomber Jan 15, 2022
97ed98a
Made changes Zabu requested, typos, Moved declarations, updated doc, …
Budbomber Jan 17, 2022
0aca975
Final changes made, renamed handle method to RequiredOptionIfPresent,…
Budbomber Jan 17, 2022
c11b8ea
Changes made, remaned maxAge to maxAgeDays, added check in voicechane…
Budbomber Jan 17, 2022
19062bc
Fixed typo's changed text in options to reflect the values set in the…
Budbomber Jan 17, 2022
0a98196
Fixed value that was forgotten, input is now in days for UX, and the …
Budbomber Jan 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
Expand All @@ -24,6 +23,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
* Implements the {@code vc-activity} command. Creates VC activities.
Expand All @@ -45,6 +45,10 @@ public final class VcActivityCommand extends SlashCommandAdapter {
private static final String MAX_USES_OPTION = "max-uses";
private static final String MAX_AGE_OPTION = "max-age";

// sets the Max_age duration of the voice channel in seconds - converts to days.
private static final long MAX_VALUE_MAX_AGE = TimeUnit.SECONDS.toDays(7);
private static final long MAX_VALE_MAX_USES = 100;

public static final String YOUTUBE_TOGETHER_NAME = "YouTube Together";
public static final String POKER_NAME = "Poker";
public static final String BETRAYAL_IO_NAME = "Betrayal.io";
Expand Down Expand Up @@ -83,11 +87,12 @@ public final class VcActivityCommand extends SlashCommandAdapter {
"879863976006127627", LETTERTILE_NAME, "879863686565621790");

private static final List<OptionData> inviteOptions = List.of(
new OptionData(OptionType.STRING, MAX_USES_OPTION,
"The amount of times the invite can be used, default is infinity", false),
new OptionData(OptionType.INTEGER, MAX_USES_OPTION,
"The amount of times the invite can be used, default is infinity", false)
.setRequiredRange(0, MAX_VALE_MAX_USES),
new OptionData(OptionType.INTEGER, MAX_AGE_OPTION,
"Max age in seconds. Set this to 0 to never expire, default is 1 day", false));

"Max age in seconds. Set this to 0 to never expire, default is 1 day", false)
.setRequiredRange(1, MAX_VALUE_MAX_AGE));

/**
* Constructs an instance
Expand Down Expand Up @@ -145,29 +150,6 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) {
OptionMapping applicationOption = event.getOption(APPLICATION_OPTION);
OptionMapping idOption = event.getOption(ID_OPTION);

OptionMapping maxUsesOption = event.getOption(MAX_USES_OPTION);
OptionMapping maxAgeOption = event.getOption(MAX_AGE_OPTION);

Integer maxUses;

// the user already received the error in the handleIntegerTypeOption method
// it still throws to tell us to return this method and stop the proceeding code
try {
maxUses = handleIntegerTypeOption(event, maxUsesOption);
} catch (IllegalArgumentException ignore) {
return;
}

Integer maxAge;

// the user already received the error in the handleIntegerTypeOption method
// it still throws to tell us to return this method and stop the proceeding code
try {
maxAge = handleIntegerTypeOption(event, maxAgeOption);
} catch (IllegalArgumentException ignore) {
return;
}


String applicationId;
String applicationName;
Expand All @@ -182,7 +164,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) {
getKeyByValue(VC_APPLICATION_TO_ID, applicationId).orElse("an activity");
}

handleSubcommand(event, voiceChannel, applicationId, maxUses, maxAge, applicationName);
handleSubcommand(event, voiceChannel, applicationId, applicationName);
}

private static <K, V> @NotNull Optional<K> getKeyByValue(@NotNull Map<K, V> map,
Expand All @@ -197,13 +179,11 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) {
}

private static void handleSubcommand(@NotNull SlashCommandEvent event,
@NotNull VoiceChannel voiceChannel, @NotNull String applicationId,
@Nullable Integer maxUses, @Nullable Integer maxAge, @NotNull String applicationName) {
@NotNull VoiceChannel voiceChannel, @NotNull String applicationId,
@NotNull String applicationName) {

voiceChannel.createInvite()
.setTargetApplication(applicationId)
.setMaxUses(maxUses)
.setMaxAge(maxAge)
.flatMap(invite -> replyInvite(event, invite, applicationName))
.queue(null, throwable -> handleErrors(event, throwable));
}
Expand All @@ -222,52 +202,4 @@ private static void handleErrors(@NotNull SlashCommandEvent event,
event.reply("Something went wrong :/").queue();
logger.warn("Something went wrong in the VcActivityCommand", throwable);
}


/**
* This grabs the OptionMapping, after this it <br />
* - validates whenever it's within an {@link Integer Integer's} range <br />
* - validates whenever it's positive <br />
*
* <p>
* <p/>
*
* @param event the {@link SlashCommandEvent}
* @param optionMapping the {@link OptionMapping}
* @return nullable {@link Integer}
* @throws java.lang.IllegalArgumentException if the option's value is - outside of
* {@link Integer#MAX_VALUE} - negative
*/
@Contract("_, null -> null")
private static @Nullable Integer handleIntegerTypeOption(@NotNull SlashCommandEvent event,
@Nullable OptionMapping optionMapping) {

int optionValue;

if (optionMapping == null) {
return null;
}

try {
optionValue = Math.toIntExact(optionMapping.getAsLong());
} catch (ArithmeticException e) {
event
.reply("The " + optionMapping.getName() + " is above `" + Integer.MAX_VALUE
+ "`, which is too high")
.setEphemeral(true)
.queue();
throw new IllegalArgumentException(
optionMapping.getName() + " can't be above " + Integer.MAX_VALUE);
}

if (optionValue < 0) {
event.reply("The " + optionMapping.getName() + " is negative, which isn't supported")
.setEphemeral(true)
.queue();
throw new IllegalArgumentException(optionMapping.getName() + " can't be negative");
}


return optionValue;
}
}