From 2f50240e0e5c8637230eaf9d831967ccb9aa1256 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rainer=20Gan=C3=9F?= Date: Fri, 20 Aug 2021 17:13:36 +0200 Subject: [PATCH 1/2] wip --- build.gradle | 3 + gradle/wrapper/gradle-wrapper.properties | 2 +- .../foursoft/discordbot/BotConfiguration.java | 14 +++ .../foursoft/discordbot/BotInitializer.java | 60 ++++++++++ .../de/foursoft/discordbot/BotListener.java | 113 ------------------ .../foursoft/discordbot/CommandRegistry.java | 21 ---- .../discordbot/FourSoftDiscordBot.java | 51 +------- .../discordbot/ListenerAdapterDispatcher.java | 33 +++++ .../foursoft/discordbot/commands/Command.java | 2 +- .../commands/GuildMessageReceivedCommand.java | 55 +++++++++ .../discordbot/commands/PingCommand.java | 6 +- .../discordbot/commands/ReactCommand.java | 5 +- .../discordbot/commands/ResetCommand.java | 5 +- .../discordbot/commands/SecretCommand.java | 7 +- .../discordbot/listener/DadListener.java | 2 + .../listener/ReactionAddEventConsumer.java | 32 +++++ src/main/resources/application.properties | 1 + 17 files changed, 223 insertions(+), 189 deletions(-) create mode 100644 src/main/java/de/foursoft/discordbot/BotConfiguration.java create mode 100644 src/main/java/de/foursoft/discordbot/BotInitializer.java delete mode 100644 src/main/java/de/foursoft/discordbot/BotListener.java delete mode 100644 src/main/java/de/foursoft/discordbot/CommandRegistry.java create mode 100644 src/main/java/de/foursoft/discordbot/ListenerAdapterDispatcher.java create mode 100644 src/main/java/de/foursoft/discordbot/listener/ReactionAddEventConsumer.java create mode 100644 src/main/resources/application.properties diff --git a/build.gradle b/build.gradle index 660b482..c71e7b9 100644 --- a/build.gradle +++ b/build.gradle @@ -2,6 +2,8 @@ plugins { id'java' id'application' id'com.github.johnrengelman.shadow' version '5.2.0' + id 'org.springframework.boot' version '2.5.4' + id 'io.spring.dependency-management' version '1.0.11.RELEASE' } group 'de.foursoft' @@ -20,6 +22,7 @@ repositories { } dependencies { + implementation 'org.springframework.boot:spring-boot-starter' implementation ('net.dv8tion:JDA:4.3.0_310') { exclude module: 'opus-java' } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 44e7c4d..3ab0b72 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.9.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/de/foursoft/discordbot/BotConfiguration.java b/src/main/java/de/foursoft/discordbot/BotConfiguration.java new file mode 100644 index 0000000..e963232 --- /dev/null +++ b/src/main/java/de/foursoft/discordbot/BotConfiguration.java @@ -0,0 +1,14 @@ +package de.foursoft.discordbot; + +import com.jagrosh.jdautilities.commons.waiter.EventWaiter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class BotConfiguration { + + @Bean + public EventWaiter eventWaiter(){ + return new EventWaiter(); + } +} diff --git a/src/main/java/de/foursoft/discordbot/BotInitializer.java b/src/main/java/de/foursoft/discordbot/BotInitializer.java new file mode 100644 index 0000000..7821c95 --- /dev/null +++ b/src/main/java/de/foursoft/discordbot/BotInitializer.java @@ -0,0 +1,60 @@ +package de.foursoft.discordbot; + +import com.jagrosh.jdautilities.commons.waiter.EventWaiter; +import de.foursoft.discordbot.commands.PingCommand; +import de.foursoft.discordbot.commands.ReactCommand; +import de.foursoft.discordbot.commands.ResetCommand; +import de.foursoft.discordbot.commands.SecretCommand; +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.requests.GatewayIntent; +import net.dv8tion.jda.api.utils.ChunkingFilter; +import net.dv8tion.jda.api.utils.MemberCachePolicy; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.security.auth.login.LoginException; + +@Component +public class BotInitializer { + private static final Logger LOGGER = LoggerFactory.getLogger(FourSoftDiscordBot.class); + + private final JDA discordApi; + + @Autowired + private EventWaiter eventWaiter; + @Autowired + private ListenerAdapterDispatcher dispatcher; + + public BotInitializer() { + Configuration config = new Configuration("config.properties"); + + JDA tmpJda = null; + try { + tmpJda = JDABuilder.createDefault(config.getToken()) // init default settings + .setChunkingFilter(ChunkingFilter.ALL) // load all members + .enableIntents( + GatewayIntent.GUILD_MEMBERS, // allow access to guild members + GatewayIntent.GUILD_PRESENCES // access to online status and activities + ) + .setMemberCachePolicy(MemberCachePolicy.ALL) // always cache members + .addEventListeners(dispatcher) + .addEventListeners(eventWaiter) // waits for events + .build().awaitReady(); // wait until the API is ready + } catch (LoginException e) { + LOGGER.error("Could not start the Bot, invalid Token!"); + System.exit(-1); + } catch (InterruptedException e) { + LOGGER.error("Could not start the Bot, interrupted while waiting!"); + System.exit(-1); + } + + discordApi = tmpJda; + } + + public JDA getDiscordApi() { + return discordApi; + } +} diff --git a/src/main/java/de/foursoft/discordbot/BotListener.java b/src/main/java/de/foursoft/discordbot/BotListener.java deleted file mode 100644 index a248738..0000000 --- a/src/main/java/de/foursoft/discordbot/BotListener.java +++ /dev/null @@ -1,113 +0,0 @@ -package de.foursoft.discordbot; - -import de.foursoft.discordbot.commands.Command; -import de.foursoft.discordbot.listener.DadListener; -import net.dv8tion.jda.api.entities.*; -import net.dv8tion.jda.api.entities.MessageReaction.ReactionEmote; -import net.dv8tion.jda.api.events.message.guild.GuildMessageDeleteEvent; -import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; -import net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent; -import net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent; -import net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionRemoveEvent; -import net.dv8tion.jda.api.hooks.ListenerAdapter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.regex.Pattern; - -public class BotListener extends ListenerAdapter { - - private static final Pattern WHITESPACES_PATTERN = Pattern.compile("\\s+"); - private static final Logger LOGGER = LoggerFactory.getLogger(BotListener.class); - - private static final String PREFIX = "!"; - -// private static final Map -// -// static { -// MAP.put(ListenerAdapter::onStageInstanceDelete, DadListener.class); -// } - - private final CommandRegistry commandRegistry; - - public BotListener(CommandRegistry commandRegistry) { - this.commandRegistry = commandRegistry; - } - - @Override - public void onGuildMessageReceived(GuildMessageReceivedEvent event) { - User user = event.getAuthor(); - Message message = event.getMessage(); - - - // Mentions will converted to ids, Markdown characters are included - String contentRaw = message.getContentRaw(); - - LOGGER.info("{}: {}", user.getAsTag(), contentRaw); - - //TODO insert into generic event dispatcher - new DadListener().accept(event); - - if (!contentRaw.startsWith(PREFIX)) { - return; - } - - // split at whitespaces - final String contentWithoutPrefix = contentRaw.substring(PREFIX.length()).trim(); - String[] args = WHITESPACES_PATTERN.split(contentWithoutPrefix); - - String userCommand = args[0].toLowerCase(); // args will never be empty due to the impl of split - if (userCommand.isEmpty()) { - return; - } - - final Command cmd = commandRegistry.getCommand(userCommand); - if (cmd == null) { - return; - } - - cmd.execute(event); - - } - - - @Override - public void onGuildMessageUpdate(GuildMessageUpdateEvent event) { - // means a message was edited or (un)pinned - // NOTE: Previous Content / State is not provided by the API - - Message message = event.getMessage(); - - LOGGER.info("[MESSAGE EDIT] {}", message.getContentRaw()); - } - - @Override - public void onGuildMessageDelete(GuildMessageDeleteEvent event) { - // NOTE: Message Content / State is not provided by the API - - TextChannel channel = event.getChannel(); - long msgId = event.getMessageIdLong(); - LOGGER.info("Message with Id {} was deleted from {}", msgId, channel.getName()); - } - - @Override - public void onGuildMessageReactionAdd(GuildMessageReactionAddEvent event) { - Member member = event.getMember(); - long msgId = event.getMessageIdLong(); - ReactionEmote reaction = event.getReactionEmote(); // can be emoji or emote - - boolean isEmoji = reaction.isEmoji(); // default emojis like :D - boolean isEmote = reaction.isEmote(); // emote = custom emoji, is bound to a server - - LOGGER.info("{} reacted to Message with Id {}", member.getUser().getAsTag(), msgId); - } - - - @Override - public void onGuildMessageReactionRemove(GuildMessageReactionRemoveEvent event) { - // Same as above - - LOGGER.info("{} removed reaction from Message with Id {}", event.getUser().getAsTag(), event.getMessageIdLong()); - } - -} diff --git a/src/main/java/de/foursoft/discordbot/CommandRegistry.java b/src/main/java/de/foursoft/discordbot/CommandRegistry.java deleted file mode 100644 index 5662461..0000000 --- a/src/main/java/de/foursoft/discordbot/CommandRegistry.java +++ /dev/null @@ -1,21 +0,0 @@ -package de.foursoft.discordbot; - -import de.foursoft.discordbot.commands.Command; -import net.dv8tion.jda.api.events.Event; - -import java.util.HashMap; -import java.util.Map; - -public class CommandRegistry { - - private final Map> commands = new HashMap<>(); - - public CommandRegistry addCommand(String name, Command command) { - commands.put(name, command); - return this; - } - - public Command getCommand(String name) { - return (Command)commands.get(name); - } -} diff --git a/src/main/java/de/foursoft/discordbot/FourSoftDiscordBot.java b/src/main/java/de/foursoft/discordbot/FourSoftDiscordBot.java index 6b619e8..8624865 100644 --- a/src/main/java/de/foursoft/discordbot/FourSoftDiscordBot.java +++ b/src/main/java/de/foursoft/discordbot/FourSoftDiscordBot.java @@ -12,57 +12,18 @@ import net.dv8tion.jda.api.utils.MemberCachePolicy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; import javax.security.auth.login.LoginException; +@SpringBootApplication +@ComponentScan("de.foursoft") public class FourSoftDiscordBot { - private static final Logger LOGGER = LoggerFactory.getLogger(FourSoftDiscordBot.class); - - private final JDA discordApi; - - private final EventWaiter eventWaiter = new EventWaiter(); - - private final CommandRegistry commandRegistry = new CommandRegistry(); - - public FourSoftDiscordBot() { - Configuration config = new Configuration("config.properties"); - - // init commands - commandRegistry.addCommand("ping", new PingCommand()) - .addCommand("react", new ReactCommand()) - .addCommand("secret", new SecretCommand(eventWaiter)) - .addCommand("reset", new ResetCommand()); - - JDA tmpJda = null; - try { - tmpJda = JDABuilder.createDefault(config.getToken()) // init default settings - .setChunkingFilter(ChunkingFilter.ALL) // load all members - .enableIntents( - GatewayIntent.GUILD_MEMBERS, // allow access to guild members - GatewayIntent.GUILD_PRESENCES // access to online status and activities - ) - .setMemberCachePolicy(MemberCachePolicy.ALL) // always cache members - .addEventListeners(new BotListener(commandRegistry)) // enable the class to receive events - .addEventListeners(eventWaiter) // waits for events - .build().awaitReady(); // wait until the API is ready - } catch (LoginException e) { - LOGGER.error("Could not start the Bot, invalid Token!"); - System.exit(-1); - } catch (InterruptedException e) { - LOGGER.error("Could not start the Bot, interrupted while waiting!"); - System.exit(-1); - } - - discordApi = tmpJda; - } - - public JDA getDiscordApi() { - return discordApi; - } - public static void main(String[] args) { - new FourSoftDiscordBot(); + SpringApplication.run(FourSoftDiscordBot.class, args); } } diff --git a/src/main/java/de/foursoft/discordbot/ListenerAdapterDispatcher.java b/src/main/java/de/foursoft/discordbot/ListenerAdapterDispatcher.java new file mode 100644 index 0000000..12fdc0f --- /dev/null +++ b/src/main/java/de/foursoft/discordbot/ListenerAdapterDispatcher.java @@ -0,0 +1,33 @@ +package de.foursoft.discordbot; + +import de.foursoft.discordbot.commands.Command; +import net.dv8tion.jda.api.events.Event; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.util.List; + +@Component +public class ListenerAdapterDispatcher implements InvocationHandler { + + @Autowired + private List allCommands; + + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + + if(args.length != 1){ + return null; + } + + Object event = args[0]; + + allCommands.stream() + .filter(c -> c.getClassOfT().equals(event.getClass())) + .forEach(c -> c.execute((Event) event)); + + return null; + } +} diff --git a/src/main/java/de/foursoft/discordbot/commands/Command.java b/src/main/java/de/foursoft/discordbot/commands/Command.java index 8cea2e2..814940d 100644 --- a/src/main/java/de/foursoft/discordbot/commands/Command.java +++ b/src/main/java/de/foursoft/discordbot/commands/Command.java @@ -4,7 +4,7 @@ public abstract class Command { - public abstract String getName(); + public abstract Class getClassOfT(); public abstract void execute(T event); } diff --git a/src/main/java/de/foursoft/discordbot/commands/GuildMessageReceivedCommand.java b/src/main/java/de/foursoft/discordbot/commands/GuildMessageReceivedCommand.java index 1b75b55..ad58d69 100644 --- a/src/main/java/de/foursoft/discordbot/commands/GuildMessageReceivedCommand.java +++ b/src/main/java/de/foursoft/discordbot/commands/GuildMessageReceivedCommand.java @@ -1,6 +1,61 @@ package de.foursoft.discordbot.commands; +import de.foursoft.discordbot.listener.DadListener; +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.regex.Pattern; public abstract class GuildMessageReceivedCommand extends Command { + + private static final Logger LOGGER = LoggerFactory.getLogger(GuildMessageReceivedCommand.class); + + private static final Pattern WHITESPACES_PATTERN = Pattern.compile("\\s+"); + + private static final String PREFIX = "!"; + + public abstract String getName(); + + @Override + public void execute(GuildMessageReceivedEvent event) { + User user = event.getAuthor(); + Message message = event.getMessage(); + + + // Mentions will converted to ids, Markdown characters are included + String contentRaw = message.getContentRaw(); + + LOGGER.info("{}: {}", user.getAsTag(), contentRaw); + + //TODO insert into generic event dispatcher + new DadListener().accept(event); + + if (!contentRaw.startsWith(PREFIX)) { + return; + } + + // split at whitespaces + final String contentWithoutPrefix = contentRaw.substring(PREFIX.length()).trim(); + String[] args = WHITESPACES_PATTERN.split(contentWithoutPrefix); + + String userCommand = args[0].toLowerCase(); // args will never be empty due to the impl of split + if (userCommand.isEmpty()) { + return; + } + + if (getName().equals(userCommand)){ + doExecute(event); + } + + } + + protected abstract void doExecute(GuildMessageReceivedEvent event); + + @Override + public Class getClassOfT() { + return GuildMessageReceivedEvent.class; + } } diff --git a/src/main/java/de/foursoft/discordbot/commands/PingCommand.java b/src/main/java/de/foursoft/discordbot/commands/PingCommand.java index cc8b662..4ad96bc 100644 --- a/src/main/java/de/foursoft/discordbot/commands/PingCommand.java +++ b/src/main/java/de/foursoft/discordbot/commands/PingCommand.java @@ -1,15 +1,17 @@ package de.foursoft.discordbot.commands; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; +import org.springframework.stereotype.Component; +@Component public class PingCommand extends GuildMessageReceivedCommand { - @Override + public String getName() { return "ping"; } @Override - public void execute(GuildMessageReceivedEvent event) { + public void doExecute(GuildMessageReceivedEvent event) { event.getChannel().sendMessage("Pong!") .queue(); // IMPORTANT - .queue is needed when request is made } diff --git a/src/main/java/de/foursoft/discordbot/commands/ReactCommand.java b/src/main/java/de/foursoft/discordbot/commands/ReactCommand.java index 98d2b75..3cb8d95 100644 --- a/src/main/java/de/foursoft/discordbot/commands/ReactCommand.java +++ b/src/main/java/de/foursoft/discordbot/commands/ReactCommand.java @@ -4,19 +4,20 @@ import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; +@Component public class ReactCommand extends GuildMessageReceivedCommand { private static final Logger LOGGER = LoggerFactory.getLogger(ReactCommand.class); private static final String THUMBS_UP_UNICODE = "\uD83D\uDC4D"; - @Override public String getName() { return "react"; } @Override - public void execute(GuildMessageReceivedEvent event) { + public void doExecute(GuildMessageReceivedEvent event) { Message message = event.getMessage(); message.addReaction(THUMBS_UP_UNICODE).queue(success -> { LOGGER.debug("Reaction worked!"); diff --git a/src/main/java/de/foursoft/discordbot/commands/ResetCommand.java b/src/main/java/de/foursoft/discordbot/commands/ResetCommand.java index 25f79f7..3baee46 100644 --- a/src/main/java/de/foursoft/discordbot/commands/ResetCommand.java +++ b/src/main/java/de/foursoft/discordbot/commands/ResetCommand.java @@ -7,16 +7,17 @@ import net.dv8tion.jda.api.entities.Category; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; +import org.springframework.stereotype.Component; +@Component public class ResetCommand extends GuildMessageReceivedCommand { - @Override public String getName() { return "reset"; } @Override - public void execute(GuildMessageReceivedEvent event) { + public void doExecute(GuildMessageReceivedEvent event) { Guild guild = event.getGuild(); Category category = guild.getCategoryById(PASSWORD_CATEGORY_ID); diff --git a/src/main/java/de/foursoft/discordbot/commands/SecretCommand.java b/src/main/java/de/foursoft/discordbot/commands/SecretCommand.java index 18c870d..e161bc8 100644 --- a/src/main/java/de/foursoft/discordbot/commands/SecretCommand.java +++ b/src/main/java/de/foursoft/discordbot/commands/SecretCommand.java @@ -7,15 +7,19 @@ import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; +import org.springframework.stereotype.Component; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; +@Component public class SecretCommand extends GuildMessageReceivedCommand { private static final Map PASSWORD_TO_CHANNELS = new HashMap<>(); + private static final long FAIL_CHANNEL = 852912833024491520L; + private final EventWaiter eventWaiter; { @@ -27,13 +31,12 @@ public SecretCommand(EventWaiter eventWaiter) { this.eventWaiter = eventWaiter; } - @Override public String getName() { return "secret"; } @Override - public void execute(GuildMessageReceivedEvent event) { + public void doExecute(GuildMessageReceivedEvent event) { Guild guild = event.getGuild(); User user = event.getAuthor(); SelfUser selfUser = event.getJDA().getSelfUser(); diff --git a/src/main/java/de/foursoft/discordbot/listener/DadListener.java b/src/main/java/de/foursoft/discordbot/listener/DadListener.java index 06e931e..eccebf7 100644 --- a/src/main/java/de/foursoft/discordbot/listener/DadListener.java +++ b/src/main/java/de/foursoft/discordbot/listener/DadListener.java @@ -3,10 +3,12 @@ import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.TextChannel; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; +import org.springframework.stereotype.Component; import java.util.*; import java.util.function.Consumer; +@Component public class DadListener implements Consumer { private static final Set VALID_KEY_PHRASES; diff --git a/src/main/java/de/foursoft/discordbot/listener/ReactionAddEventConsumer.java b/src/main/java/de/foursoft/discordbot/listener/ReactionAddEventConsumer.java new file mode 100644 index 0000000..59d29de --- /dev/null +++ b/src/main/java/de/foursoft/discordbot/listener/ReactionAddEventConsumer.java @@ -0,0 +1,32 @@ +package de.foursoft.discordbot.listener; + +import de.foursoft.discordbot.commands.Command; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.MessageReaction; +import net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +@Component +public class ReactionAddEventConsumer extends Command { + + private static final Logger LOGGER = LoggerFactory.getLogger(ReactionAddEventConsumer.class); + + @Override + public Class getClassOfT() { + return GuildMessageReactionAddEvent.class; + } + + @Override + public void execute(GuildMessageReactionAddEvent event) { + Member member = event.getMember(); + long msgId = event.getMessageIdLong(); + MessageReaction.ReactionEmote reaction = event.getReactionEmote(); // can be emoji or emote + + boolean isEmoji = reaction.isEmoji(); // default emojis like :D + boolean isEmote = reaction.isEmote(); // emote = custom emoji, is bound to a server + + LOGGER.info("{} reacted to Message with Id {}", member.getUser().getAsTag(), msgId); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..12c10a3 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.main.web-application-type=NONE \ No newline at end of file From 1ec8948a61423b25eab0d786a2a7f2920401d0fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rainer=20Gan=C3=9F?= Date: Fri, 20 Aug 2021 17:26:33 +0200 Subject: [PATCH 2/2] springify --- .../foursoft/discordbot/BotConfiguration.java | 15 ++ .../foursoft/discordbot/BotInitializer.java | 25 +-- .../discordbot/FourSoftDiscordBot.java | 14 -- .../discordbot/ListenerAdapterDispatcher.java | 20 +- .../DadEventConsumer.java} | 12 +- .../EventConsumer.java} | 4 +- .../GuildMessageReceivedEventConsumer.java} | 15 +- .../PingEventConsumer.java} | 4 +- .../ReactEventConsumer.java} | 6 +- .../ReactionAddEventConsumer.java | 5 +- .../ResetEventConsumer.java} | 4 +- .../SecretEventConsumer.java} | 190 +++++++++--------- 12 files changed, 157 insertions(+), 157 deletions(-) rename src/main/java/de/foursoft/discordbot/{listener/DadListener.java => eventconsumer/DadEventConsumer.java} (81%) rename src/main/java/de/foursoft/discordbot/{commands/Command.java => eventconsumer/EventConsumer.java} (57%) rename src/main/java/de/foursoft/discordbot/{commands/GuildMessageReceivedCommand.java => eventconsumer/GuildMessageReceivedEventConsumer.java} (81%) rename src/main/java/de/foursoft/discordbot/{commands/PingCommand.java => eventconsumer/PingEventConsumer.java} (77%) rename src/main/java/de/foursoft/discordbot/{commands/ReactCommand.java => eventconsumer/ReactEventConsumer.java} (83%) rename src/main/java/de/foursoft/discordbot/{listener => eventconsumer}/ReactionAddEventConsumer.java (85%) rename src/main/java/de/foursoft/discordbot/{commands/ResetCommand.java => eventconsumer/ResetEventConsumer.java} (88%) rename src/main/java/de/foursoft/discordbot/{commands/SecretCommand.java => eventconsumer/SecretEventConsumer.java} (93%) diff --git a/src/main/java/de/foursoft/discordbot/BotConfiguration.java b/src/main/java/de/foursoft/discordbot/BotConfiguration.java index e963232..dc52682 100644 --- a/src/main/java/de/foursoft/discordbot/BotConfiguration.java +++ b/src/main/java/de/foursoft/discordbot/BotConfiguration.java @@ -1,14 +1,29 @@ package de.foursoft.discordbot; import com.jagrosh.jdautilities.commons.waiter.EventWaiter; +import net.dv8tion.jda.api.hooks.EventListener; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.lang.reflect.Proxy; + @Configuration public class BotConfiguration { + @Autowired + ListenerAdapterDispatcher dispatcher; + @Bean public EventWaiter eventWaiter(){ return new EventWaiter(); } + + @Bean + public EventListener eventListener(){ + return (EventListener) Proxy.newProxyInstance( + BotConfiguration.class.getClassLoader(), + new Class[] { EventListener.class }, + dispatcher); + } } diff --git a/src/main/java/de/foursoft/discordbot/BotInitializer.java b/src/main/java/de/foursoft/discordbot/BotInitializer.java index 7821c95..2e59c44 100644 --- a/src/main/java/de/foursoft/discordbot/BotInitializer.java +++ b/src/main/java/de/foursoft/discordbot/BotInitializer.java @@ -1,12 +1,8 @@ package de.foursoft.discordbot; import com.jagrosh.jdautilities.commons.waiter.EventWaiter; -import de.foursoft.discordbot.commands.PingCommand; -import de.foursoft.discordbot.commands.ReactCommand; -import de.foursoft.discordbot.commands.ResetCommand; -import de.foursoft.discordbot.commands.SecretCommand; -import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.hooks.EventListener; import net.dv8tion.jda.api.requests.GatewayIntent; import net.dv8tion.jda.api.utils.ChunkingFilter; import net.dv8tion.jda.api.utils.MemberCachePolicy; @@ -15,32 +11,31 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import javax.annotation.PostConstruct; import javax.security.auth.login.LoginException; @Component public class BotInitializer { private static final Logger LOGGER = LoggerFactory.getLogger(FourSoftDiscordBot.class); - private final JDA discordApi; - @Autowired private EventWaiter eventWaiter; @Autowired - private ListenerAdapterDispatcher dispatcher; + private EventListener eventListener; - public BotInitializer() { + @PostConstruct + private void init(){ Configuration config = new Configuration("config.properties"); - JDA tmpJda = null; try { - tmpJda = JDABuilder.createDefault(config.getToken()) // init default settings + JDABuilder.createDefault(config.getToken()) // init default settings .setChunkingFilter(ChunkingFilter.ALL) // load all members .enableIntents( GatewayIntent.GUILD_MEMBERS, // allow access to guild members GatewayIntent.GUILD_PRESENCES // access to online status and activities ) .setMemberCachePolicy(MemberCachePolicy.ALL) // always cache members - .addEventListeners(dispatcher) + .addEventListeners(eventListener) .addEventListeners(eventWaiter) // waits for events .build().awaitReady(); // wait until the API is ready } catch (LoginException e) { @@ -50,11 +45,5 @@ public BotInitializer() { LOGGER.error("Could not start the Bot, interrupted while waiting!"); System.exit(-1); } - - discordApi = tmpJda; - } - - public JDA getDiscordApi() { - return discordApi; } } diff --git a/src/main/java/de/foursoft/discordbot/FourSoftDiscordBot.java b/src/main/java/de/foursoft/discordbot/FourSoftDiscordBot.java index 8624865..867e56b 100644 --- a/src/main/java/de/foursoft/discordbot/FourSoftDiscordBot.java +++ b/src/main/java/de/foursoft/discordbot/FourSoftDiscordBot.java @@ -1,23 +1,9 @@ package de.foursoft.discordbot; -import com.jagrosh.jdautilities.commons.waiter.EventWaiter; -import de.foursoft.discordbot.commands.PingCommand; -import de.foursoft.discordbot.commands.ReactCommand; -import de.foursoft.discordbot.commands.ResetCommand; -import de.foursoft.discordbot.commands.SecretCommand; -import net.dv8tion.jda.api.JDA; -import net.dv8tion.jda.api.JDABuilder; -import net.dv8tion.jda.api.requests.GatewayIntent; -import net.dv8tion.jda.api.utils.ChunkingFilter; -import net.dv8tion.jda.api.utils.MemberCachePolicy; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; -import javax.security.auth.login.LoginException; - @SpringBootApplication @ComponentScan("de.foursoft") public class FourSoftDiscordBot { diff --git a/src/main/java/de/foursoft/discordbot/ListenerAdapterDispatcher.java b/src/main/java/de/foursoft/discordbot/ListenerAdapterDispatcher.java index 12fdc0f..42af7fc 100644 --- a/src/main/java/de/foursoft/discordbot/ListenerAdapterDispatcher.java +++ b/src/main/java/de/foursoft/discordbot/ListenerAdapterDispatcher.java @@ -1,19 +1,32 @@ package de.foursoft.discordbot; -import de.foursoft.discordbot.commands.Command; +import de.foursoft.discordbot.eventconsumer.EventConsumer; import net.dv8tion.jda.api.events.Event; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import javax.annotation.PostConstruct; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; +import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; @Component public class ListenerAdapterDispatcher implements InvocationHandler { @Autowired - private List allCommands; + private List allEventConsumers; + + private Map> consumerByEvent; + + @PostConstruct + private void init(){ + consumerByEvent = allEventConsumers.stream().collect(Collectors.groupingBy(EventConsumer::getClassOfT)); + } + @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { @@ -24,8 +37,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl Object event = args[0]; - allCommands.stream() - .filter(c -> c.getClassOfT().equals(event.getClass())) + consumerByEvent.getOrDefault(event.getClass(), Collections.emptyList()) .forEach(c -> c.execute((Event) event)); return null; diff --git a/src/main/java/de/foursoft/discordbot/listener/DadListener.java b/src/main/java/de/foursoft/discordbot/eventconsumer/DadEventConsumer.java similarity index 81% rename from src/main/java/de/foursoft/discordbot/listener/DadListener.java rename to src/main/java/de/foursoft/discordbot/eventconsumer/DadEventConsumer.java index eccebf7..38890f8 100644 --- a/src/main/java/de/foursoft/discordbot/listener/DadListener.java +++ b/src/main/java/de/foursoft/discordbot/eventconsumer/DadEventConsumer.java @@ -1,4 +1,4 @@ -package de.foursoft.discordbot.listener; +package de.foursoft.discordbot.eventconsumer; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.TextChannel; @@ -6,10 +6,9 @@ import org.springframework.stereotype.Component; import java.util.*; -import java.util.function.Consumer; @Component -public class DadListener implements Consumer { +public class DadEventConsumer extends EventConsumer { private static final Set VALID_KEY_PHRASES; @@ -22,7 +21,12 @@ public class DadListener implements Consumer { } @Override - public void accept(GuildMessageReceivedEvent event) { + public Class getClassOfT() { + return GuildMessageReceivedEvent.class; + } + + @Override + public void execute(GuildMessageReceivedEvent event) { Message message = event.getMessage(); String contentRaw = message.getContentRaw(); diff --git a/src/main/java/de/foursoft/discordbot/commands/Command.java b/src/main/java/de/foursoft/discordbot/eventconsumer/EventConsumer.java similarity index 57% rename from src/main/java/de/foursoft/discordbot/commands/Command.java rename to src/main/java/de/foursoft/discordbot/eventconsumer/EventConsumer.java index 814940d..78c4f49 100644 --- a/src/main/java/de/foursoft/discordbot/commands/Command.java +++ b/src/main/java/de/foursoft/discordbot/eventconsumer/EventConsumer.java @@ -1,8 +1,8 @@ -package de.foursoft.discordbot.commands; +package de.foursoft.discordbot.eventconsumer; import net.dv8tion.jda.api.events.Event; -public abstract class Command { +public abstract class EventConsumer { public abstract Class getClassOfT(); diff --git a/src/main/java/de/foursoft/discordbot/commands/GuildMessageReceivedCommand.java b/src/main/java/de/foursoft/discordbot/eventconsumer/GuildMessageReceivedEventConsumer.java similarity index 81% rename from src/main/java/de/foursoft/discordbot/commands/GuildMessageReceivedCommand.java rename to src/main/java/de/foursoft/discordbot/eventconsumer/GuildMessageReceivedEventConsumer.java index ad58d69..aa34d47 100644 --- a/src/main/java/de/foursoft/discordbot/commands/GuildMessageReceivedCommand.java +++ b/src/main/java/de/foursoft/discordbot/eventconsumer/GuildMessageReceivedEventConsumer.java @@ -1,6 +1,5 @@ -package de.foursoft.discordbot.commands; +package de.foursoft.discordbot.eventconsumer; -import de.foursoft.discordbot.listener.DadListener; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; @@ -9,16 +8,13 @@ import java.util.regex.Pattern; -public abstract class GuildMessageReceivedCommand extends Command { +public abstract class GuildMessageReceivedEventConsumer extends EventConsumer { - private static final Logger LOGGER = LoggerFactory.getLogger(GuildMessageReceivedCommand.class); + private static final Logger LOGGER = LoggerFactory.getLogger(GuildMessageReceivedEventConsumer.class); private static final Pattern WHITESPACES_PATTERN = Pattern.compile("\\s+"); - private static final String PREFIX = "!"; - public abstract String getName(); - @Override public void execute(GuildMessageReceivedEvent event) { User user = event.getAuthor(); @@ -30,9 +26,6 @@ public void execute(GuildMessageReceivedEvent event) { LOGGER.info("{}: {}", user.getAsTag(), contentRaw); - //TODO insert into generic event dispatcher - new DadListener().accept(event); - if (!contentRaw.startsWith(PREFIX)) { return; } @@ -52,6 +45,8 @@ public void execute(GuildMessageReceivedEvent event) { } + protected abstract String getName(); + protected abstract void doExecute(GuildMessageReceivedEvent event); @Override diff --git a/src/main/java/de/foursoft/discordbot/commands/PingCommand.java b/src/main/java/de/foursoft/discordbot/eventconsumer/PingEventConsumer.java similarity index 77% rename from src/main/java/de/foursoft/discordbot/commands/PingCommand.java rename to src/main/java/de/foursoft/discordbot/eventconsumer/PingEventConsumer.java index 4ad96bc..3812f48 100644 --- a/src/main/java/de/foursoft/discordbot/commands/PingCommand.java +++ b/src/main/java/de/foursoft/discordbot/eventconsumer/PingEventConsumer.java @@ -1,10 +1,10 @@ -package de.foursoft.discordbot.commands; +package de.foursoft.discordbot.eventconsumer; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import org.springframework.stereotype.Component; @Component -public class PingCommand extends GuildMessageReceivedCommand { +public class PingEventConsumer extends GuildMessageReceivedEventConsumer { public String getName() { return "ping"; diff --git a/src/main/java/de/foursoft/discordbot/commands/ReactCommand.java b/src/main/java/de/foursoft/discordbot/eventconsumer/ReactEventConsumer.java similarity index 83% rename from src/main/java/de/foursoft/discordbot/commands/ReactCommand.java rename to src/main/java/de/foursoft/discordbot/eventconsumer/ReactEventConsumer.java index 3cb8d95..d0ebd6f 100644 --- a/src/main/java/de/foursoft/discordbot/commands/ReactCommand.java +++ b/src/main/java/de/foursoft/discordbot/eventconsumer/ReactEventConsumer.java @@ -1,4 +1,4 @@ -package de.foursoft.discordbot.commands; +package de.foursoft.discordbot.eventconsumer; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; @@ -7,8 +7,8 @@ import org.springframework.stereotype.Component; @Component -public class ReactCommand extends GuildMessageReceivedCommand { - private static final Logger LOGGER = LoggerFactory.getLogger(ReactCommand.class); +public class ReactEventConsumer extends GuildMessageReceivedEventConsumer { + private static final Logger LOGGER = LoggerFactory.getLogger(ReactEventConsumer.class); private static final String THUMBS_UP_UNICODE = "\uD83D\uDC4D"; diff --git a/src/main/java/de/foursoft/discordbot/listener/ReactionAddEventConsumer.java b/src/main/java/de/foursoft/discordbot/eventconsumer/ReactionAddEventConsumer.java similarity index 85% rename from src/main/java/de/foursoft/discordbot/listener/ReactionAddEventConsumer.java rename to src/main/java/de/foursoft/discordbot/eventconsumer/ReactionAddEventConsumer.java index 59d29de..07afeb1 100644 --- a/src/main/java/de/foursoft/discordbot/listener/ReactionAddEventConsumer.java +++ b/src/main/java/de/foursoft/discordbot/eventconsumer/ReactionAddEventConsumer.java @@ -1,6 +1,5 @@ -package de.foursoft.discordbot.listener; +package de.foursoft.discordbot.eventconsumer; -import de.foursoft.discordbot.commands.Command; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.MessageReaction; import net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent; @@ -9,7 +8,7 @@ import org.springframework.stereotype.Component; @Component -public class ReactionAddEventConsumer extends Command { +public class ReactionAddEventConsumer extends EventConsumer { private static final Logger LOGGER = LoggerFactory.getLogger(ReactionAddEventConsumer.class); diff --git a/src/main/java/de/foursoft/discordbot/commands/ResetCommand.java b/src/main/java/de/foursoft/discordbot/eventconsumer/ResetEventConsumer.java similarity index 88% rename from src/main/java/de/foursoft/discordbot/commands/ResetCommand.java rename to src/main/java/de/foursoft/discordbot/eventconsumer/ResetEventConsumer.java index 3baee46..f61d990 100644 --- a/src/main/java/de/foursoft/discordbot/commands/ResetCommand.java +++ b/src/main/java/de/foursoft/discordbot/eventconsumer/ResetEventConsumer.java @@ -1,4 +1,4 @@ -package de.foursoft.discordbot.commands; +package de.foursoft.discordbot.eventconsumer; import static de.foursoft.discordbot.GuildUtilsAndConstantsAndOtherUsefulStuffAndShit.PASSWORD_CATEGORY_ID; import static de.foursoft.discordbot.GuildUtilsAndConstantsAndOtherUsefulStuffAndShit.deleteChannel; @@ -10,7 +10,7 @@ import org.springframework.stereotype.Component; @Component -public class ResetCommand extends GuildMessageReceivedCommand { +public class ResetEventConsumer extends GuildMessageReceivedEventConsumer { public String getName() { return "reset"; diff --git a/src/main/java/de/foursoft/discordbot/commands/SecretCommand.java b/src/main/java/de/foursoft/discordbot/eventconsumer/SecretEventConsumer.java similarity index 93% rename from src/main/java/de/foursoft/discordbot/commands/SecretCommand.java rename to src/main/java/de/foursoft/discordbot/eventconsumer/SecretEventConsumer.java index e161bc8..e9d9d54 100644 --- a/src/main/java/de/foursoft/discordbot/commands/SecretCommand.java +++ b/src/main/java/de/foursoft/discordbot/eventconsumer/SecretEventConsumer.java @@ -1,95 +1,95 @@ -package de.foursoft.discordbot.commands; - -import static de.foursoft.discordbot.GuildUtilsAndConstantsAndOtherUsefulStuffAndShit.PASSWORD_CATEGORY_ID; -import static de.foursoft.discordbot.GuildUtilsAndConstantsAndOtherUsefulStuffAndShit.deleteChannel; - -import com.jagrosh.jdautilities.commons.waiter.EventWaiter; -import net.dv8tion.jda.api.Permission; -import net.dv8tion.jda.api.entities.*; -import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; -import org.springframework.stereotype.Component; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.TimeUnit; - -@Component -public class SecretCommand extends GuildMessageReceivedCommand { - private static final Map PASSWORD_TO_CHANNELS = new HashMap<>(); - - private static final long FAIL_CHANNEL = 852912833024491520L; - - private final EventWaiter eventWaiter; - - { - PASSWORD_TO_CHANNELS.put("1234", 852912770969370624L); - PASSWORD_TO_CHANNELS.put("1337", 852912856814845962L); - } - - public SecretCommand(EventWaiter eventWaiter) { - this.eventWaiter = eventWaiter; - } - - public String getName() { - return "secret"; - } - - @Override - public void doExecute(GuildMessageReceivedEvent event) { - Guild guild = event.getGuild(); - User user = event.getAuthor(); - SelfUser selfUser = event.getJDA().getSelfUser(); - final Category pwCategory = guild.getCategoryById(PASSWORD_CATEGORY_ID); - guild.createTextChannel("enter-the-password-" + user.getName(), pwCategory) - .addRolePermissionOverride(guild.getPublicRole().getIdLong(), null, Collections.singletonList(Permission.VIEW_CHANNEL)) - .addMemberPermissionOverride(user.getIdLong(), Collections.singletonList(Permission.VIEW_CHANNEL), null) - .addMemberPermissionOverride(selfUser.getIdLong(), Collections.singletonList(Permission.VIEW_CHANNEL), null) - .queue(pwChannel -> { - eventWaiter.waitForEvent(GuildMessageReceivedEvent.class, - userResponse -> userResponse.getAuthor().equals(user) && - userResponse.getChannel().equals(pwChannel), - userResponse -> { - - handlePasswordResponse(userResponse); - deleteChannel(pwChannel, 1, TimeUnit.MINUTES); - }, - 1, TimeUnit.MINUTES, () -> { - deleteChannel(pwChannel); - }); - }); - } - - private void handlePasswordResponse(GuildMessageReceivedEvent userResponse) { - final TextChannel channel = userResponse.getChannel(); - - Long targetChannelId = PASSWORD_TO_CHANNELS.get(userResponse.getMessage() - .getContentRaw()); - - long channelId; - String responseMessage; - if (targetChannelId == null) { - channelId = FAIL_CHANNEL; - responseMessage = "password incorrect!"; - } else { - channelId = targetChannelId; - responseMessage = "password correct, you have permission to enter the <#" + targetChannelId + ">"; - } - TextChannel targetChannel = userResponse.getGuild().getTextChannelById(channelId); - - if (targetChannel == null) { - userResponse.getGuild().getOwner().getUser().openPrivateChannel().queue(privateChannel -> { - privateChannel.sendMessage("Channel with id " + channelId + " does not exist anymore!").queue(); - }); - - channel.sendMessage("Internal Server Error, please try again later.").queue(); - return; - } - - targetChannel - .upsertPermissionOverride(userResponse.getMember()) - .setAllow(Permission.VIEW_CHANNEL) - .queue(); - channel.sendMessage(responseMessage).queue(); - } -} +package de.foursoft.discordbot.eventconsumer; + +import static de.foursoft.discordbot.GuildUtilsAndConstantsAndOtherUsefulStuffAndShit.PASSWORD_CATEGORY_ID; +import static de.foursoft.discordbot.GuildUtilsAndConstantsAndOtherUsefulStuffAndShit.deleteChannel; + +import com.jagrosh.jdautilities.commons.waiter.EventWaiter; +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; +import org.springframework.stereotype.Component; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +@Component +public class SecretEventConsumer extends GuildMessageReceivedEventConsumer { + private static final Map PASSWORD_TO_CHANNELS = new HashMap<>(); + + private static final long FAIL_CHANNEL = 852912833024491520L; + + private final EventWaiter eventWaiter; + + { + PASSWORD_TO_CHANNELS.put("1234", 852912770969370624L); + PASSWORD_TO_CHANNELS.put("1337", 852912856814845962L); + } + + public SecretEventConsumer(EventWaiter eventWaiter) { + this.eventWaiter = eventWaiter; + } + + public String getName() { + return "secret"; + } + + @Override + public void doExecute(GuildMessageReceivedEvent event) { + Guild guild = event.getGuild(); + User user = event.getAuthor(); + SelfUser selfUser = event.getJDA().getSelfUser(); + final Category pwCategory = guild.getCategoryById(PASSWORD_CATEGORY_ID); + guild.createTextChannel("enter-the-password-" + user.getName(), pwCategory) + .addRolePermissionOverride(guild.getPublicRole().getIdLong(), null, Collections.singletonList(Permission.VIEW_CHANNEL)) + .addMemberPermissionOverride(user.getIdLong(), Collections.singletonList(Permission.VIEW_CHANNEL), null) + .addMemberPermissionOverride(selfUser.getIdLong(), Collections.singletonList(Permission.VIEW_CHANNEL), null) + .queue(pwChannel -> { + eventWaiter.waitForEvent(GuildMessageReceivedEvent.class, + userResponse -> userResponse.getAuthor().equals(user) && + userResponse.getChannel().equals(pwChannel), + userResponse -> { + + handlePasswordResponse(userResponse); + deleteChannel(pwChannel, 1, TimeUnit.MINUTES); + }, + 1, TimeUnit.MINUTES, () -> { + deleteChannel(pwChannel); + }); + }); + } + + private void handlePasswordResponse(GuildMessageReceivedEvent userResponse) { + final TextChannel channel = userResponse.getChannel(); + + Long targetChannelId = PASSWORD_TO_CHANNELS.get(userResponse.getMessage() + .getContentRaw()); + + long channelId; + String responseMessage; + if (targetChannelId == null) { + channelId = FAIL_CHANNEL; + responseMessage = "password incorrect!"; + } else { + channelId = targetChannelId; + responseMessage = "password correct, you have permission to enter the <#" + targetChannelId + ">"; + } + TextChannel targetChannel = userResponse.getGuild().getTextChannelById(channelId); + + if (targetChannel == null) { + userResponse.getGuild().getOwner().getUser().openPrivateChannel().queue(privateChannel -> { + privateChannel.sendMessage("Channel with id " + channelId + " does not exist anymore!").queue(); + }); + + channel.sendMessage("Internal Server Error, please try again later.").queue(); + return; + } + + targetChannel + .upsertPermissionOverride(userResponse.getMember()) + .setAllow(Permission.VIEW_CHANNEL) + .queue(); + channel.sendMessage(responseMessage).queue(); + } +}