diff --git a/src/main/java/tk/sciwhiz12/janitor/JanitorBot.java b/src/main/java/tk/sciwhiz12/janitor/JanitorBot.java new file mode 100644 index 0000000..9624b18 --- /dev/null +++ b/src/main/java/tk/sciwhiz12/janitor/JanitorBot.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2021 SciWhiz12 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, subject + * to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/* + * Copyright (c) 2021 SciWhiz12 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, subject + * to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tk.sciwhiz12.janitor; + +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.OnlineStatus; +import net.dv8tion.jda.api.events.ReadyEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import net.dv8tion.jda.api.requests.GatewayIntent; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import tk.sciwhiz12.janitor.bot.BotOptions; + +import javax.security.auth.login.LoginException; +import java.time.OffsetDateTime; +import java.util.EnumSet; +import java.util.Objects; + +public class JanitorBot extends ListenerAdapter { + public static final Logger LOGGER = LoggerFactory.getLogger(JanitorBot.class); + // TODO: fine-tune the gateway intents + private static final EnumSet GATEWAY_INTENTS = EnumSet.allOf(GatewayIntent.class); + + private static final String version = Objects.requireNonNullElse(JanitorBot.class.getPackage().getImplementationVersion(), "DEVELOPMENT-" + OffsetDateTime.now().toString()); + + private final BotOptions options; + private final JDA jda; + + public JanitorBot(BotOptions options) throws LoginException { + LOGGER.info("Starting up Janitor v{}", version); + + this.options = options; + final String token = options.getToken().orElseThrow(() -> new IllegalArgumentException("Bot token not found. Supply via config file or CLI option")); + + this.jda = JDABuilder.createDefault(token, GATEWAY_INTENTS) + .setStatus(OnlineStatus.DO_NOT_DISTURB) + .addEventListeners(this) + .build(); + } + + @Override + public void onReady(@NotNull ReadyEvent event) { + LOGGER.info("Connection is ready"); + jda.getPresence().setPresence(OnlineStatus.ONLINE, null); + } + + public BotOptions getOptions() { + return options; + } + + public JDA getJda() { + return jda; + } +} diff --git a/src/main/java/tk/sciwhiz12/janitor/bot/config/BotOptions.java b/src/main/java/tk/sciwhiz12/janitor/bot/BotOptions.java similarity index 69% rename from src/main/java/tk/sciwhiz12/janitor/bot/config/BotOptions.java rename to src/main/java/tk/sciwhiz12/janitor/bot/BotOptions.java index 297f456..61f460c 100644 --- a/src/main/java/tk/sciwhiz12/janitor/bot/config/BotOptions.java +++ b/src/main/java/tk/sciwhiz12/janitor/bot/BotOptions.java @@ -40,7 +40,28 @@ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package tk.sciwhiz12.janitor.bot.config; +/* + * Copyright (c) 2021 SciWhiz12 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, subject + * to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package tk.sciwhiz12.janitor.bot; import it.unimi.dsi.fastutil.longs.LongSet; diff --git a/src/main/java/tk/sciwhiz12/janitor/bot/BotStartup.java b/src/main/java/tk/sciwhiz12/janitor/bot/BotStartup.java index bcf47cc..f079f49 100644 --- a/src/main/java/tk/sciwhiz12/janitor/bot/BotStartup.java +++ b/src/main/java/tk/sciwhiz12/janitor/bot/BotStartup.java @@ -42,20 +42,35 @@ package tk.sciwhiz12.janitor.bot; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import tk.sciwhiz12.janitor.JanitorBot; import tk.sciwhiz12.janitor.bot.config.BotFileConfig; -import tk.sciwhiz12.janitor.bot.config.BotOptions; import tk.sciwhiz12.janitor.bot.config.CommandLineBotOptions; import tk.sciwhiz12.janitor.bot.config.MergedBotOptions; +import javax.security.auth.login.LoginException; import java.nio.file.Path; public class BotStartup { - public static void main(String[] args) { + private static final Logger LOGGER = LoggerFactory.getLogger(BotStartup.class); + + public static void main(String[] args) throws LoginException { + LOGGER.info("Parsing command line options"); CommandLineBotOptions cmdLine = new CommandLineBotOptions(args); Path configFile = cmdLine.getConfigFile(); + LOGGER.info("Loading config file from {}", configFile.toAbsolutePath()); BotFileConfig fileConfig = new BotFileConfig(configFile); BotOptions options = new MergedBotOptions(fileConfig, cmdLine); + + if (options.getToken().isEmpty()) { + LOGGER.warn("No bot token found."); + LOGGER.warn("Please specify a bot token using the config file or the CLI option."); + return; + } + + new JanitorBot(options); } } diff --git a/src/main/java/tk/sciwhiz12/janitor/bot/config/BotFileConfig.java b/src/main/java/tk/sciwhiz12/janitor/bot/config/BotFileConfig.java index c948950..07b49e1 100644 --- a/src/main/java/tk/sciwhiz12/janitor/bot/config/BotFileConfig.java +++ b/src/main/java/tk/sciwhiz12/janitor/bot/config/BotFileConfig.java @@ -67,15 +67,13 @@ import com.electronwill.nightconfig.core.file.CommentedFileConfig; import com.google.common.primitives.Longs; import it.unimi.dsi.fastutil.longs.LongArraySet; import it.unimi.dsi.fastutil.longs.LongSet; +import tk.sciwhiz12.janitor.bot.BotOptions; import java.nio.file.Path; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; -/** - * File-backed bot configuration. - */ public class BotFileConfig implements BotOptions { private final CommentedFileConfig config; diff --git a/src/main/java/tk/sciwhiz12/janitor/bot/config/CommandLineBotOptions.java b/src/main/java/tk/sciwhiz12/janitor/bot/config/CommandLineBotOptions.java index 7615718..31eaed9 100644 --- a/src/main/java/tk/sciwhiz12/janitor/bot/config/CommandLineBotOptions.java +++ b/src/main/java/tk/sciwhiz12/janitor/bot/config/CommandLineBotOptions.java @@ -50,6 +50,7 @@ import joptsimple.OptionParser; import joptsimple.OptionSet; import joptsimple.util.PathConverter; import joptsimple.util.PathProperties; +import tk.sciwhiz12.janitor.bot.BotOptions; import javax.annotation.Nullable; import java.nio.file.Path; diff --git a/src/main/java/tk/sciwhiz12/janitor/bot/config/MergedBotOptions.java b/src/main/java/tk/sciwhiz12/janitor/bot/config/MergedBotOptions.java index 3d182b5..7c04d5b 100644 --- a/src/main/java/tk/sciwhiz12/janitor/bot/config/MergedBotOptions.java +++ b/src/main/java/tk/sciwhiz12/janitor/bot/config/MergedBotOptions.java @@ -43,6 +43,7 @@ package tk.sciwhiz12.janitor.bot.config; import it.unimi.dsi.fastutil.longs.LongSet; +import tk.sciwhiz12.janitor.bot.BotOptions; import java.util.Optional;