From 713c0f068a011f292c8c992a90a83c253bb8f746 Mon Sep 17 00:00:00 2001 From: Sandy Mossgrave Date: Sun, 7 May 2023 23:54:46 +0000 Subject: [PATCH] Added build IDs and moved program name / version information to a .properties file. --- pom.xml | 137 ++++++++++------ .../com/tinyplantnews/priestess/Command.java | 12 ++ .../priestess/Configuration.java | 26 +++ .../tinyplantnews/priestess/Priestess.java | 149 +++++++++++------- 4 files changed, 213 insertions(+), 111 deletions(-) diff --git a/pom.xml b/pom.xml index 3f6d7ca..2a9acb9 100644 --- a/pom.xml +++ b/pom.xml @@ -3,61 +3,96 @@ 4.0.0 com.tinyplantnews.priestess Priestess - 0.0 + Priestesss of the Bean Goose Cult + 0.1 jar + + scm:git:https://tinyplantnews.com/git/Priestess.git + scm:git:https://twi@10.0.0.1:/srv/git/Priestess.git + - - - org.apache.maven.plugins - maven-shade-plugin - 3.4.1 - - - package - - shade - - - - - com.tinyplantnews.priestess.Priestess - - - - - - - + + + org.codehaus.mojo + buildnumber-maven-plugin + 1.4 + + + buildnumber + validate + + create + + + + + {0,number} + + buildNumber + + false + false + unknownbuild + + + + org.apache.maven.plugins + maven-shade-plugin + 3.4.1 + + + package + + shade + + + + + com.tinyplantnews.priestess.Priestess + + + + + + + + ${project.artifactId}-${project.version}.${buildNumber} + + + src/main/java/com/tinyplantnews/priestess/version + true + + - - com.discord4j - discord4j-core - 3.2.3 - - - org.junit.jupiter - junit-jupiter-api - 5.6.0 - test - - - org.junit.jupiter - junit-jupiter-params - 5.6.0 - test - - - org.junit.jupiter - junit-jupiter-engine - 5.6.0 - test - + + com.discord4j + discord4j-core + 3.2.3 + + + org.junit.jupiter + junit-jupiter-api + 5.6.0 + test + + + org.junit.jupiter + junit-jupiter-params + 5.6.0 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.6.0 + test + - UTF-8 - 18 - 18 - com.tinyplantnews.priestess.Priestess + UTF-8 + 18 + 18 + com.tinyplantnews.priestess.Priestess - \ No newline at end of file + diff --git a/src/main/java/com/tinyplantnews/priestess/Command.java b/src/main/java/com/tinyplantnews/priestess/Command.java index d763ae2..4943c72 100644 --- a/src/main/java/com/tinyplantnews/priestess/Command.java +++ b/src/main/java/com/tinyplantnews/priestess/Command.java @@ -47,6 +47,11 @@ public class Command { case FAILURE: replyTo(m, String.format("Command %s failed for some reason idfk", s)); return; + case NOMATCH: + replyTo(m, String.format("Command %s says it doesn't match command name", s)); + break; + default: + break; } } } @@ -112,6 +117,13 @@ public class Command { return name; } + /** + * @return a help string + */ + public String getHelp() { + return callback.getHelp(this.name); + } + public static Command commandThatJustRepliesWith(String name, String reply) { Command c = new Command(name, 1); c.setCallback(new Callback() { diff --git a/src/main/java/com/tinyplantnews/priestess/Configuration.java b/src/main/java/com/tinyplantnews/priestess/Configuration.java index b72872f..d8f24aa 100644 --- a/src/main/java/com/tinyplantnews/priestess/Configuration.java +++ b/src/main/java/com/tinyplantnews/priestess/Configuration.java @@ -205,4 +205,30 @@ public class Configuration { }); return sb.toString(); } + + private static Properties getJarProperties() { + Properties props = new Properties(); + try { + props.load(Configuration.class.getClassLoader().getResourceAsStream("version/version.properties")); + } catch (IOException e) { + log.log(Level.SEVERE, "Problem encountered while loading basic jar properties: {0}", e); + return null; + } + return props; + } + + public static final String PROGNAME; + public static final String VERSION; + + static { + Properties props = getJarProperties(); + if (props == null) { + PROGNAME = "Priestess, probably"; + VERSION = "(version unknown)"; + } else { + PROGNAME = props.getProperty("progname"); + VERSION = props.getProperty("version"); + } + } + } diff --git a/src/main/java/com/tinyplantnews/priestess/Priestess.java b/src/main/java/com/tinyplantnews/priestess/Priestess.java index 18be656..0042324 100644 --- a/src/main/java/com/tinyplantnews/priestess/Priestess.java +++ b/src/main/java/com/tinyplantnews/priestess/Priestess.java @@ -1,6 +1,4 @@ /* -:q:q -qqqqqqqq:qdf * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/${packagePath}/${mainClassName}.java to edit this template */ @@ -41,6 +39,8 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.reactivestreams.Publisher; import reactor.core.publisher.Mono; +import static com.tinyplantnews.priestess.Configuration.PROGNAME; +import static com.tinyplantnews.priestess.Configuration.VERSION; /** * @@ -103,6 +103,8 @@ public class Priestess { private int invocationsThisSession = 0; + private static boolean offlineMode = false; + public static void main(String[] args) { for (int i = 0; i < args.length; i++) { @@ -113,7 +115,9 @@ public class Priestess { if (args[i].startsWith("--")) { System.out.println("Command-line switch (" + args[i] + ") detected."); - String yarg = args[i].substring(2, args[i].indexOf('=')); + int endi = args[i].indexOf('='); + endi = endi > 0 ? endi : args[i].length(); + String yarg = args[i].substring(2, endi); switch (yarg) { case "config-file": Configuration.setCONFIG_FILE_LOCATION(yarg, false); @@ -124,7 +128,7 @@ public class Priestess { i++; break; case "debug-level": - String newlevel = args[i].substring(args[i].indexOf('=')+1); + String newlevel = args[i].substring(args[i].indexOf('=') + 1); Configuration.setConfigurationParameter(DEBUG_LEVEL, newlevel); trySetDebugLevel(newlevel); i++; @@ -142,6 +146,12 @@ public class Priestess { allChannelsCultable = true; log.log(Level.INFO, "Listening to all channels in joined guilds."); break; + case "version": + log.log(Level.INFO, PROGNAME + " " + VERSION); + break; + case "offline": + offlineMode = true; + break; default: log.log(Level.INFO, "Unknown command-line argument: {0}", yarg); } @@ -256,65 +266,73 @@ public class Priestess { log.log(Level.SEVERE, "bot token is null. aborting."); return; } - dc = DiscordClientBuilder.create(botToken).build().login().block(); - if (dc == null) { - log.log(Level.SEVERE, "gatewaydiscordclient was null. aborting."); - return; - } - String boot_time = ""; - Date d = new Date(); - SimpleDateFormat df = new SimpleDateFormat("dd MMM YYYY kk:mm"); - boot_time = df.format(d); - dc.updatePresence(ClientPresence.of(Status.ONLINE, ClientActivity.playing("since " + boot_time))).subscribe(); - dc.getGuilds().flatMap((g) -> { - if (!guildemojimaps.containsKey(g)) { - guildemojimaps.put(g, new GuildProfile(g)); - } - - if (!guildnames.containsKey(g.getName())) { - guildnames.put(g.getName(), g); - } - return Mono.empty(); - }).subscribe(); - - dc.getGuilds().flatMap((g) -> g.getChannels().flatMap((channel) -> { - - log.log(Level.INFO, "Checking cultability of channel {0}", channel.getName()); - if (channel.getName().equals(cultChannelName)) { - log.log(Level.INFO, "Channel " + channel.getName() + " in " + g.getName() + " is cultable"); - cultableChannelIDs.add(channel.getId()); - } - return Mono.empty(); - })).subscribe(); - - dc.getGuilds().flatMap(g -> g.getEmojis()).flatMap(g2 -> { - // system.out.println("emoji"); - registerEmoji(g2.getGuild().block(), g2.getId(), g2.getName()); - return Mono.empty(); - }).subscribe(); - - dc.on(ConnectEvent.class, event -> { - - log.log(Level.INFO, "connected"); - - return Mono.empty(); - }).subscribe(); - - dc.on(ChatInputInteractionEvent.class).flatMap(event -> { - return event.reply().withContent("agreed"); - }).subscribe(); - SysInListener sil = new SysInListener(); Thread t = new Thread(sil); t.start(); flywheel.put("System.in listener", t); + if (offlineMode) { + try { + t.join(); + } catch (InterruptedException e) { + t.interrupt(); + } + } else { + dc = DiscordClientBuilder.create(botToken).build().login().block(); + if (dc == null) { + log.log(Level.SEVERE, "gatewaydiscordclient was null. aborting."); + return; + } + String boot_time = ""; + Date d = new Date(); + SimpleDateFormat df = new SimpleDateFormat("dd MMM YYYY kk:mm"); + boot_time = df.format(d); + dc.updatePresence(ClientPresence.of(Status.ONLINE, ClientActivity.playing("since " + boot_time))) + .subscribe(); + dc.getGuilds().flatMap((g) -> { + if (!guildemojimaps.containsKey(g)) { + guildemojimaps.put(g, new GuildProfile(g)); + } - dc.on(MessageCreateEvent.class).filter(event -> !event.getMessage().getAuthor().get().isBot()) - .filter(g -> !g.getGuild().blockOptional().isEmpty()).flatMap(guildedmessagehandler).subscribe(); + if (!guildnames.containsKey(g.getName())) { + guildnames.put(g.getName(), g); + } + return Mono.empty(); + }).subscribe(); - dc.on(MessageCreateEvent.class).filter(event -> !event.getMessage().getAuthor().get().isBot()) - .filter(g -> g.getGuild().blockOptional().isEmpty()).flatMap(directmessagehandler).blockLast(); + dc.getGuilds().flatMap((g) -> g.getChannels().flatMap((channel) -> { + + log.log(Level.INFO, "Checking cultability of channel {0}", channel.getName()); + if (channel.getName().equals(cultChannelName)) { + log.log(Level.INFO, "Channel " + channel.getName() + " in " + g.getName() + " is cultable"); + cultableChannelIDs.add(channel.getId()); + } + return Mono.empty(); + })).subscribe(); + + dc.getGuilds().flatMap(g -> g.getEmojis()).flatMap(g2 -> { + // system.out.println("emoji"); + registerEmoji(g2.getGuild().block(), g2.getId(), g2.getName()); + return Mono.empty(); + }).subscribe(); + + dc.on(ConnectEvent.class, event -> { + + log.log(Level.INFO, "connected"); + + return Mono.empty(); + }).subscribe(); + + dc.on(ChatInputInteractionEvent.class).flatMap(event -> { + return event.reply().withContent("agreed"); + }).subscribe(); + + dc.on(MessageCreateEvent.class).filter(event -> !event.getMessage().getAuthor().get().isBot()) + .filter(g -> !g.getGuild().blockOptional().isEmpty()).flatMap(guildedmessagehandler).subscribe(); + + dc.on(MessageCreateEvent.class).filter(event -> !event.getMessage().getAuthor().get().isBot()) + .filter(g -> g.getGuild().blockOptional().isEmpty()).flatMap(directmessagehandler).blockLast(); + } } private int invocationsBeforeThisSession = 0; @@ -512,7 +530,7 @@ public class Priestess { } private Guild resolveGuildByNameOrId(String token) { - log.log(Level.INFO, "Finding guild by name or ID " + token); + log.log(Level.FINE, "Finding guild by name or ID " + token); if (guildnames.containsKey(token)) { return guildnames.get(token); } @@ -541,7 +559,7 @@ public class Priestess { } catch (InterruptedException ex) { Logger.getLogger(this.getClass().getName()).log(Level.INFO, "System.in Listener interrupted."); } - System.out.println("break repl"); + System.out.println("Breaking repl"); } } @@ -597,8 +615,7 @@ public class Priestess { @Override public Publisher apply(CommandArgs t) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from - // nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + throw new UnsupportedOperationException("Not supported yet."); } public String getHelp(String commandName) { @@ -843,7 +860,19 @@ public class Priestess { } }; + public final Callback listCommandsCommand = new Callback() { + @Override + public Publisher apply(CommandArgs o) { + o.replyWith(PROGNAME + " " + VERSION); + for (String name : Command.commands.keySet()) { + o.replyWith(" Help for command " + name + ": " + Command.commands.get(name).getHelp()); + } + return Mono.empty(); + } + }; + private void registerDefaultCommands() { + Command.registerCommand(new Command("help", 0).setCallback(listCommandsCommand)); Command.registerCommand(new Command("status", 1).setCallback(showCommand)); Command.registerCommand(new Command("reload messages", 1).setCallback(reloadMessageStoreCommand)); Command.registerCommand(new Command("use emoji", 1).setCallback(registerEmojiCommand));