This is a new commit

This commit is contained in:
JoeShimo 2019-07-28 22:11:04 +01:00
commit 81a70f80f4
6 changed files with 294 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
out/
target/
.idea/
discordwhitelister.iml

1
README.md Normal file
View File

@ -0,0 +1 @@
# DiscordWhitelisterBot-Spigot

11
plugin.yml Normal file
View File

@ -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

123
pom.xml Normal file
View File

@ -0,0 +1,123 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>uk.co.angrybee.joe</groupId>
<artifactId>discord-whitelister</artifactId>
<version>1.0-SNAPSHOT</version>
<name>discord-whitelister</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>bintray-dv8fromtheworld-maven</id>
<name>bintray</name>
<url>https://dl.bintray.com/dv8fromtheworld/maven</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.14.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.BETA.0_30</version>
<exclusions>
<exclusion>
<groupId>club.minnced</groupId>
<artifactId>opus-java</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>uber-${project.artifactId}-${project.version}</finalName>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -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<String> 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");
}
}

View File

@ -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();
}
}