Skip to content

Commit

Permalink
Added forceUnslot by slot number slash command
Browse files Browse the repository at this point in the history
  • Loading branch information
Alf-Melmac committed Nov 2, 2021
1 parent e47ee39 commit 756ea86
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
Expand All @@ -20,6 +22,7 @@
import static de.webalf.slotbot.util.bot.MentionUtils.getId;
import static de.webalf.slotbot.util.bot.MentionUtils.isUserMention;
import static de.webalf.slotbot.util.bot.MessageUtils.*;
import static de.webalf.slotbot.util.bot.SlashCommandUtils.getOptionalIntegerOption;
import static de.webalf.slotbot.util.permissions.BotPermissionHelper.Authorization.EVENT_MANAGE;
import static de.webalf.slotbot.util.permissions.BotPermissionHelper.Authorization.NONE;
import static de.webalf.slotbot.util.permissions.BotPermissionHelper.isAuthorized;
Expand All @@ -38,6 +41,10 @@
@SlashCommand(name = "unslot",
description = "Slottet dich aus einem Event aus.",
authorization = NONE)
@SlashCommand(name = "forceUnslot",
description = "Slottet jemanden anderen aus einem Event aus.",
authorization = EVENT_MANAGE,
optionPosition = 0)
public class Unslot implements DiscordCommand, DiscordSlashCommand {
private final EventBotService eventBotService;

Expand Down Expand Up @@ -87,12 +94,27 @@ private void selfUnslot(@NonNull Message message) {
deleteMessagesInstant(message);
}

private static final String OPTION_SLOT_NUMBER = "slotnummer";
private static final List<List<OptionData>> OPTIONS = List.of(
List.of(new OptionData(OptionType.INTEGER, OPTION_SLOT_NUMBER, "Nummer des zu leerenden Slots.", true))
);

@Override
public void execute(SlashCommandEvent event) {
log.trace("Slash command: unslot");

eventBotService.unslot(event.getChannel().getIdLong(), event.getUser().getId());
final Integer slotNumber = getOptionalIntegerOption(event.getOption(OPTION_SLOT_NUMBER));
if (slotNumber == null) { //Self unslot
eventBotService.unslot(event.getChannel().getIdLong(), event.getUser().getId());
} else { //Unslot others
eventBotService.unslot(event.getChannel().getIdLong(), slotNumber);
}

finishedSlashCommandAction(event);
}

@Override
public List<OptionData> getOptions(int optionPosition) {
return OPTIONS.get(optionPosition);
}
}
10 changes: 10 additions & 0 deletions src/main/java/de/webalf/slotbot/util/bot/SlashCommandUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ public static int getIntegerOption(@NonNull OptionMapping option) {
return Math.toIntExact(option.getAsLong());
}

/**
* Returns the integer value of the given nullable {@link OptionMapping}
*
* @param option to get int from
* @return int or null
*/
public static Integer getOptionalIntegerOption(OptionMapping option) {
return option == null ? null : getIntegerOption(option);
}

/**
* Returns the id of the {@link User} of the given not null {@link OptionMapping}
*
Expand Down

0 comments on commit 756ea86

Please sign in to comment.