1
0
mirror of https://github.com/sciwhiz12/Janitor.git synced 2024-10-10 02:51:26 +00:00

Add basic skeleton for the bot

This commit is contained in:
SciWhiz12 2021-03-27 13:05:35 +08:00
parent 0862c8b7f1
commit 832e236526
Signed by: sciwhiz12
GPG Key ID: 622CF446534317E1
6 changed files with 138 additions and 6 deletions

View File

@ -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<GatewayIntent> 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;
}
}

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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;