forked from SteamWar/SteamWar
Merge branch 'main' into 1.21.6/fightsystem
This commit is contained in:
@@ -41,7 +41,6 @@ import net.dv8tion.jda.api.entities.Role;
|
||||
import net.dv8tion.jda.api.entities.emoji.Emoji;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
|
||||
import net.dv8tion.jda.api.interactions.commands.Command;
|
||||
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.Commands;
|
||||
@@ -49,13 +48,14 @@ import net.dv8tion.jda.api.interactions.commands.build.OptionData;
|
||||
import net.dv8tion.jda.api.interactions.components.ActionRow;
|
||||
import net.dv8tion.jda.api.interactions.components.buttons.Button;
|
||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||
import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction;
|
||||
import net.dv8tion.jda.api.utils.MemberCachePolicy;
|
||||
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@@ -67,9 +67,10 @@ public class DiscordBot {
|
||||
|
||||
@Getter
|
||||
private static final Map<String, SWCommand> commands = new HashMap<>();
|
||||
private final OptionData commandArgument = new OptionData(OptionType.STRING, ARGUMENT_NAME, "Command arguments", false);
|
||||
|
||||
public static void withBot(Consumer<DiscordBot> consumer) {
|
||||
if(instance != null)
|
||||
if (instance != null)
|
||||
consumer.accept(instance);
|
||||
}
|
||||
|
||||
@@ -130,6 +131,7 @@ public class DiscordBot {
|
||||
reply.system("DC_ROLE_ADDED", role.getAsMention());
|
||||
}
|
||||
}));
|
||||
|
||||
new StaticMessageChannel(config.channel("rules"), () -> new MessageCreateBuilder()
|
||||
.setEmbeds(new EmbedBuilder()
|
||||
.setDescription(String.join("\n", config.getRules()))
|
||||
@@ -141,9 +143,10 @@ public class DiscordBot {
|
||||
ActionRow.of(Button.link("https://steamwar.de", "Website"), Button.link("https://steamwar.de/youtube", "YouTube")),
|
||||
ActionRow.of(Button.primary("auth", Emoji.fromUnicode("U+2705")).withLabel("Minecraft verknüpfen"))
|
||||
), event -> {
|
||||
if(event.getComponentId().equals("auth"))
|
||||
if (event.getComponentId().equals("auth"))
|
||||
event.reply("Gebe innerhalb der nächsten 10 Minuten ``/verify " + AuthManager.createDiscordAuthToken(event.getUser()) + "`` auf dem Minecraft Server ein").setEphemeral(true).queue();
|
||||
});
|
||||
|
||||
List<ActionRow> actionRows = new ArrayList<>();
|
||||
List<Button> list = new ArrayList<>();
|
||||
for (DiscordTicketType type : DiscordTicketType.values()) {
|
||||
@@ -163,6 +166,7 @@ public class DiscordBot {
|
||||
.setColor(Color.RED)
|
||||
.build())
|
||||
.setComponents(actionRows), DiscordTicketHandler::openTicket);
|
||||
|
||||
eventChannel = new StaticMessageChannel(config.channel("events"), EventChannel::get);
|
||||
checklistChannel = new ChecklistChannel(config.channel("checklist"));
|
||||
config.getCouncilThread().forEach((roleId, threadId) -> new CouncilChannel(DiscordBot.getGuild().getRoleById(roleId), DiscordBot.getGuild().getThreadChannelById(threadId)));
|
||||
@@ -193,45 +197,32 @@ public class DiscordBot {
|
||||
|
||||
VelocityCore.schedule(CouncilChannel::updateAll).repeat(1, TimeUnit.HOURS).schedule();
|
||||
|
||||
VacationCommand vacationCommand = new VacationCommand();
|
||||
jda.addEventListener(
|
||||
new DiscordTicketHandler(),
|
||||
new DiscordTeamEvent(),
|
||||
new ChannelListener(),
|
||||
new DiscordSchemUpload()
|
||||
new DiscordSchemUpload(),
|
||||
vacationCommand
|
||||
);
|
||||
|
||||
commandSetup(jda.retrieveCommands().complete(), jda.updateCommands());
|
||||
}
|
||||
|
||||
private final OptionData commandArgument = new OptionData(OptionType.STRING, ARGUMENT_NAME, "Command arguments", false);
|
||||
private void commandSetup(List<Command> existing, CommandListUpdateAction updateCommands) {
|
||||
Set<String> correctCommands = new HashSet<>();
|
||||
for(Command command : existing) {
|
||||
if(!commands.containsKey(command.getName())) {
|
||||
command.delete().complete();
|
||||
continue;
|
||||
}
|
||||
|
||||
List<Command.Option> options = command.getOptions();
|
||||
if(options.size() != 1 || options.get(0).getType() != OptionType.STRING)
|
||||
command.editCommand().clearOptions().addOptions(commandArgument).complete();
|
||||
|
||||
correctCommands.add(command.getName());
|
||||
}
|
||||
|
||||
updateCommands
|
||||
.addCommands(commands
|
||||
.keySet().stream()
|
||||
.filter(command -> !correctCommands.contains(command))
|
||||
jda.updateCommands()
|
||||
.addCommands(commands.keySet()
|
||||
.stream()
|
||||
.filter(command -> command.matches("^[\\w-]+$"))
|
||||
.map(command -> Commands.slash(command, command).addOptions(commandArgument))
|
||||
.toArray(CommandData[]::new))
|
||||
.queue();
|
||||
|
||||
jda.getGuildById(1241489896909180998L)
|
||||
.upsertCommand(vacationCommand.COMMAND)
|
||||
.queue();
|
||||
}
|
||||
|
||||
private boolean activityToggle = false;
|
||||
|
||||
private void activity() {
|
||||
if(activityToggle) {
|
||||
if (activityToggle) {
|
||||
Event event = Event.get();
|
||||
jda.getPresence().setActivity(event != null ? Activity.competing("dem Event " + event.getEventName()) : Activity.playing("auf SteamWar.de"));
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2020 SteamWar.de-Serverteam
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.velocitycore.discord;
|
||||
|
||||
import it.unimi.dsi.fastutil.Pair;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.ScheduledEvent;
|
||||
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import net.dv8tion.jda.api.interactions.InteractionHook;
|
||||
import net.dv8tion.jda.api.interactions.commands.Command;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionType;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
|
||||
import net.dv8tion.jda.internal.interactions.CommandDataImpl;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class VacationCommand extends ListenerAdapter {
|
||||
|
||||
private final Guild guild = Objects.requireNonNull(DiscordBot.getInstance().getJda().getGuildById(1241489896909180998L));
|
||||
|
||||
public final CommandDataImpl COMMAND = new CommandDataImpl("vacation", "Verwalte deinen Urlaub");
|
||||
|
||||
public VacationCommand() {
|
||||
COMMAND.addSubcommands(new SubcommandData("create", "Erstelle deinen Urlaub")
|
||||
.addOptions(new OptionData(OptionType.STRING, "from", "Datum (TT.MM.JJJJ)", true),
|
||||
new OptionData(OptionType.STRING, "to", "Datum (TT.MM.JJJJ)", true)));
|
||||
COMMAND.addSubcommands(new SubcommandData("delete", "Lösche deinen Urlaub")
|
||||
.addOptions(new OptionData(OptionType.STRING, "vacation", "Dein Urlaub", true, true)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
|
||||
if (!event.getName().equals("vacation")) return;
|
||||
switch (event.getSubcommandName()) {
|
||||
case "create":
|
||||
createVacation(event);
|
||||
break;
|
||||
case "delete":
|
||||
deleteVacation(event);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static final DateTimeFormatter PARSER = DateTimeFormatter.ofPattern("dd.MM.uuuu");
|
||||
|
||||
private void createVacation(SlashCommandInteractionEvent event) {
|
||||
InteractionHook interactionHook = event.deferReply(true).complete();
|
||||
String from = event.getOption("from").getAsString();
|
||||
String to = event.getOption("to").getAsString();
|
||||
|
||||
LocalDateTime fromDate;
|
||||
try {
|
||||
fromDate = LocalDate.parse(from, PARSER).atStartOfDay();
|
||||
} catch (DateTimeParseException e) {
|
||||
interactionHook.editOriginal("Das Datumsformat ist falsch! Bitte verwenden Sie TT.MM.JJJJ für den ersten Tag deines Urlaubs").queue();
|
||||
return;
|
||||
}
|
||||
if (fromDate == null) {
|
||||
interactionHook.editOriginal("Das Datumsformat ist falsch! Bitte verwenden Sie TT.MM.JJJJ für den ersten Tag deines Urlaubs").queue();
|
||||
return;
|
||||
}
|
||||
if (!fromDate.isAfter(LocalDateTime.now())) {
|
||||
interactionHook.editOriginal("Bitte gib ein Datum in der Zukunft an!").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
LocalDateTime toDate;
|
||||
try {
|
||||
toDate = LocalDate.parse(to, PARSER).atTime(23, 59, 59);
|
||||
} catch (DateTimeParseException e) {
|
||||
interactionHook.editOriginal("Das Datumsformat ist falsch! Bitte verwenden Sie TT.MM.JJJJ für den letzten Tag deines Urlaubs").queue();
|
||||
return;
|
||||
}
|
||||
if (toDate == null) {
|
||||
interactionHook.editOriginal("Das Datumsformat ist falsch! Bitte verwenden Sie TT.MM.JJJJ für den letzten Tag deines Urlaubs").queue();
|
||||
return;
|
||||
}
|
||||
if (!toDate.isAfter(LocalDateTime.now())) {
|
||||
interactionHook.editOriginal("Bitte gib ein Datum in der Zukunft an!").queue();
|
||||
return;
|
||||
}
|
||||
if (!toDate.isAfter(fromDate)) {
|
||||
interactionHook.editOriginal("Bitte gib ein Datum nach dem ersten Urlaubstag an!").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
guild.createScheduledEvent(
|
||||
"Urlaub " + event.getMember().getEffectiveName(),
|
||||
event.getMember().getId(),
|
||||
OffsetDateTime.of(fromDate, ZoneId.of("Europe/Berlin").getRules().getOffset(fromDate)),
|
||||
OffsetDateTime.of(toDate, ZoneId.of("Europe/Berlin").getRules().getOffset(toDate))
|
||||
).onSuccess(scheduledEvent -> {
|
||||
interactionHook.editOriginal("Urlaub erstellt!").queue();
|
||||
})
|
||||
.onErrorMap(throwable -> {
|
||||
interactionHook.editOriginal("Urlaub konnte nicht erstellt werden!").queue();
|
||||
return null;
|
||||
})
|
||||
.queue();
|
||||
}
|
||||
|
||||
private void deleteVacation(SlashCommandInteractionEvent event) {
|
||||
InteractionHook interactionHook = event.deferReply(true).complete();
|
||||
String eventId = event.getOption("vacation").getAsString();
|
||||
ScheduledEvent scheduledEvent = guild.getScheduledEventById(eventId);
|
||||
if (scheduledEvent == null) {
|
||||
interactionHook.editOriginal("Konnte den Urlaub nicht finden!").queue();
|
||||
return;
|
||||
}
|
||||
scheduledEvent.delete()
|
||||
.onSuccess(unused -> {
|
||||
interactionHook.editOriginal("Urlaub gelöscht!").queue();
|
||||
})
|
||||
.onErrorMap(throwable -> {
|
||||
interactionHook.editOriginal("Urlaub konnte nicht gelöscht werden!").queue();
|
||||
return null;
|
||||
})
|
||||
.queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommandAutoCompleteInteraction(@NotNull CommandAutoCompleteInteractionEvent event) {
|
||||
if (!event.getName().equals("vacation")) return;
|
||||
switch (event.getFocusedOption().getName()) {
|
||||
case "vacation":
|
||||
listVacations(event);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void listVacations(CommandAutoCompleteInteractionEvent event) {
|
||||
String vacation = event.getOption("vacation").getAsString();
|
||||
List<Command.Choice> choices = guild.getScheduledEvents()
|
||||
.stream()
|
||||
.filter(scheduledEvent -> scheduledEvent.getLocation().equals(event.getMember().getId()))
|
||||
.map(scheduledEvent -> {
|
||||
StringBuilder st = new StringBuilder();
|
||||
st.append(String.format("%02d", scheduledEvent.getStartTime().getDayOfMonth())).append(".");
|
||||
st.append(String.format("%02d", scheduledEvent.getStartTime().getMonthValue())).append(".");
|
||||
st.append(scheduledEvent.getStartTime().getYear());
|
||||
st.append(" - ");
|
||||
st.append(String.format("%02d", scheduledEvent.getEndTime().getDayOfMonth())).append(".");
|
||||
st.append(String.format("%02d", scheduledEvent.getEndTime().getMonthValue())).append(".");
|
||||
st.append(scheduledEvent.getEndTime().getYear());
|
||||
return Pair.of(scheduledEvent, st.toString());
|
||||
})
|
||||
.filter(pair -> pair.right().startsWith(vacation))
|
||||
.limit(25)
|
||||
.map(pair -> {
|
||||
return new Command.Choice(pair.right(), pair.left().getId());
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
event.replyChoices(choices)
|
||||
.queue();
|
||||
}
|
||||
}
|
||||
@@ -70,6 +70,7 @@ public class ChannelListener extends ListenerAdapter {
|
||||
|
||||
@Override
|
||||
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
|
||||
if (event.getGuild().getIdLong() == 1241489896909180998L) return;
|
||||
InteractionReply.reply(event, sender -> {
|
||||
if(sender.user().getDiscordId() == null)
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user