From 6810a0fbb853ceaa1f394625b90a9479e8d65b79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=ADan=20Errity?= Date: Sun, 4 Oct 2020 16:14:22 +0100 Subject: [PATCH 1/6] Configuration basis --- .gitignore | 5 +++++ README.md | 8 ++++++++ src/main/resources/configuration.json.example | 3 +++ 3 files changed, 16 insertions(+) create mode 100644 src/main/resources/configuration.json.example diff --git a/.gitignore b/.gitignore index 87d4436..c9edc33 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ +# Project-specific stuff + +src/main/resources/configuration.json + + # User-specific stuff .idea/ diff --git a/README.md b/README.md index b792bdb..ff9e4c0 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ # TCD-Discord-Bot + +Hacktoberfest project 2020 + +For the Trinity Class of '24' Discord. +https://discord.gg/9PuBCaD + +In order to deploy this yourself, you have to rename `configuration.json.example` to `configuration.json` and add a token aquired from the [developers portal](https://discord.com/developers) + diff --git a/src/main/resources/configuration.json.example b/src/main/resources/configuration.json.example new file mode 100644 index 0000000..d203e7f --- /dev/null +++ b/src/main/resources/configuration.json.example @@ -0,0 +1,3 @@ +{ + "token": "NzYyMzrthMrthwMjQ2OTc1r0MjEz.X3nlXg.DtBrEKhXZ8nvV4rgego6c9VtEtUKw" +} \ No newline at end of file From f38ba945b73debf9ca7501ed0c55d13ee7c469ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=ADan=20Errity?= Date: Sun, 4 Oct 2020 16:22:08 +0100 Subject: [PATCH 2/6] Add Contributing instructions --- CONTRIBUTING.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..e9b77b7 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,11 @@ +# Contributing + +I'm more than happy for people to contribute. +I'm following git-flow for the purposes of this project. If you have maintainer access please create a new branch with a name which indicates the purpose of the branch, i.e what it implements. Then proceed to create a pull request based on the develop branch, if it doesn't exist create the develop branch first. + +Delete your branches once they're merged into master. + +For external contributors, fork the repository and follow the same principals, create a new branch, new PR based on develop, etc etc. I'll get to it when I can. + +## Hacktoberfest 2020 +This project was developed during Hacktoberfest 2020. We welcome participants to contribute, as it will count towards for PR count. \ No newline at end of file From 237fa0b6a4c974360b5f6ae2abac59bb9b77ae24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=ADan=20Errity?= Date: Sun, 4 Oct 2020 18:17:24 +0100 Subject: [PATCH 3/6] JDA Basis / Login & Ready Event --- pom.xml | 56 +++++++++++++-- src/main/java/io/paradaux/tcdbot/TCDBot.java | 72 ++++++++++++++++++- .../tcdbot/api/ConfigurationCache.java | 25 +++++++ .../tcdbot/listeners/ReadyListener.java | 21 ++++++ 4 files changed, 169 insertions(+), 5 deletions(-) create mode 100644 src/main/java/io/paradaux/tcdbot/api/ConfigurationCache.java create mode 100644 src/main/java/io/paradaux/tcdbot/listeners/ReadyListener.java diff --git a/pom.xml b/pom.xml index 36d8fc4..e04b31c 100644 --- a/pom.xml +++ b/pom.xml @@ -8,6 +8,49 @@ tcd-bot 0.1.0-SNAPSHOT + + + jcenter + jcenter-bintray + https://jcenter.bintray.com + + + + + + junit + junit + 4.12 + test + + + + com.google.code.gson + gson + 2.8.6 + + + + org.slf4j + slf4j-simple + 1.7.21 + compile + + + + net.dv8tion + JDA + 4.2.0_184 + + + club.minnced + opus-java + + + compile + + + UTF-8 1.8 @@ -18,6 +61,7 @@ org.apache.maven.plugins maven-compiler-plugin + 3.8.1 1.6 1.6 @@ -30,17 +74,21 @@ 3.2.4 + package shade - true + false - - io.paradaux.tcdbot.TCDBot + + + false + ${project.artifactId} diff --git a/src/main/java/io/paradaux/tcdbot/TCDBot.java b/src/main/java/io/paradaux/tcdbot/TCDBot.java index 51726ab..92e90a1 100644 --- a/src/main/java/io/paradaux/tcdbot/TCDBot.java +++ b/src/main/java/io/paradaux/tcdbot/TCDBot.java @@ -1,9 +1,79 @@ package io.paradaux.tcdbot; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.stream.JsonReader; +import io.paradaux.tcdbot.api.ConfigurationCache; +import io.paradaux.tcdbot.listeners.ReadyListener; + +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.utils.cache.CacheFlag; +import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import sun.security.krb5.Config; + +import javax.security.auth.login.LoginException; +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; + public class TCDBot { + private static Logger logger; + public static Logger getLogger() { return logger; } + + private static JDA client; + + private static ConfigurationCache configurationCache; + public static ConfigurationCache getConfigurationCache() { return configurationCache; } + public static void main(String[] args) { - System.out.println("Hi"); + TCDBot bot = new TCDBot(); + } + + public TCDBot() { + logger = LoggerFactory.getLogger(getClass()); + logger.info("TCDBot is starting."); + + try { + configurationCache = readConfigurationFile(); + } catch (FileNotFoundException exception) { + logger.error("Cannot find the configuration file."); + return; + } + + client = login(configurationCache.getToken()); } + @Nullable + public JDA login(String token) { + JDABuilder builder = JDABuilder.createDefault(token); + + builder.disableCache(CacheFlag.MEMBER_OVERRIDES, CacheFlag.VOICE_STATE); + builder.setBulkDeleteSplittingEnabled(false); + + builder.addEventListeners(new ReadyListener(logger)); + + if (token == null) { + logger.warn("You have not set the token for your discord bot for discord2mc functionality. This has been disabled."); + return null; + } + + try { + return builder.build(); + } catch(LoginException exception) { + logger.warn("TCDBot failed to login with the provided token."); + return null; + } + } + + public ConfigurationCache readConfigurationFile() throws FileNotFoundException { + GsonBuilder builder = new GsonBuilder(); + Gson gson = builder.create(); + + BufferedReader bufferedReader = new BufferedReader(new FileReader("configuration.json")); + return gson.fromJson(bufferedReader, ConfigurationCache.class); + } } diff --git a/src/main/java/io/paradaux/tcdbot/api/ConfigurationCache.java b/src/main/java/io/paradaux/tcdbot/api/ConfigurationCache.java new file mode 100644 index 0000000..9072c61 --- /dev/null +++ b/src/main/java/io/paradaux/tcdbot/api/ConfigurationCache.java @@ -0,0 +1,25 @@ +package io.paradaux.tcdbot.api; + +public class ConfigurationCache { + + String token; + + public ConfigurationCache(String token) { + this.token = token; + } + + public ConfigurationCache() {} + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + @Override + public String toString() { + return "ConfigurationCache: [ token: " + token + "]"; + } +} diff --git a/src/main/java/io/paradaux/tcdbot/listeners/ReadyListener.java b/src/main/java/io/paradaux/tcdbot/listeners/ReadyListener.java new file mode 100644 index 0000000..e374cb4 --- /dev/null +++ b/src/main/java/io/paradaux/tcdbot/listeners/ReadyListener.java @@ -0,0 +1,21 @@ +package io.paradaux.tcdbot.listeners; + +import net.dv8tion.jda.api.events.ReadyEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import org.slf4j.Logger; + +public class ReadyListener extends ListenerAdapter { + + Logger logger; + + public ReadyListener(Logger logger) { + this.logger = logger; + } + + @Override + public void onReady(ReadyEvent event) { + logger.info("TCDBot has logged in successfully."); + logger.info("Serving " + event.getGuildTotalCount() + " guilds."); + } + +} From 41999e652f09717bb41b1d5832b0d58241c006e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=ADan=20Errity?= Date: Sun, 4 Oct 2020 18:30:48 +0100 Subject: [PATCH 4/6] added jda-utilities --- pom.xml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pom.xml b/pom.xml index e04b31c..934ca6f 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,11 @@ jcenter-bintray https://jcenter.bintray.com + + + jitpack.io + https://jitpack.io + @@ -49,6 +54,14 @@ compile + + + com.jagrosh + jda-utilities + 3.0.4 + compile + pom + From 8a6ac349fe887fb236cf70e23ae866a514bfeba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=ADan=20Errity?= Date: Sun, 4 Oct 2020 19:04:35 +0100 Subject: [PATCH 5/6] Configuration deployment / changes --- src/main/java/io/paradaux/tcdbot/TCDBot.java | 30 ++++++++++--- .../tcdbot/api/ConfigurationCache.java | 24 +++++++++- .../io/paradaux/tcdbot/api/FileUtils.java | 45 +++++++++++++++++++ src/main/resources/configuration.json.example | 4 +- 4 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 src/main/java/io/paradaux/tcdbot/api/FileUtils.java diff --git a/src/main/java/io/paradaux/tcdbot/TCDBot.java b/src/main/java/io/paradaux/tcdbot/TCDBot.java index 92e90a1..3048ab5 100644 --- a/src/main/java/io/paradaux/tcdbot/TCDBot.java +++ b/src/main/java/io/paradaux/tcdbot/TCDBot.java @@ -3,7 +3,11 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.stream.JsonReader; +import com.jagrosh.jdautilities.command.CommandClient; +import com.jagrosh.jdautilities.command.CommandClientBuilder; import io.paradaux.tcdbot.api.ConfigurationCache; +import io.paradaux.tcdbot.api.FileUtils; +import io.paradaux.tcdbot.listeners.MessageReceivedListener; import io.paradaux.tcdbot.listeners.ReadyListener; import net.dv8tion.jda.api.JDA; @@ -16,6 +20,7 @@ import javax.security.auth.login.LoginException; import java.io.BufferedReader; +import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; @@ -37,6 +42,16 @@ public TCDBot() { logger = LoggerFactory.getLogger(getClass()); logger.info("TCDBot is starting."); + if (!new File("configuration.yml").exists()) { + try { + FileUtils.ExportResource("configuration.json"); + } catch (Exception exception) { + logger.error("Failed to deploy configuration.\n" + exception.toString()); + return; + } + + } + try { configurationCache = readConfigurationFile(); } catch (FileNotFoundException exception) { @@ -49,12 +64,10 @@ public TCDBot() { @Nullable public JDA login(String token) { - JDABuilder builder = JDABuilder.createDefault(token); - - builder.disableCache(CacheFlag.MEMBER_OVERRIDES, CacheFlag.VOICE_STATE); - builder.setBulkDeleteSplittingEnabled(false); - - builder.addEventListeners(new ReadyListener(logger)); + JDABuilder builder = JDABuilder.createDefault(token) + .disableCache(CacheFlag.MEMBER_OVERRIDES, CacheFlag.VOICE_STATE) + .setBulkDeleteSplittingEnabled(false) + .addEventListeners(new ReadyListener(logger), new MessageReceivedListener(configurationCache)); if (token == null) { logger.warn("You have not set the token for your discord bot for discord2mc functionality. This has been disabled."); @@ -76,4 +89,9 @@ public ConfigurationCache readConfigurationFile() throws FileNotFoundException { BufferedReader bufferedReader = new BufferedReader(new FileReader("configuration.json")); return gson.fromJson(bufferedReader, ConfigurationCache.class); } + + public CommandClient createCommandClient() { + CommandClientBuilder builder = new CommandClientBuilder(); + return null; + } } diff --git a/src/main/java/io/paradaux/tcdbot/api/ConfigurationCache.java b/src/main/java/io/paradaux/tcdbot/api/ConfigurationCache.java index 9072c61..5326525 100644 --- a/src/main/java/io/paradaux/tcdbot/api/ConfigurationCache.java +++ b/src/main/java/io/paradaux/tcdbot/api/ConfigurationCache.java @@ -1,11 +1,17 @@ package io.paradaux.tcdbot.api; +import java.util.List; + public class ConfigurationCache { String token; + String prefix; + List courseSelectionChannels; - public ConfigurationCache(String token) { + public ConfigurationCache(String token, String prefix, List courseSelectionChannels) { this.token = token; + this.prefix = prefix; + this.courseSelectionChannels = courseSelectionChannels; } public ConfigurationCache() {} @@ -18,6 +24,22 @@ public void setToken(String token) { this.token = token; } + public String getPrefix() { + return prefix; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + } + + public List getCourseSelectionChannels() { + return courseSelectionChannels; + } + + public void setCourseSelectionChannels(List courseSelectionChannels) { + this.courseSelectionChannels = courseSelectionChannels; + } + @Override public String toString() { return "ConfigurationCache: [ token: " + token + "]"; diff --git a/src/main/java/io/paradaux/tcdbot/api/FileUtils.java b/src/main/java/io/paradaux/tcdbot/api/FileUtils.java new file mode 100644 index 0000000..f27ec77 --- /dev/null +++ b/src/main/java/io/paradaux/tcdbot/api/FileUtils.java @@ -0,0 +1,45 @@ +package io.paradaux.tcdbot.api; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; + +public class FileUtils { + + /** + * Export a resource embedded into a Jar file to the local file path. + * + * @param resourceName ie.: "/SmartLibrary.dll" + * @return The path to the exported resource + * @throws Exception + */ + public static String ExportResource(String resourceName) throws Exception { + InputStream stream = null; + OutputStream resStreamOut = null; + String jarFolder; + try { + stream = FileUtils.class.getResourceAsStream(resourceName);//note that each / is a directory down in the "jar tree" been the jar the root of the tree + if(stream == null) { + throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file."); + } + + int readBytes; + byte[] buffer = new byte[4096]; + jarFolder = new File(FileUtils.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile().getPath().replace('\\', '/'); + resStreamOut = new FileOutputStream(jarFolder + resourceName); + while ((readBytes = stream.read(buffer)) > 0) { + resStreamOut.write(buffer, 0, readBytes); + } + } catch (Exception ex) { + throw ex; + } finally { + stream.close(); + resStreamOut.close(); + } + + return jarFolder + resourceName; + } + + +} diff --git a/src/main/resources/configuration.json.example b/src/main/resources/configuration.json.example index d203e7f..ff93a17 100644 --- a/src/main/resources/configuration.json.example +++ b/src/main/resources/configuration.json.example @@ -1,3 +1,5 @@ { - "token": "NzYyMzrthMrthwMjQ2OTc1r0MjEz.X3nlXg.DtBrEKhXZ8nvV4rgego6c9VtEtUKw" + "token": "NzYyMzrthMrthwMjQ2OTc1r0MjEz.X3nlXg.DtBrEKhXZ8nvV4rgego6c9VtEtUKw", + "prefix": ";", + "course-selection-channel": ["730441682817908797", "732213443104604251"] } \ No newline at end of file From 5ec00a6493ebd0ed8c06b3647bbf94403146c368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=ADan=20Errity?= Date: Sun, 4 Oct 2020 19:04:41 +0100 Subject: [PATCH 6/6] First commands --- .../tcdbot/commands/InviteCommand.java | 18 ++++++++++++++++ .../listeners/MessageReceivedListener.java | 21 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/main/java/io/paradaux/tcdbot/commands/InviteCommand.java create mode 100644 src/main/java/io/paradaux/tcdbot/listeners/MessageReceivedListener.java diff --git a/src/main/java/io/paradaux/tcdbot/commands/InviteCommand.java b/src/main/java/io/paradaux/tcdbot/commands/InviteCommand.java new file mode 100644 index 0000000..f3c223d --- /dev/null +++ b/src/main/java/io/paradaux/tcdbot/commands/InviteCommand.java @@ -0,0 +1,18 @@ +package io.paradaux.tcdbot.commands; + +import com.jagrosh.jdautilities.command.Command; +import com.jagrosh.jdautilities.command.CommandEvent; + +public class InviteCommand extends Command { + + public InviteCommand(String name, String[] aliases, String help) { + this.name = "invite"; + this.aliases = new String[] {"inv", "i"}; + this.help = "Gives an invite link."; + } + + @Override + protected void execute(CommandEvent commandEvent) { + + } +} diff --git a/src/main/java/io/paradaux/tcdbot/listeners/MessageReceivedListener.java b/src/main/java/io/paradaux/tcdbot/listeners/MessageReceivedListener.java new file mode 100644 index 0000000..5cd3f41 --- /dev/null +++ b/src/main/java/io/paradaux/tcdbot/listeners/MessageReceivedListener.java @@ -0,0 +1,21 @@ +package io.paradaux.tcdbot.listeners; + +import io.paradaux.tcdbot.api.ConfigurationCache; +import net.dv8tion.jda.api.entities.MessageChannel; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import org.jetbrains.annotations.NotNull; + +public class MessageReceivedListener extends ListenerAdapter { + + ConfigurationCache configurationCache; + + public MessageReceivedListener(ConfigurationCache configurationCache) { + this.configurationCache = configurationCache; + } + + @Override + public void onMessageReceived(@NotNull MessageReceivedEvent event) { + MessageChannel channel = event.getMessage().getChannel(); + } +}