From 81a70f80f4d51b1999b1ef37c3b684d0f5bcf858 Mon Sep 17 00:00:00 2001 From: JoeShimo Date: Sun, 28 Jul 2019 22:11:04 +0100 Subject: [PATCH] This is a new commit --- .gitignore | 4 + README.md | 1 + plugin.yml | 11 ++ pom.xml | 123 ++++++++++++++++++ .../co/angrybee/joe/DiscordWhitelister.java | 120 +++++++++++++++++ .../co/angrybee/joe/ServerDiscordClient.java | 35 +++++ 6 files changed, 294 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 plugin.yml create mode 100644 pom.xml create mode 100644 src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java create mode 100644 src/main/java/uk/co/angrybee/joe/ServerDiscordClient.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..88ce45e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +out/ +target/ +.idea/ +discordwhitelister.iml diff --git a/README.md b/README.md new file mode 100644 index 0000000..80c6661 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# DiscordWhitelisterBot-Spigot diff --git a/plugin.yml b/plugin.yml new file mode 100644 index 0000000..9b4ea78 --- /dev/null +++ b/plugin.yml @@ -0,0 +1,11 @@ +name: DiscordWhitelister +version: 1.0.0 +author: Joe Shimell +main: uk.co.angrybee.joe.DiscordWhitelister +description: Discord whitelister bot. +api-version: 1.13 + +permissions: + discordbot: + description: discordbot permission. + default: true \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..6429a08 --- /dev/null +++ b/pom.xml @@ -0,0 +1,123 @@ + + + + 4.0.0 + + uk.co.angrybee.joe + discord-whitelister + 1.0-SNAPSHOT + + discord-whitelister + + http://www.example.com + + + UTF-8 + 1.7 + 1.7 + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + + false + + bintray-dv8fromtheworld-maven + bintray + https://dl.bintray.com/dv8fromtheworld/maven + + + + + + + junit + junit + 4.11 + test + + + org.spigotmc + spigot-api + 1.14.2-R0.1-SNAPSHOT + provided + + + net.dv8tion + JDA + 4.BETA.0_30 + + + club.minnced + opus-java + + + + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + org.apache.maven.plugins + maven-shade-plugin + + + package + + shade + + + + + uber-${project.artifactId}-${project.version} + + + + + + diff --git a/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java b/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java new file mode 100644 index 0000000..00a11f9 --- /dev/null +++ b/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java @@ -0,0 +1,120 @@ +package uk.co.angrybee.joe; + +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +public class DiscordWhitelister extends JavaPlugin +{ + + private File whitelisterBotConfigFile; + private FileConfiguration whitelisterBotConfig; + + private boolean configCreated = false; + + private String botToken; + private List allowedRoles; + private boolean botEnabled = true; + + @Override + public void onEnable() + { + ConfigSetup(); + + AssignVars(); + + if(!botEnabled) + { + getLogger().info("Bot is disabled as per the config, doing nothing"); + } + else if(configCreated) + { + getLogger().info("Config newly created, token will be in-valid, doing nothing until next server start"); + } + else + { + getLogger().info("Initializing Discord client"); + ServerDiscordClient serverDiscordClient = new ServerDiscordClient(); + serverDiscordClient.InitializeClient(botToken); + getLogger().info("Successfully initialized Discord client"); + } + } + + public FileConfiguration getWhitelisterBotConfig() + { + return this.whitelisterBotConfig; + } + + public void ConfigSetup() + { + String fileString = "discord-whitelister.yml"; + + whitelisterBotConfigFile = new File(getDataFolder(), fileString); + + if(!whitelisterBotConfigFile.exists()) + { + whitelisterBotConfigFile.getParentFile().mkdirs(); + + //saveResource(fileString, false); // from example, doesn't seem to work? + + try + { + whitelisterBotConfigFile.createNewFile(); + } + catch(IOException e) + { + e.printStackTrace(); + } + + getLogger().info("Configuration file created at " + whitelisterBotConfigFile.getPath() + + ", please edit this else the plugin will not work!"); + configCreated = true; + } + + whitelisterBotConfig = new YamlConfiguration(); + + try + { + getWhitelisterBotConfig().load(whitelisterBotConfigFile); + } + catch(IOException | InvalidConfigurationException e) + { + e.printStackTrace(); + } + + if(configCreated) + { + getWhitelisterBotConfig().set("discord-bot-token", "Discord bot token goes here, you can find it here: " + + "https://discordapp.com/developers/applications/"); + + List tempSetupRoles = Arrays.asList("Owner", "Admin", "Mod"); + getWhitelisterBotConfig().set("allowed-to-use-roles", tempSetupRoles); + + getWhitelisterBotConfig().set("bot-enabled", true); + + try + { + getWhitelisterBotConfig().save((whitelisterBotConfigFile.getPath())); + } + catch(IOException e) + { + e.printStackTrace(); + } + + } + } + + public void AssignVars() + { + botToken = getWhitelisterBotConfig().getString("discord-bot-token"); + allowedRoles = getWhitelisterBotConfig().getList("allowed-to-use-roles"); + botEnabled = getWhitelisterBotConfig().getBoolean("bot-enabled"); + } + +} diff --git a/src/main/java/uk/co/angrybee/joe/ServerDiscordClient.java b/src/main/java/uk/co/angrybee/joe/ServerDiscordClient.java new file mode 100644 index 0000000..81d069f --- /dev/null +++ b/src/main/java/uk/co/angrybee/joe/ServerDiscordClient.java @@ -0,0 +1,35 @@ +package uk.co.angrybee.joe; + +import net.dv8tion.jda.api.AccountType; +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; + +import javax.security.auth.login.LoginException; + +// handles Discord interaction +public class ServerDiscordClient extends ListenerAdapter +{ + public void InitializeClient(String clientToken) + { + try + { + JDA jda = new JDABuilder(AccountType.BOT) + .setToken(clientToken) + .build(); + } + catch(LoginException e) + { + e.printStackTrace(); + } + } + + public void onMessageRecieved(MessageReceivedEvent messageReceivedEvent) + { + // context + User author = messageReceivedEvent.getAuthor(); + String messageContents = messageReceivedEvent.getMessage().getContentDisplay(); + } +}