-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
634 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...scordbotapi/server/assets/Permission.java → ...va/me/joshb/discordbotapi/Permission.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
|
78 changes: 78 additions & 0 deletions
78
src/main/java/me/joshb/discordbotapi/bungee/AccountManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/me/joshb/discordbotapi/bungee/assets/Assets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/me/joshb/discordbotapi/bungee/command/CommandLink.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/me/joshb/discordbotapi/bungee/command/CommandManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.