diff --git a/.gitignore b/.gitignore index fca7f4c..1241b87 100644 --- a/.gitignore +++ b/.gitignore @@ -2,10 +2,6 @@ .gradle/ build/ out/ -routes.js -database.db !.idea/copyright/hirobot.xml !.idea/copyright/profiles_settings.xml -hiro-akiba-8f9a5c0a58a0.json .env -bday.init.json5 diff --git a/build.gradle.kts b/build.gradle.kts index f2329e1..46b480f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -37,7 +37,7 @@ repositories { } dependencies { - implementation(group = "net.dv8tion", name = "JDA", version = "4.2.0_221") + implementation(group = "net.dv8tion", name = "JDA", version = "4.2.0_227") implementation(group = "com.jagrosh", name = "jda-utilities-command", version = "3.0.4") implementation(group = "com.fasterxml.jackson.core", name = "jackson-databind", version = "2.10.1") @@ -49,8 +49,6 @@ dependencies { implementation(group = "com.zaxxer", name = "HikariCP", version = "3.4.1") implementation(group = "org.xerial", name = "sqlite-jdbc", version = "3.30.1") - implementation(group = "com.google.cloud", name = "google-cloud-dialogflow", version = "2.0.0") - } tasks.withType { @@ -65,5 +63,5 @@ configure { tasks.withType { distributionType = DistributionType.ALL - gradleVersion = "6.1.1" + gradleVersion = "6.8" } diff --git a/data/.gitignore b/data/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/data/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 5c2d1cf..e708b1c 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 4e1cc9d..80cf08e 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-6.1.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index 83f2acf..4f906e0 100644 --- a/gradlew +++ b/gradlew @@ -82,6 +82,7 @@ esac CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + # Determine the Java command to use to start the JVM. if [ -n "$JAVA_HOME" ] ; then if [ -x "$JAVA_HOME/jre/sh/java" ] ; then @@ -129,6 +130,7 @@ fi if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then APP_HOME=`cygpath --path --mixed "$APP_HOME"` CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` # We build the pattern for arguments to be converted via cygpath @@ -154,19 +156,19 @@ if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then else eval `echo args$i`="\"$arg\"" fi - i=$((i+1)) + i=`expr $i + 1` done case $i in - (0) set -- ;; - (1) set -- "$args0" ;; - (2) set -- "$args0" "$args1" ;; - (3) set -- "$args0" "$args1" "$args2" ;; - (4) set -- "$args0" "$args1" "$args2" "$args3" ;; - (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; esac fi @@ -175,14 +177,9 @@ save () { for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done echo " " } -APP_ARGS=$(save "$@") +APP_ARGS=`save "$@"` # Collect all arguments for the java command, following the shell quoting and substitution rules eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" -# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong -if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then - cd "$(dirname "$0")" -fi - exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat index 24467a1..ac1b06f 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -29,6 +29,9 @@ if "%DIRNAME%" == "" set DIRNAME=. set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" @@ -37,7 +40,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init +if "%ERRORLEVEL%" == "0" goto execute echo. echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. @@ -51,7 +54,7 @@ goto fail set JAVA_HOME=%JAVA_HOME:"=% set JAVA_EXE=%JAVA_HOME%/bin/java.exe -if exist "%JAVA_EXE%" goto init +if exist "%JAVA_EXE%" goto execute echo. echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% @@ -61,28 +64,14 @@ echo location of your Java installation. goto fail -:init -@rem Get command-line arguments, handling Windows variants - -if not "%OS%" == "Windows_NT" goto win9xME_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* - :execute @rem Setup the command line set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + @rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* :end @rem End local scope for the variables with windows NT shell diff --git a/roles_map.json5 b/roles_map.json5 deleted file mode 100644 index b162335..0000000 --- a/roles_map.json5 +++ /dev/null @@ -1,47 +0,0 @@ -{ - // Campers - - // Keitaro - "514293667041771531": "672512679625097269", - // Hunter - "514294078570102784": "672513213954392074", - // Natsumi - "514293667192766465": "672513281306525716", - // Yoichi - "514293667595419663": "672513040775643162", - // Taiga - "514293667507208193": "672514266988806172", - // Seto - "514293667205349377": "672513416455520287", - // Felix - "514293667247423498": "672513666897149982", - // Lee - "586763383806754816": "672513740951781425", - // Eduard - "514293667234709519": "672513848716296193", - // Kieran - "652587591115472907": "672514904976261186", - // Aiden - "514293666936782850": "672515881590587393", - - // Cameos - - // Yuuto - "586763775156420618": "680944688165158995", - // Noah - "610168246477520919": "680945501851746324", - // Jirou - "586763281977311243": "680944951416455176", - // Knox, - "586763368044429312": "680945120585318562", - // Connor - "586763102306172958": "680945956212179115", - // Rayne - "667909099425628163": "680946036352745539", - // Avan - "586763085398933504": "680944793857294426", - // Haruki - "586763119234252810": "680945063618150474", - // Chiaki - "609824438715809957": "680944509936336929" -} \ No newline at end of file diff --git a/routes-raw.json b/routes-raw.json deleted file mode 100644 index f10ea75..0000000 --- a/routes-raw.json +++ /dev/null @@ -1 +0,0 @@ -[{"name":"Keitaro Nagame","age":19,"bday":"April 8","motif":"Frog","color":"#39fe19","tagline":"Keitaro is an energetic guy who loves to 'collect' memories through taking pictures. His playful and friendly nature makes him a natural crowdpleaser.","thb":"https://cdn.discordapp.com/emojis/514293667041771531.png?v=1"},{"name":"Hiro Akiba","age":19,"bday":"September 22","motif":"Raccoon","color":"#ff6600","tagline":"As Keitaro's childhood friend and best friend, Hiro has growing affection for Keitaro. He tends to get jealous easily, but his cheerful personality and talent for cooking make up for it!","thb":"https://cdn.discordapp.com/emojis/514293666853158913.png?v=1"},{"name":"Yoichi Yukimura","age":20,"bday":"January 21","motif":"Wolf","color":"#b11aff","tagline":"Yoichi heeds no authority and likes to bully others, giving him the reputation of a delinquent. No one understands the true reason behind his actions. Surprisingly, he has a soft heart when it comes to animals.","thb":"https://cdn.discordapp.com/emojis/514293667595419663.png?v=1"},{"name":"Natsumi Hamasaki","age":20,"bday":"June 20","motif":"Stag Beetle","color":"#1ab1ff","tagline":"Natsumi is seen as a role model among the campers due to his responsible and hardworking nature. However, he is pressured with the high expectations built around him.","thb":"https://cdn.discordapp.com/emojis/514293667192766465.png?v=1"},{"name":"Hunter Springfield","age":18,"bday":"March 20","motif":"Bunny","color":"#f7f63c","tagline":"Hunter has a shy and timid personality. He is always having a hard time socializing with others. It may look like that he prefers spending his time alone, but he just wants to have friends, just like everybody else.","thb":"https://cdn.discordapp.com/emojis/514294078570102784.png?v=1"},{"name":"Taiga Akatora","age":19,"bday":"July 13","motif":"Tiger","color":"#e81615","tagline":"Taiga is a born leader, but unfortunately, he's using it to drag his friends into his schemes. He has an unreasonable hate towards Keitaro.","thb":"https://cdn.discordapp.com/emojis/514293667507208193.png?v=1"},{"name":"Lee Kurosawa","age":19,"bday":"August 4","motif":"Snake","color":"#00aa6c","tagline":"Lee is very smart, but he's physically fragile. He looks up to Taiga as a role model for his bravery, hoping one day he could be as strong as him.","thb":"https://cdn.discordapp.com/emojis/586763383806754816.png?v=1"},{"name":"Eduard Fitzpatrick","age":19,"bday":"May 30","motif":"Flamingo","color":"#d37fb7","tagline":"Eduard is known for being extremely vain and sassy. No one's really sure how and why he's friends with Taiga.","thb":"https://cdn.discordapp.com/emojis/514293667234709519.png?v=1"},{"name":"Shintaro Aihara","age":18,"bday":"July 24","motif":"Cat","color":"#9dd5e6","tagline":"Seto usually spends a lot of his time playing games and listening to music. He's very chill and casual, but he can be really straightforward sometimes.","thb":"https://cdn.discordapp.com/emojis/514293667205349377.png?v=1"},{"name":"Felix Clermont","age":18,"bday":"September 26","motif":"Arctic Fox","color":"#bdd7e6","tagline":"Felix came from a wealthy family. He is used to getting what he wants. He can be quite difficult to deal with. Hopefully, Camp Buddy changes him for the better.","thb":"https://cdn.discordapp.com/emojis/514293667247423498.png?v=1"},{"name":"Aiden Flynn","age":27,"bday":"June 9","motif":"Lizard","color":"#5d8c6b","tagline":"Aiden is the high-spirited, all around helper of Camp Buddy. He is in charge of cooking, cleaning, gardening, repairs, healthcare and physical education.","thb":"https://cdn.discordapp.com/emojis/514293666936782850.png?v=1"},{"name":"Goro Nomoru","age":47,"bday":"July 3","motif":"Panda","color":"#9c9b99","tagline":"Goro is the owner and president of Camp Buddy. His grumpy and irritable attitude sometimes hinders his judgement, but he sincerely wants the best for the camp.","thb":"https://cdn.discordapp.com/emojis/514293666861416466.png?v=1"},{"name":"Yoshinori Nagira","age":29,"bday":"March 7","motif":"Grizzly Bear","color":"#c07c35","tagline":"Yoshinori is the optimistic and idealistic leader of Camp Buddy. He has yet to prove his determination to redeem his shortcomings form the past.","thb":"https://cdn.discordapp.com/emojis/514293667473653781.png?v=1"},{"name":"Yuri Nomoru","age":28,"bday":"May 8","motif":"Butterfly","color":"#ff73bb","tagline":"Yuri is the daughter of Goro and Yoshinori's assistant. Don't be fooled by her looks; she has a wild imagination when it comes to certain things.","thb":"https://cdn.discordapp.com/emojis/514291726069006336.png?v=1"},{"name":"Naoto Hamasaki","age":26,"bday":"June 14","motif":"Hercules Beetle","color":"#227abe","tagline":"Naoto is Natsumi's older brother. He's very charismatic and well-rounded. It seems like he has a huge influence on Natsumi's personality.","thb":"https://cdn.discordapp.com/emojis/586763400562868234.png?v=1"},{"name":"Kieran Moreno","age":19,"bday":"August 8","motif":"Lion","color":"#e7ab0d","tagline":"Kieran is a camper who used to attend Camp Buddy. He seems burdened by the past, and wants to make up for it as best as he can.","thb":"https://cdn.discordapp.com/emojis/652587591115472907.png?v=1"},{"name":"Jirou","age":18,"bday":"April 8","motif":"Red-Eyed Tree Frog","color":"#fead3e","tagline":"Jirou is a very hyper boy who always gets himself into trouble. He's a total klutz, stumbling and hurting himself all the time, but this doesn't stop him from jumping around and having fun.","thb":"https://cdn.discordapp.com/emojis/586763281977311243.png?v=1"},{"name":"Connor Bleufin","age":18,"bday":"June 2","motif":"Marlin","color":"#5ca0d5","tagline":"Connor is an energetic and fun-loving boy who lives in a fishing town. He and his best friend Jirou are always getting into trouble, but Connor is always there to look out for him.","thb":"https://cdn.discordapp.com/emojis/586763102306172958.png?v=1"},{"name":"Avan Geiserford","age":20,"bday":"January 9","motif":"Penguin","color":"#13c4d4","tagline":"A cool headed yet sporty guy who never seems to have anything on his mind but working his way towards a professional swimming career.","thb":"https://cdn.discordapp.com/emojis/586763085398933504.png?v=1"},{"name":"Yuuto Mizushima","age":20,"bday":"April 4","motif":"Sea Otter","color":"#fdbbe4","tagline":"Yuuto is a volleyball player with a springy and impulsive personality, but that doesn't stop him from making friends! He also loves the beach, but ironically... he can't even swim!","thb":"https://cdn.discordapp.com/emojis/586763775156420618.png?v=1"},{"name":"Haruki Sakurai","age":21,"bday":"August 19","motif":"Fox","color":"#f0534e","tagline":"Haruki spends most of his time preparing for his big idol debut. Beyond his charming personality lies more than meets the eye.","thb":"https://cdn.discordapp.com/emojis/586763119234252810.png?v=1"},{"name":"Chiaki Yumeno","age":20,"bday":"June 25","motif":"Ocelot","color":"#b46075","tagline":"Chiaki isn't afraid to be a tease and poke a little fun at everyone he meets. If there's one thing he's skilled at in creative arts, it's getting caught up in colourful messes!","thb":"https://cdn.discordapp.com/emojis/609824438715809957.png?v=1"},{"name":"Noah Reiner","age":19,"bday":"October 10","motif":"Panther","color":"#554ca0","tagline":"Noah is a happy-go-lucky graffiti artist and gamer. He has been caught in the middle of painting a few times, but that doesn't stop him from trying again.","thb":"https://cdn.discordapp.com/emojis/610168246477520919.png?v=1"},{"name":"Knox","age":19,"bday":"September 16","motif":"Jackal","color":"#a53154","tagline":"Renegade, trickster, and a lewd lord all around, Knox is a boy who’s all about fun in the naughtiest ways possible. Follow him as he flips around the world of boys’ love, trying (and failing) to keep out of trouble.","thb":"https://cdn.discordapp.com/emojis/586763368044429312.png?v=1"}] \ No newline at end of file diff --git a/routes.json b/routes.json deleted file mode 100644 index cb5fb81..0000000 --- a/routes.json +++ /dev/null @@ -1,56 +0,0 @@ -[ - { - "name": "Hiro Akiba", - "description": "As Keitaro's childhood friend and best friend, Hiro has growing affection for Keitaro. He tends to get jealous easily, but his cheerful personality and talent for cooking make up for it!", - "age": 19, - "birthday": "September 22", - "animal": "Raccoon", - "color": "#ff6600", - "emoteId": "514541939979452431" - }, - { - "name": "Keitaro Nagame", - "description": "Keitaro is an energetic guy who loves to 'collect' memories through taking pictures. His playful and friendly nature makes him a natural crowdpleaser.", - "age": 19, - "birthday": "April 8", - "animal": "Frog", - "color": "#42a82b", - "emoteId": "514627809919107072" - }, - { - "name": "Hunter Springfield", - "description": "Hunter has a shy and timid personality. He is always having a hard time socializing with others. It may look like that he prefers spending his time alone, but he just wants to have friends, just like everybody else.", - "age": 18, - "birthday": "March 20", - "animal": "Bunny", - "color": "#f7f63c", - "emoteId": "514543742167023625" - }, - { - "name": "Natsumi Hamasaki", - "description": "Natsumi is seen as a role model among the campers due to his responsible and hardworking nature. However, he is pressured with the high expectations built around him.", - "age": 20, - "birthday": "June 20", - "animal": "Stag Beetle", - "color": "#1ab1ff", - "emoteId": "514542811937505301" - }, - { - "name": "Yoichi Yukimura", - "description": "Yoichi heeds no authority and likes to bully others, giving him the reputation of a delinquent. No one understands the true reason behind his actions. Surprisingly, he has a soft heart when it comes to animals.", - "age": 20, - "birthday": "January 21", - "animal": "Wolf", - "color": "#b11aff", - "emoteId": "514543014979567617" - }, - { - "name": "Taiga Akatora", - "description": "Taiga is a born leader, but unfortunately, he's using it to drag his friends into his schemes. He has an unreasonable hate towards Keitaro.", - "age": 19, - "birthday": "July 13", - "animal": "Tiger", - "color": "#e81615", - "emoteId": "514535865691930634" - } -] \ No newline at end of file diff --git a/src/main/java/me/duncte123/hirobot/Hiro.java b/src/main/java/me/duncte123/hirobot/Hiro.java index 8657280..0a04409 100644 --- a/src/main/java/me/duncte123/hirobot/Hiro.java +++ b/src/main/java/me/duncte123/hirobot/Hiro.java @@ -29,7 +29,6 @@ import me.duncte123.hirobot.database.objects.Birthday; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; -import net.dv8tion.jda.api.entities.ChannelType; import net.dv8tion.jda.api.requests.GatewayIntent; import net.dv8tion.jda.api.utils.MemberCachePolicy; import net.dv8tion.jda.api.utils.cache.CacheFlag; @@ -113,15 +112,11 @@ private void helpConsumer(CommandEvent event) { } } - event.reply(builder.toString(), (unused) -> { - if (event.isFromType(ChannelType.TEXT)) { - event.reactSuccess(); - } - }, (t) -> event.replyWarning("Help cannot be sent because you are blocking Direct Messages.")); + event.reply(builder.toString()); } private void loadBirthdays() throws IOException { - final var bdayInit = new File("bday.init.json5"); + final var bdayInit = new File("./data/bday.init.json5"); if (bdayInit.exists()) { final List dataArray = ReactionHelpers.MAPPER.readValue(bdayInit, new TypeReference<>() {}); @@ -143,6 +138,11 @@ private static Map loadEnvironment() throws IOException { final List lines = Files.readAllLines(new File(".env").toPath()); for (String line : lines) { + // ignore comments + if (line.startsWith("#")) { + continue; + } + final String[] kv = line.split("="); env.put(kv[0], kv[1]); diff --git a/src/main/java/me/duncte123/hirobot/ReactionHelpers.java b/src/main/java/me/duncte123/hirobot/ReactionHelpers.java index b89291c..e16ce25 100644 --- a/src/main/java/me/duncte123/hirobot/ReactionHelpers.java +++ b/src/main/java/me/duncte123/hirobot/ReactionHelpers.java @@ -75,7 +75,7 @@ public class ReactionHelpers { }*/ public static void load() throws IOException { - final ObjectNode d = (ObjectNode) MAPPER.readTree(new File("roles_map.json5")); + final ObjectNode d = (ObjectNode) MAPPER.readTree(new File("./data/roles_map.json5")); d.fieldNames().forEachRemaining((key) -> { final long longKey = Long.parseLong(key); diff --git a/src/main/java/me/duncte123/hirobot/RoutesJSRegexParser.java b/src/main/java/me/duncte123/hirobot/RoutesJSRegexParser.java index e8918b9..cf0b6fc 100644 --- a/src/main/java/me/duncte123/hirobot/RoutesJSRegexParser.java +++ b/src/main/java/me/duncte123/hirobot/RoutesJSRegexParser.java @@ -32,8 +32,8 @@ public class RoutesJSRegexParser { public static void main(String[] args) throws Exception { - File input = new File("routes-raw.json"); - File output = new File("valentines.json"); + File input = new File("./data/routes-raw.json"); + File output = new File("./data/valentines.json"); String content = Files.readString(input.toPath()); diff --git a/src/main/java/me/duncte123/hirobot/chat/ChatBot.java b/src/main/java/me/duncte123/hirobot/chat/ChatBot.java deleted file mode 100644 index 5dd9873..0000000 --- a/src/main/java/me/duncte123/hirobot/chat/ChatBot.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Custom bot for the Hiro Akiba fan server on discord - * Copyright (C) 2020 Duncan "duncte123" Sterken - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package me.duncte123.hirobot.chat; - -import com.google.api.gax.core.FixedCredentialsProvider; -import com.google.auth.oauth2.GoogleCredentials; -import com.google.cloud.dialogflow.v2.*; -import com.google.common.collect.Lists; -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.io.FileInputStream; -import java.io.IOException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -public class ChatBot { - private static final Logger LOGGER = LoggerFactory.getLogger(ChatBot.class); - private static final String PROJECT_ID = "hiro-akiba-burymj"; - private static final String CREDS_FILE = "hiro-akiba-8f9a5c0a58a0.json"; - private static SessionsSettings SETTINGS = null; - private final ExecutorService executor = Executors.newSingleThreadExecutor((r) -> { - final Thread t = new Thread(r, "chatbot-thread"); - - // Don't prevent the jvm from shutting down - t.setDaemon(true); - - return t; - }); - - static { - try { - GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(CREDS_FILE)) - .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/dialogflow")); - - SETTINGS = SessionsSettings.newBuilder() - .setCredentialsProvider(FixedCredentialsProvider.create(credentials)) - .build(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - /** - * Handles the input from the chatbot - * - * @param input - * The contents of the message with the mention prefix stripped off - * @param event - * the event as received from discord - */ - public void handleInput(String input, GuildMessageReceivedEvent event) { - executor.submit(() -> { - try { - event.getChannel().sendTyping().queue(); - this.handleInternally(input, event); - } catch (Exception e) { - LOGGER.error("Failed to run chatbot", e); - } - }); - } - - private void handleInternally(String input, GuildMessageReceivedEvent event) throws Exception { - final User author = event.getAuthor(); - final String response = this.getResponse(input, author.getId()); - - event.getChannel() - .sendMessage(author.getAsMention()) - .append(", ") - .append(response) - .queue(); - } - - /** - * Get the chatbot response for an input - * - * @param input - * the user input - * @param userId - * the id of the user (used for the session id) - * - * @return the response from the bot - * - * @throws Exception when google did throw - */ - private String getResponse(String input, String userId) throws Exception { - try (SessionsClient sessionsClient = SessionsClient.create(SETTINGS)) { - final SessionName session = SessionName.of(PROJECT_ID, userId); - final TextInput.Builder textInput = TextInput.newBuilder().setText(input).setLanguageCode("en-US"); - // Build the query with the TextInput - final QueryInput queryInput = QueryInput.newBuilder().setText(textInput).build(); - // Performs the detect intent request - final DetectIntentResponse response = sessionsClient.detectIntent(session, queryInput); - // Display the query result - final QueryResult queryResult = response.getQueryResult(); - - LOGGER.info("===================="); - LOGGER.info("Query Text: '{}'", queryResult.getQueryText()); - LOGGER.info("Detected Intent: {} (confidence: {})", - queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence()); - LOGGER.info("Detected action: {}", queryResult.getAction()); - LOGGER.info("Fulfillment Text: '{}'", queryResult.getFulfillmentText()); - - return queryResult.getFulfillmentText(); - } - } -} diff --git a/src/main/java/me/duncte123/hirobot/commands/RouteCommand.java b/src/main/java/me/duncte123/hirobot/commands/RouteCommand.java index 7d8ca63..203974e 100644 --- a/src/main/java/me/duncte123/hirobot/commands/RouteCommand.java +++ b/src/main/java/me/duncte123/hirobot/commands/RouteCommand.java @@ -44,7 +44,7 @@ public RouteCommand() { this.cooldown = 10; this.cooldownScope = CooldownScope.GUILD; // USER is default - this.characters = loadCharactersFromFile("routes.json"); + this.characters = loadCharactersFromFile("./data/routes.json"); } @Override diff --git a/src/main/java/me/duncte123/hirobot/commands/ValentineCommand.java b/src/main/java/me/duncte123/hirobot/commands/ValentineCommand.java index 9876c2c..ebd076d 100644 --- a/src/main/java/me/duncte123/hirobot/commands/ValentineCommand.java +++ b/src/main/java/me/duncte123/hirobot/commands/ValentineCommand.java @@ -21,7 +21,6 @@ import com.jagrosh.jdautilities.command.Command; import com.jagrosh.jdautilities.command.CommandEvent; import me.duncte123.hirobot.database.Database; -import me.duncte123.hirobot.database.SQLiteDatabase; import me.duncte123.hirobot.objects.CBCharacter; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Member; @@ -43,7 +42,7 @@ public ValentineCommand(Database database) { this.cooldown = 20; this.database = database; - this.characters = loadCharactersFromFile("valentines.json"); + this.characters = loadCharactersFromFile("./data/valentines.json"); } @Override diff --git a/src/main/java/me/duncte123/hirobot/database/SQLiteDatabase.java b/src/main/java/me/duncte123/hirobot/database/SQLiteDatabase.java index a2d47e5..87c41c2 100644 --- a/src/main/java/me/duncte123/hirobot/database/SQLiteDatabase.java +++ b/src/main/java/me/duncte123/hirobot/database/SQLiteDatabase.java @@ -39,9 +39,9 @@ public class SQLiteDatabase implements Database { private final HikariDataSource ds; public SQLiteDatabase() { - try { - final File dbFile = new File("database.db"); + final File dbFile = new File("./data/database.db"); + try { if (!dbFile.exists()) { if (dbFile.createNewFile()) { LOGGER.info("Created database file"); @@ -55,7 +55,7 @@ public SQLiteDatabase() { } HikariConfig config = new HikariConfig(); - config.setJdbcUrl("jdbc:sqlite:database.db"); + config.setJdbcUrl("jdbc:sqlite:" + dbFile); config.setConnectionTestQuery("SELECT 1"); config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("prepStmtCacheSize", "250"); @@ -144,7 +144,9 @@ public List getBirthdays() { try (final Connection conn = ds.getConnection()) { try (final Statement smt = conn.createStatement()) { - try (final ResultSet resultSet = smt.executeQuery("SELECT * FROM birthdays")) { + try (final ResultSet resultSet = + // language=SQLite + smt.executeQuery("SELECT * FROM birthdays")) { while (resultSet.next()) { birthdays.add(new Birthday( resultSet.getLong("user_id"), diff --git a/src/main/java/me/duncte123/hirobot/events/FanServerEventHandler.java b/src/main/java/me/duncte123/hirobot/events/FanServerEventHandler.java index 18a13f3..43d0aae 100644 --- a/src/main/java/me/duncte123/hirobot/events/FanServerEventHandler.java +++ b/src/main/java/me/duncte123/hirobot/events/FanServerEventHandler.java @@ -20,7 +20,6 @@ import me.duncte123.hirobot.Hiro; import me.duncte123.hirobot.ReactionHelpers; -import me.duncte123.hirobot.chat.ChatBot; import me.duncte123.hirobot.database.Database; import me.duncte123.hirobot.database.objects.Birthday; import net.dv8tion.jda.api.JDA; @@ -68,7 +67,7 @@ public class FanServerEventHandler implements EventListener { "on Twitch", "Camp Buddy", "Minecraft", - "something nice ;)", + ";)", }; private static final String[] BDAY_MESSAGES = { @@ -79,8 +78,6 @@ public class FanServerEventHandler implements EventListener { "Hey listen! It's <@%s>'s bday today!", }; - private final ChatBot chatbot = new ChatBot(); - private final Hiro hiro; private final Database database; @@ -170,20 +167,6 @@ private void onGuildMessageReceived(@Nonnull GuildMessageReceivedEvent event) { jda.getHttpClient().dispatcher().executorService().shutdown(); System.exit(0); - - return; - } - - final SelfUser selfUser = event.getJDA().getSelfUser(); - final String selfId = selfUser.getId(); - final String selfMemberMention = "<@!" + selfId + '>'; - final String selfUserMention = selfUser.getAsMention(); - - if (contentRaw.startsWith(selfMemberMention) || contentRaw.startsWith(selfUserMention)) { - this.chatbot.handleInput( - contentRaw.replaceFirst("<@!?" + selfId + '>', "").trim(), - event - ); } } @@ -193,30 +176,35 @@ public void initBdayTimer() { final ZonedDateTime now = ZonedDateTime.now(zone); ZonedDateTime nextRun = now.withHour(12).withMinute(0).withSecond(0); + // if now is greather then the next run + // do a BDay pull right away and set the nextRun a day in the future if(now.compareTo(nextRun) > 0) { + handleBirthday(zone); nextRun = nextRun.plusDays(1); } final Duration duration = Duration.between(now, nextRun); final long initalDelay = duration.getSeconds(); - this.scheduler.scheduleAtFixedRate(() -> { - final Birthday birthday = this.database.getBirthday(LocalDate.now(zone)); - - if (birthday == null) { - return; - } - - final int i = ThreadLocalRandom.current().nextInt(BDAY_MESSAGES.length); - - //noinspection ConstantConditions - this.hiro.jda.getTextChannelById(GENERAL_CHANNEL_ID) - .sendMessageFormat(BDAY_MESSAGES[i], birthday.getUserId()) - .queue(); - }, + this.scheduler.scheduleAtFixedRate(() -> handleBirthday(zone), initalDelay, TimeUnit.DAYS.toSeconds(1), TimeUnit.SECONDS ); } + + private void handleBirthday(ZoneId zone) { + final Birthday birthday = this.database.getBirthday(LocalDate.now(zone)); + + if (birthday == null) { + return; + } + + final int i = ThreadLocalRandom.current().nextInt(BDAY_MESSAGES.length); + + //noinspection ConstantConditions + this.hiro.jda.getTextChannelById(GENERAL_CHANNEL_ID) + .sendMessageFormat(BDAY_MESSAGES[i], birthday.getUserId()) + .queue(); + } } diff --git a/src/main/java/me/duncte123/hirobot/objects/CBCharacter.java b/src/main/java/me/duncte123/hirobot/objects/CBCharacter.java index c2b953d..c77c839 100644 --- a/src/main/java/me/duncte123/hirobot/objects/CBCharacter.java +++ b/src/main/java/me/duncte123/hirobot/objects/CBCharacter.java @@ -46,7 +46,7 @@ public String getName() { } public String getFirstName() { - return name.split("\\s+")[0]; + return name.substring(0, name.indexOf(' ')); } public String getDescription() { diff --git a/valentines.json b/valentines.json deleted file mode 100644 index 68dd189..0000000 --- a/valentines.json +++ /dev/null @@ -1,218 +0,0 @@ -[ - { - "birthday": "April 8", - "color": "#39fe19", - "name": "Keitaro Nagame", - "description": "Keitaro is an energetic guy who loves to 'collect' memories through taking pictures. His playful and friendly nature makes him a natural crowdpleaser.", - "animal": "Frog", - "emoteId": "514293667041771531", - "age": 19 - }, - { - "birthday": "September 22", - "color": "#ff6600", - "name": "Hiro Akiba", - "description": "As Keitaro's childhood friend and best friend, Hiro has growing affection for Keitaro. He tends to get jealous easily, but his cheerful personality and talent for cooking make up for it!", - "animal": "Raccoon", - "emoteId": "514293666853158913", - "age": 19 - }, - { - "birthday": "January 21", - "color": "#b11aff", - "name": "Yoichi Yukimura", - "description": "Yoichi heeds no authority and likes to bully others, giving him the reputation of a delinquent. No one understands the true reason behind his actions. Surprisingly, he has a soft heart when it comes to animals.", - "animal": "Wolf", - "emoteId": "514293667595419663", - "age": 20 - }, - { - "birthday": "June 20", - "color": "#1ab1ff", - "name": "Natsumi Hamasaki", - "description": "Natsumi is seen as a role model among the campers due to his responsible and hardworking nature. However, he is pressured with the high expectations built around him.", - "animal": "Stag Beetle", - "emoteId": "514293667192766465", - "age": 20 - }, - { - "birthday": "March 20", - "color": "#f7f63c", - "name": "Hunter Springfield", - "description": "Hunter has a shy and timid personality. He is always having a hard time socializing with others. It may look like that he prefers spending his time alone, but he just wants to have friends, just like everybody else.", - "animal": "Bunny", - "emoteId": "514294078570102784", - "age": 18 - }, - { - "birthday": "July 13", - "color": "#e81615", - "name": "Taiga Akatora", - "description": "Taiga is a born leader, but unfortunately, he's using it to drag his friends into his schemes. He has an unreasonable hate towards Keitaro.", - "animal": "Tiger", - "emoteId": "514293667507208193", - "age": 19 - }, - { - "birthday": "August 4", - "color": "#00aa6c", - "name": "Lee Kurosawa", - "description": "Lee is very smart, but he's physically fragile. He looks up to Taiga as a role model for his bravery, hoping one day he could be as strong as him.", - "animal": "Snake", - "emoteId": "586763383806754816", - "age": 19 - }, - { - "birthday": "May 30", - "color": "#d37fb7", - "name": "Eduard Fitzpatrick", - "description": "Eduard is known for being extremely vain and sassy. No one's really sure how and why he's friends with Taiga.", - "animal": "Flamingo", - "emoteId": "514293667234709519", - "age": 19 - }, - { - "birthday": "July 24", - "color": "#9dd5e6", - "name": "Seto (Shintaro) Aihara", - "description": "Seto usually spends a lot of his time playing games and listening to music. He's very chill and casual, but he can be really straightforward sometimes.", - "animal": "Cat", - "emoteId": "514293667205349377", - "age": 18 - }, - { - "birthday": "September 26", - "color": "#bdd7e6", - "name": "Felix Clermont", - "description": "Felix came from a wealthy family. He is used to getting what he wants. He can be quite difficult to deal with. Hopefully, Camp Buddy changes him for the better.", - "animal": "Arctic Fox", - "emoteId": "514293667247423498", - "age": 18 - }, - { - "birthday": "June 9", - "color": "#5d8c6b", - "name": "Aiden Flynn", - "description": "Aiden is the high-spirited, all around helper of Camp Buddy. He is in charge of cooking, cleaning, gardening, repairs, healthcare and physical education.", - "animal": "Lizard", - "emoteId": "514293666936782850", - "age": 27 - }, - { - "birthday": "July 3", - "color": "#9c9b99", - "name": "Goro Nomoru", - "description": "Goro is the owner and president of Camp Buddy. His grumpy and irritable attitude sometimes hinders his judgement, but he sincerely wants the best for the camp.", - "animal": "Panda", - "emoteId": "514293666861416466", - "age": 47 - }, - { - "birthday": "March 7", - "color": "#c07c35", - "name": "Yoshinori Nagira", - "description": "Yoshinori is the optimistic and idealistic leader of Camp Buddy. He has yet to prove his determination to redeem his shortcomings form the past.", - "animal": "Grizzly Bear", - "emoteId": "514293667473653781", - "age": 29 - }, - { - "birthday": "May 8", - "color": "#ff73bb", - "name": "Yuri Nomoru", - "description": "Yuri is the daughter of Goro and Yoshinori's assistant. Don't be fooled by her looks; she has a wild imagination when it comes to certain things.", - "animal": "Butterfly", - "emoteId": "514291726069006336", - "age": 28 - }, - { - "birthday": "June 14", - "color": "#227abe", - "name": "Naoto Hamasaki", - "description": "Naoto is Natsumi's older brother. He's very charismatic and well-rounded. It seems like he has a huge influence on Natsumi's personality.", - "animal": "Hercules Beetle", - "emoteId": "586763400562868234", - "age": 26 - }, - { - "birthday": "August 8", - "color": "#e7ab0d", - "name": "Kieran Moreno", - "description": "Kieran is a camper who used to attend Camp Buddy. He seems burdened by the past, and wants to make up for it as best as he can.", - "animal": "Lion", - "emoteId": "652587591115472907", - "age": 19 - }, - { - "birthday": "April 8", - "color": "#fead3e", - "name": "Jirou", - "description": "Jirou is a very hyper boy who always gets himself into trouble. He's a total klutz, stumbling and hurting himself all the time, but this doesn't stop him from jumping around and having fun.", - "animal": "Red-Eyed Tree Frog", - "emoteId": "586763281977311243", - "age": 18 - }, - { - "birthday": "June 2", - "color": "#5ca0d5", - "name": "Connor Bleufin", - "description": "Connor is an energetic and fun-loving boy who lives in a fishing town. He and his best friend Jirou are always getting into trouble, but Connor is always there to look out for him.", - "animal": "Marlin", - "emoteId": "586763102306172958", - "age": 18 - }, - { - "birthday": "January 9", - "color": "#13c4d4", - "name": "Avan Geiserford", - "description": "A cool headed yet sporty guy who never seems to have anything on his mind but working his way towards a professional swimming career.", - "animal": "Penguin", - "emoteId": "586763085398933504", - "age": 20 - }, - { - "birthday": "April 4", - "color": "#fdbbe4", - "name": "Yuuto Mizushima", - "description": "Yuuto is a volleyball player with a springy and impulsive personality, but that doesn't stop him from making friends! He also loves the beach, but ironically... he can't even swim!", - "animal": "Sea Otter", - "emoteId": "586763775156420618", - "age": 20 - }, - { - "birthday": "August 19", - "color": "#f0534e", - "name": "Haruki Sakurai", - "description": "Haruki spends most of his time preparing for his big idol debut. Beyond his charming personality lies more than meets the eye.", - "animal": "Fox", - "emoteId": "586763119234252810", - "age": 21 - }, - { - "birthday": "June 25", - "color": "#b46075", - "name": "Chiaki Yumeno", - "description": "Chiaki isn't afraid to be a tease and poke a little fun at everyone he meets. If there's one thing he's skilled at in creative arts, it's getting caught up in colourful messes!", - "animal": "Ocelot", - "emoteId": "609824438715809957", - "age": 20 - }, - { - "birthday": "October 10", - "color": "#554ca0", - "name": "Noah Reiner", - "description": "Noah is a happy-go-lucky graffiti artist and gamer. He has been caught in the middle of painting a few times, but that doesn't stop him from trying again.", - "animal": "Panther", - "emoteId": "610168246477520919", - "age": 19 - }, - { - "birthday": "September 16", - "color": "#a53154", - "name": "Knox", - "description": "Renegade, trickster, and a lewd lord all around, Knox is a boy who’s all about fun in the naughtiest ways possible. Follow him as he flips around the world of boys’ love, trying (and failing) to keep out of trouble.", - "animal": "Jackal", - "emoteId": "586763368044429312", - "age": 19 - } -] \ No newline at end of file