1
0
mirror of https://github.com/sciwhiz12/Janitor.git synced 2024-10-18 06:01:27 +00:00

Add config file and CLI options

This commit is contained in:
SciWhiz12 2021-03-27 12:10:21 +08:00
parent 9e4659c7b7
commit 8a27740ecd
Signed by: sciwhiz12
GPG Key ID: 622CF446534317E1
9 changed files with 423 additions and 30 deletions

View File

@ -18,6 +18,9 @@ dependencies {
implementation "com.fasterxml.jackson.core:jackson-annotations:$jackson_ver" implementation "com.fasterxml.jackson.core:jackson-annotations:$jackson_ver"
implementation "com.fasterxml.jackson.core:jackson-databind:$jackson_ver" implementation "com.fasterxml.jackson.core:jackson-databind:$jackson_ver"
implementation "ch.qos.logback:logback-classic:$logback_ver" implementation "ch.qos.logback:logback-classic:$logback_ver"
implementation "com.electronwill.night-config:toml:$nightconfig_ver"
implementation "it.unimi.dsi:fastutil:$fastutil_ver"
implementation "net.sf.jopt-simple:jopt-simple:$jopt_ver"
testImplementation "org.junit.jupiter:junit-jupiter:$junit_ver" testImplementation "org.junit.jupiter:junit-jupiter:$junit_ver"
} }
@ -29,7 +32,7 @@ java {
} }
application { application {
mainClass = 'tk.sciwhiz12.janitor.App' mainClass = 'tk.sciwhiz12.janitor.bot.BotStartup'
} }
jar { jar {

View File

@ -3,4 +3,7 @@ junit_ver=5.7.1
jda_ver=4.2.0_222 jda_ver=4.2.0_222
guava_ver=29.0-jre guava_ver=29.0-jre
jackson_ver=2.11.2 jackson_ver=2.11.2
logback_ver=1.3.0-alpha5 logback_ver=1.3.0-alpha5
nightconfig_ver=3.6.3
fastutil_ver=8.5.2
jopt_ver=6.0-alpha-3

View File

@ -1,28 +0,0 @@
/*
* 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;
public class BotStartup {
public static void main(String[] args) {
}
}

View File

@ -0,0 +1,61 @@
/*
* 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.bot;
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 java.nio.file.Path;
public class BotStartup {
public static void main(String[] args) {
CommandLineBotOptions cmdLine = new CommandLineBotOptions(args);
Path configFile = cmdLine.getConfigFile();
BotFileConfig fileConfig = new BotFileConfig(configFile);
BotOptions options = new MergedBotOptions(fileConfig, cmdLine);
}
}

View File

@ -0,0 +1,104 @@
/*
* 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.
*/
/*
* 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.config;
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 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;
public BotFileConfig(Path botConfigFile) {
config = CommentedFileConfig.builder(botConfigFile)
.defaultResource("/bot-config.toml")
.build();
config.load();
}
@Override
public Optional<String> getToken() {
return config.<String>getOptional("token").map(str -> str.isBlank() ? null : str);
}
@Override
public Optional<LongSet> getBotOwners() {
//noinspection ConstantConditions
return config.<List<String>>getOptional("owners")
.map(list -> new LongArraySet(list.stream()
.filter(str -> Longs.tryParse(str) != null)
.map(Longs::tryParse)
.collect(Collectors.toUnmodifiableList())))
.map(list -> list.isEmpty() ? null : list);
}
}

View File

@ -0,0 +1,59 @@
/*
* 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.bot.config;
import it.unimi.dsi.fastutil.longs.LongSet;
import java.util.Optional;
/**
* Configuration options interface for the main bot configuration.
*
* <p>The configuration options exposed by this class are meant for controlling bot behaviors by the bot owner.
* These configuration options are also meant to be read-only.</p>
*/
public interface BotOptions {
Optional<String> getToken();
Optional<LongSet> getBotOwners();
}

View File

@ -0,0 +1,103 @@
/*
* 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.bot.config;
import com.google.common.primitives.Longs;
import it.unimi.dsi.fastutil.longs.LongArraySet;
import it.unimi.dsi.fastutil.longs.LongSet;
import joptsimple.ArgumentAcceptingOptionSpec;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.util.PathConverter;
import joptsimple.util.PathProperties;
import javax.annotation.Nullable;
import java.nio.file.Path;
import java.util.Optional;
import java.util.stream.Collectors;
import static java.util.List.of;
public class CommandLineBotOptions implements BotOptions {
@Nullable
private final String token;
private final LongSet owners;
private final Path configFile;
public CommandLineBotOptions(String[] args) {
OptionParser parser = new OptionParser();
ArgumentAcceptingOptionSpec<String> token = parser.acceptsAll(of("token", "t"), "Discord API token")
.withRequiredArg();
ArgumentAcceptingOptionSpec<String> owners = parser.acceptsAll(of("owner", "o"), "Snowflake ID(s) of bot owners")
.withRequiredArg().withValuesSeparatedBy(',');
ArgumentAcceptingOptionSpec<Path> configFile = parser.acceptsAll(of("config", "config-file", "c"), "Bot config file")
.withRequiredArg().withValuesConvertedBy(new PathConverter(PathProperties.FILE_EXISTING, PathProperties.WRITABLE))
.defaultsTo(Path.of("bot-config.toml"));
OptionSet parsed = parser.parse(args);
this.token = parsed.valueOf(token);
//noinspection ConstantConditions
this.owners = new LongArraySet(parsed.valuesOf(owners).stream()
.filter(str -> Longs.tryParse(str) != null)
.map(Longs::tryParse)
.collect(Collectors.toUnmodifiableList()));
this.configFile = parsed.valueOf(configFile);
}
@Override
public Optional<String> getToken() {
return Optional.ofNullable(this.token);
}
@Override
public Optional<LongSet> getBotOwners() {
return this.owners.isEmpty() ? Optional.empty() : Optional.of(this.owners);
}
public Path getConfigFile() {
return this.configFile;
}
}

View File

@ -0,0 +1,75 @@
/*
* 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.bot.config;
import it.unimi.dsi.fastutil.longs.LongSet;
import java.util.Optional;
public class MergedBotOptions implements BotOptions {
private final BotOptions parent;
private final BotOptions child;
public MergedBotOptions(BotOptions parent, BotOptions child) {
this.parent = parent;
this.child = child;
}
public BotOptions getParent() {
return parent;
}
public BotOptions getChild() {
return child;
}
@Override
public Optional<String> getToken() {
return child.getToken().or(parent::getToken);
}
@Override
public Optional<LongSet> getBotOwners() {
return child.getBotOwners().or(parent::getBotOwners);
}
}

View File

@ -0,0 +1,13 @@
# Bot-specific configuration for Janitor
# These configurations are read-only, and will not be modified
# The bot client token, used when connecting to the Discord API.
# Must be specified, either here or using the command line interface.
token = ""
# List of snowflake IDs of users who act as bot owners.
# Bot owners have access to bot maintenance commands, along with specific admin commands for troubleshooting
# May be unspecified. Defaults to sciwhiz12#1286, maintainer of Janitor
owners = [ 607058472709652501 ]