Skip to content

Commit

Permalink
Final complete
Browse files Browse the repository at this point in the history
  • Loading branch information
HMJosh committed Dec 7, 2020
1 parent 0b259ee commit 7abea0c
Show file tree
Hide file tree
Showing 29 changed files with 634 additions and 156 deletions.
3 changes: 2 additions & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>me.Joshb</groupId>
<artifactId>DiscordBotAPI</artifactId>
<version>1.0.3</version>
<version>1.0.0</version>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
Expand All @@ -12,6 +12,7 @@
<filtering>true</filtering>
<directory>${basedir}/src/main/resources/</directory>
<includes>
<include>bungee.yml</include>
<include>plugin.yml</include>
<include>Messages.yml</include>
<include>Config.yml</include>
Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.Joshb</groupId>
<artifactId>DiscordBotAPI</artifactId>
<version>1.0.0</version>
<version>1.0.4</version>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
Expand All @@ -16,6 +16,7 @@
<filtering>true</filtering>
<directory>${basedir}/src/main/resources/</directory>
<includes>
<include>bungee.yml</include>
<include>plugin.yml</include>
<include>Messages.yml</include>
<include>Config.yml</include>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.joshb.discordbotapi.server.assets;
package me.joshb.discordbotapi;

public enum Permission {

Expand Down
78 changes: 78 additions & 0 deletions src/main/java/me/joshb/discordbotapi/bungee/AccountManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package me.joshb.discordbotapi.bungee;


import me.joshb.discordbotapi.bungee.config.LinkedAccounts;
import net.md_5.bungee.config.Configuration;

import java.util.UUID;

public class AccountManager {

public AccountManager(){}
private static AccountManager instance = new AccountManager();
public static AccountManager getInstance(){
return instance;
}

public void setCode(UUID uuid, String code){
getConfig().set(uuid.toString() + ".Code", code);
LinkedAccounts.getInstance().save();
}

public String getCode(UUID uuid){
return getConfig().getString(uuid.toString() + ".Code");
}

public boolean matchCode(String code){
for(String uuid : getConfig().getKeys()){
if(getConfig().getString(uuid + ".Code").equals(code)) {
return true;
}
}
return false;
}

public void setDiscordID(String discordID, String code){
for(String uuid : getConfig().getKeys()){
if(getConfig().getString(uuid + ".Code").equals(code)) {
getConfig().set(uuid + ".Discord-ID", discordID);
LinkedAccounts.getInstance().save();
}
}
}

public String getDiscordID(UUID uuid){
String string = getConfig().getString(uuid.toString() + ".Discord-ID");
if(string == null || string.equalsIgnoreCase("")){
return null;
}
return string;
}

public UUID getUUID(String discordID){
for(String uuid : getConfig().getKeys()){
if(getConfig().getString(uuid + ".Discord-ID").equals(discordID)){
return UUID.fromString(uuid);
}
}
return null;
}

public void unlinkAccount(UUID uuid){
getConfig().set(uuid.toString(), null);
LinkedAccounts.getInstance().save();
}

public void unlinkAccount(String discordID){
for(String uuid : getConfig().getKeys()){
if(getConfig().getString(uuid + ".Discord-ID").equals(discordID)) {
getConfig().set(uuid, null);
LinkedAccounts.getInstance().save();
}
}
}

private Configuration getConfig(){
return LinkedAccounts.getInstance().getConfig();
}
}
25 changes: 22 additions & 3 deletions src/main/java/me/joshb/discordbotapi/bungee/DiscordBotAPI.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,48 @@
package me.joshb.discordbotapi.bungee;

import me.joshb.discordbotapi.bungee.config.Config;
import me.joshb.discordbotapi.bungee.config.LinkedAccounts;
import me.joshb.discordbotapi.bungee.config.Messages;
import me.joshb.discordbotapi.bungee.command.CommandManager;
import me.joshb.discordbotapi.bungee.listener.DiscordMessageReceived;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.plugin.Plugin;

import javax.security.auth.login.LoginException;

public class DiscordBotAPI extends Plugin {

public static JDA jda;
public JDA jda;

public static DiscordBotAPI plugin;

public void onEnable(){
plugin = this;

Config.getInstance().initialize();
LinkedAccounts.getInstance().initialize();
Messages.getInstance().initialize();

if(Config.getInstance().getConfig().getString("Bot.Token").equals("token_here")){
getLogger().severe("Plugin Disabled. The bot token is invalid.");
return;
}

JDABuilder builder = JDABuilder.createDefault(Config.getInstance().getConfig().getString("Bot.Token"));
CommandManager cm = new CommandManager();
cm.initializeSubCommands();
ProxyServer.getInstance().getPluginManager().registerCommand(this, cm);

JDABuilder builder = JDABuilder.create(Config.getInstance().getConfig().getString("Bot.Token"), GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_MESSAGE_REACTIONS);

try {
jda = builder.build();
} catch (LoginException e) {
getLogger().severe("Plugin Disabled. The bot token is invalid. Reason: " + e.getMessage());
}
jda.addEventListener(new DiscordMessageReceived());
}

public void onDisable(){
Expand All @@ -37,7 +51,12 @@ public void onDisable(){
}
}

public static JDA getJDA(){
public JDA getJDA(){
return jda;
}

public static AccountManager getAccountManager() {
return AccountManager.getInstance();
}

}
33 changes: 33 additions & 0 deletions src/main/java/me/joshb/discordbotapi/bungee/assets/Assets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package me.joshb.discordbotapi.bungee.assets;

import me.joshb.discordbotapi.bungee.config.Messages;
import net.dv8tion.jda.api.entities.User;
import net.md_5.bungee.api.ChatColor;

import java.util.ArrayList;
import java.util.List;

public class Assets {

public static String formatDiscordMessage(String location, User user){
return Messages.getInstance().getConfig().getString(location)
.replaceAll("%author_@%", user.getAsMention())
.replaceAll("%author_name%", user.getName())
.replaceAll("%author_id%", user.getId());
}

public static String format(String location){
return ChatColor.translateAlternateColorCodes('&',
Messages.getInstance().getConfig().getString(location).replaceAll("%prefix%",
Messages.getInstance().getConfig().getString("Game.Prefix")));
}

public static List<String> formatStringList(String location){
List<String> newList = new ArrayList<>();
for(String s : Messages.getInstance().getConfig().getStringList(location)){
newList.add(ChatColor.translateAlternateColorCodes('&', s));
}
return newList;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package me.joshb.discordbotapi.bungee.command;

import me.joshb.discordbotapi.Permission;
import me.joshb.discordbotapi.bungee.DiscordBotAPI;
import me.joshb.discordbotapi.bungee.assets.Assets;
import me.joshb.discordbotapi.bungee.config.Config;
import net.dv8tion.jda.api.entities.User;
import net.md_5.bungee.api.connection.ProxiedPlayer;

import java.util.List;
import java.util.Random;

public class CommandLink extends DiscordCommand {

private final String command = Config.getInstance().getConfig().getString("Bot.Command-Prefix");

@Override
public String command() {
return "link";
}

@Override
public void onCommand(ProxiedPlayer p, String[] args) {
if(!p.hasPermission(Permission.GAME_DISCORD_LINK.getValue())){
p.sendMessage(Assets.format("Game.Discord.Commands.No-Permission"));
return;
}
if(DiscordBotAPI.getAccountManager().getDiscordID(p.getUniqueId()) == null){
//Not linked
List<String> notLinked = Assets.formatStringList("Game.Commands.Discord.Sub-Commands.Link.Not-Linked");
String randomCode = code();
DiscordBotAPI.getAccountManager().setCode(p.getUniqueId(), randomCode);
for(String s : notLinked){
p.sendMessage(s
.replaceAll("%code%", randomCode)
.replaceAll("%player%", p.getName())
.replaceAll("%command_prefix%", command)
.replaceAll("%discord_bot_name%", DiscordBotAPI.plugin.getJDA().getSelfUser().getName()));
}
} else {
//Linked
User u = DiscordBotAPI.plugin.getJDA().getUserById(DiscordBotAPI.getAccountManager().getDiscordID(p.getUniqueId()));
List<String> linked = Assets.formatStringList("Game.Commands.Discord.Sub-Commands.Link.Linked");
for(String s : linked){
p.sendMessage(s
.replaceAll("%player%", p.getName())
.replaceAll("%discord_bot_name%", DiscordBotAPI.plugin.getJDA().getSelfUser().getName())
.replaceAll("%discord_author_name%", u.getName())
.replaceAll("%discord_author_discriminator%", u.getDiscriminator()));
}
}
}

private String code(){
Random r = new Random();
return String.format("%04d", r.nextInt(10000));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package me.joshb.discordbotapi.bungee.command;

import me.joshb.discordbotapi.Permission;
import me.joshb.discordbotapi.bungee.DiscordBotAPI;
import me.joshb.discordbotapi.bungee.assets.Assets;
import net.dv8tion.jda.api.entities.User;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CommandManager extends Command {

public CommandManager() { super("discord", Permission.GAME_DISCORD.getValue(), "");}

private List<DiscordCommand> commands = new ArrayList<>();

public void initializeSubCommands(){
commands.add(new CommandLink());
commands.add(new CommandUnlink());
}

public void execute(CommandSender sender, String[] args) {
if(!(sender instanceof ProxiedPlayer)){
sender.sendMessage(Assets.format("Game.Commands.Player-Only-Command"));
return;
}
ProxiedPlayer p = (ProxiedPlayer) sender;

if(args.length == 0){
//Not Linked
if(DiscordBotAPI.getAccountManager().getDiscordID(p.getUniqueId()) == null){
List<String> notLinked = Assets.formatStringList("Game.Commands.Discord.Not-Linked");
for(String s : notLinked){
p.sendMessage(s
.replaceAll("%player%", p.getName()));
}
//Linked
} else {
String discordID = DiscordBotAPI.getAccountManager().getDiscordID(p.getUniqueId());
User u = DiscordBotAPI.plugin.getJDA().getUserById(discordID);
List<String> linked = Assets.formatStringList("Game.Commands.Discord.Linked");
for(String s : linked){
p.sendMessage(s
.replaceAll("%player%", p.getName())
.replaceAll("%discord_bot_name%", DiscordBotAPI.plugin.getJDA().getSelfUser().getName())
.replaceAll("%discord_author_name%", u.getName())
.replaceAll("%discord_author_discriminator%", u.getDiscriminator()));
}
}
} else {
DiscordCommand cmd = get(args[0]);
if (!(cmd == null)) {
ArrayList<String> a = new ArrayList<>(Arrays.asList(args));
a.remove(0);
args = a.toArray(new String[a.size()]);
cmd.onCommand(p, args);
return;
}
}
}

private DiscordCommand get(String name) {
for (DiscordCommand cmd : commands) {
if (cmd.command().equalsIgnoreCase(name))
return cmd;
}
return null;
}
}
Loading

0 comments on commit 7abea0c

Please sign in to comment.