Merge pull request #26 from TBMCPlugins/dev
Fixes, added game roles, commands in mcchat, smaller additions
This commit is contained in:
commit
17c98b6edd
15 changed files with 265 additions and 125 deletions
|
@ -11,11 +11,8 @@ public class DiscordPlayer extends ChromaGamerBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDiscordID() {
|
public String getDiscordID() {
|
||||||
return did;
|
if (did == null)
|
||||||
}
|
did = plugindata.getString(getFolder() + "_id");
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getFileName() {
|
|
||||||
return did;
|
return did;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package buttondevteam.discordplugin;
|
package buttondevteam.discordplugin;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||||
|
@ -23,7 +23,9 @@ import sx.blah.discord.api.events.IListener;
|
||||||
import sx.blah.discord.api.internal.json.objects.EmbedObject;
|
import sx.blah.discord.api.internal.json.objects.EmbedObject;
|
||||||
import sx.blah.discord.handle.impl.events.ReadyEvent;
|
import sx.blah.discord.handle.impl.events.ReadyEvent;
|
||||||
import sx.blah.discord.handle.obj.*;
|
import sx.blah.discord.handle.obj.*;
|
||||||
|
import sx.blah.discord.util.DiscordException;
|
||||||
import sx.blah.discord.util.EmbedBuilder;
|
import sx.blah.discord.util.EmbedBuilder;
|
||||||
|
import sx.blah.discord.util.MissingPermissionsException;
|
||||||
import sx.blah.discord.util.RateLimitException;
|
import sx.blah.discord.util.RateLimitException;
|
||||||
|
|
||||||
public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
|
@ -32,24 +34,17 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
public static IDiscordClient dc;
|
public static IDiscordClient dc;
|
||||||
public static DiscordPlugin plugin;
|
public static DiscordPlugin plugin;
|
||||||
public static boolean SafeMode = true;
|
public static boolean SafeMode = true;
|
||||||
|
public static List<String> GameRoles;
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
try {
|
try {
|
||||||
Bukkit.getLogger().info("Initializing DiscordPlugin...");
|
Bukkit.getLogger().info("Initializing DiscordPlugin...");
|
||||||
plugin = this;
|
plugin = this;
|
||||||
final File file = new File("TBMC", "DiscordRedditLastAnnouncement.txt");
|
lastannouncementtime = getConfig().getLong("lastannouncementtime");
|
||||||
if (file.exists()) {
|
lastseentime = getConfig().getLong("lastseentime");
|
||||||
BufferedReader reader = Files.newReader(file, StandardCharsets.UTF_8);
|
GameRoles = (List<String>) getConfig().getList("gameroles", new ArrayList<String>());
|
||||||
String line = reader.readLine();
|
|
||||||
lastannouncementtime = Long.parseLong(line);
|
|
||||||
reader.close();
|
|
||||||
file.delete();
|
|
||||||
} else {
|
|
||||||
lastannouncementtime = getConfig().getLong("lastannouncementtime");
|
|
||||||
lastseentime = getConfig().getLong("lastseentime");
|
|
||||||
saveConfig();
|
|
||||||
}
|
|
||||||
ClientBuilder cb = new ClientBuilder();
|
ClientBuilder cb = new ClientBuilder();
|
||||||
cb.withToken(Files.readFirstLine(new File("TBMC", "Token.txt"), StandardCharsets.UTF_8));
|
cb.withToken(Files.readFirstLine(new File("TBMC", "Token.txt"), StandardCharsets.UTF_8));
|
||||||
dc = cb.login();
|
dc = cb.login();
|
||||||
|
@ -74,6 +69,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
public static IGuild devServer;
|
public static IGuild devServer;
|
||||||
|
|
||||||
private static volatile BukkitTask task;
|
private static volatile BukkitTask task;
|
||||||
|
private static volatile boolean sent = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(ReadyEvent event) {
|
public void handle(ReadyEvent event) {
|
||||||
|
@ -110,6 +106,20 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
SafeMode = false;
|
SafeMode = false;
|
||||||
if (task != null)
|
if (task != null)
|
||||||
task.cancel();
|
task.cancel();
|
||||||
|
if (!sent) {
|
||||||
|
sendMessageToChannel(chatchannel, "", new EmbedBuilder().withColor(Color.GREEN)
|
||||||
|
.withTitle("Server started - chat connected.").build());
|
||||||
|
try {
|
||||||
|
List<IMessage> msgs = genchannel.getPinnedMessages();
|
||||||
|
for (int i = msgs.size() - 1; i >= 10; i--) { // Unpin all pinned messages except the newest 10
|
||||||
|
genchannel.unpin(msgs.get(i));
|
||||||
|
Thread.sleep(10);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
TBMCCoreAPI.SendException("Error occured while unpinning messages!", e);
|
||||||
|
}
|
||||||
|
sent = true;
|
||||||
|
}
|
||||||
}, 0, 10);
|
}, 0, 10);
|
||||||
for (IListener<?> listener : CommandListener.getListeners())
|
for (IListener<?> listener : CommandListener.getListeners())
|
||||||
dc.getDispatcher().registerListener(listener);
|
dc.getDispatcher().registerListener(listener);
|
||||||
|
@ -121,20 +131,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this);
|
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this);
|
||||||
TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class);
|
TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class);
|
||||||
TBMCCoreAPI.RegisterUserClass(DiscordPlayer.class);
|
TBMCCoreAPI.RegisterUserClass(DiscordPlayer.class);
|
||||||
|
new Thread(() -> AnnouncementGetterThreadMethod()).start();
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(this, () -> sendMessageToChannel(chatchannel, "",
|
|
||||||
new EmbedBuilder().withColor(Color.GREEN).withTitle("Server started - chat connected.").build()));
|
|
||||||
Runnable r = new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
AnnouncementGetterThreadMethod();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Thread t = new Thread(r);
|
|
||||||
t.start();
|
|
||||||
List<IMessage> msgs = genchannel.getPinnedMessages();
|
|
||||||
for (int i = msgs.size() - 1; i >= 10; i--) { // Unpin all pinned messages except the newest 10
|
|
||||||
genchannel.unpin(msgs.get(i));
|
|
||||||
}
|
|
||||||
setupProviders();
|
setupProviders();
|
||||||
TBMCCoreAPI.SendUnsentExceptions();
|
TBMCCoreAPI.SendUnsentExceptions();
|
||||||
TBMCCoreAPI.SendUnsentDebugMessages();
|
TBMCCoreAPI.SendUnsentDebugMessages();
|
||||||
|
@ -153,6 +150,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
stop = true;
|
stop = true;
|
||||||
getConfig().set("lastannouncementtime", lastannouncementtime);
|
getConfig().set("lastannouncementtime", lastannouncementtime);
|
||||||
getConfig().set("lastseentime", lastseentime);
|
getConfig().set("lastseentime", lastseentime);
|
||||||
|
getConfig().set("gameroles", GameRoles);
|
||||||
saveConfig();
|
saveConfig();
|
||||||
sendMessageToChannel(chatchannel, "", new EmbedBuilder().withColor(Restart ? Color.ORANGE : Color.RED)
|
sendMessageToChannel(chatchannel, "", new EmbedBuilder().withColor(Restart ? Color.ORANGE : Color.RED)
|
||||||
.withTitle(Restart ? "Server restarting" : "Server stopping").build());
|
.withTitle(Restart ? "Server restarting" : "Server stopping").build());
|
||||||
|
@ -233,36 +231,18 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
Bukkit.getLogger()
|
Bukkit.getLogger()
|
||||||
.warning("Message was too long to send to discord and got truncated. In " + channel.getName());
|
.warning("Message was too long to send to discord and got truncated. In " + channel.getName());
|
||||||
}
|
}
|
||||||
for (int i = 0; i < 10; i++) {
|
try {
|
||||||
try {
|
if (channel == chatchannel)
|
||||||
Thread.sleep(i * 100);
|
MCChatListener.resetLastMessage(); // If this is a chat message, it'll be set again
|
||||||
} catch (InterruptedException e2) {
|
final String content = TBMCCoreAPI.IsTestServer() && channel != chatchannel
|
||||||
e2.printStackTrace();
|
? "*The following message is from a test server*\n" + message : message;
|
||||||
}
|
return perform(
|
||||||
try {
|
() -> embed == null ? channel.sendMessage(content) : channel.sendMessage(content, embed, false));
|
||||||
if (SafeMode)
|
} catch (Exception e) {
|
||||||
return null;
|
Bukkit.getLogger().warning(
|
||||||
if (channel == chatchannel)
|
"Failed to deliver message to Discord! Channel: " + channel.getName() + " Message: " + message);
|
||||||
MCChatListener.resetLastMessage(); // If this is a chat message, it'll be set again
|
throw new RuntimeException(e);
|
||||||
final String content = TBMCCoreAPI.IsTestServer() && channel != chatchannel
|
|
||||||
? "*The following message is from a test server*\n" + message : message;
|
|
||||||
return embed == null ? channel.sendMessage(content) : channel.sendMessage(content, embed, false);
|
|
||||||
} catch (RateLimitException e) {
|
|
||||||
try {
|
|
||||||
Thread.sleep(e.getRetryDelay());
|
|
||||||
} catch (InterruptedException e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
if (i == 9) {
|
|
||||||
Bukkit.getLogger().warning("Failed to deliver message to Discord! Channel: " + channel.getName()
|
|
||||||
+ " Message: " + message);
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} else
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Permission perms;
|
public static Permission perms;
|
||||||
|
@ -299,4 +279,43 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
}
|
}
|
||||||
return sanitizedString;
|
return sanitizedString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs Discord actions, retrying when ratelimited. May return null if action fails too many times or in safe mode.
|
||||||
|
*/
|
||||||
|
public static <T extends IDiscordObject<T>> T perform(DiscordSupplier<T> action)
|
||||||
|
throws DiscordException, MissingPermissionsException {
|
||||||
|
for (int i = 0; i < 20; i++)
|
||||||
|
try {
|
||||||
|
if (SafeMode)
|
||||||
|
return null;
|
||||||
|
return action.get();
|
||||||
|
} catch (RateLimitException e) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(e.getRetryDelay() > 0 ? e.getRetryDelay() : 10);
|
||||||
|
} catch (InterruptedException e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs Discord actions, retrying when ratelimited.
|
||||||
|
*/
|
||||||
|
public static void perform(DiscordRunnable action) throws DiscordException, MissingPermissionsException {
|
||||||
|
for (int i = 0; i < 20; i++)
|
||||||
|
try {
|
||||||
|
if (SafeMode)
|
||||||
|
return;
|
||||||
|
action.run();
|
||||||
|
return; // Gotta escape that loop
|
||||||
|
} catch (RateLimitException e) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(e.getRetryDelay() > 0 ? e.getRetryDelay() : 10);
|
||||||
|
} catch (InterruptedException e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
package buttondevteam.discordplugin;
|
||||||
|
|
||||||
|
import sx.blah.discord.util.DiscordException;
|
||||||
|
import sx.blah.discord.util.MissingPermissionsException;
|
||||||
|
import sx.blah.discord.util.RateLimitException;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface DiscordRunnable {
|
||||||
|
public abstract void run() throws DiscordException, RateLimitException, MissingPermissionsException;
|
||||||
|
}
|
|
@ -13,10 +13,17 @@ import sx.blah.discord.handle.obj.IUser;
|
||||||
public class DiscordSender extends DiscordSenderBase implements CommandSender {
|
public class DiscordSender extends DiscordSenderBase implements CommandSender {
|
||||||
private PermissibleBase perm = new PermissibleBase(this);
|
private PermissibleBase perm = new PermissibleBase(this);
|
||||||
|
|
||||||
|
private String name = null;
|
||||||
|
|
||||||
public DiscordSender(IUser user, IChannel channel) {
|
public DiscordSender(IUser user, IChannel channel) {
|
||||||
super(user, channel);
|
super(user, channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DiscordSender(IUser user, IChannel channel, String name) {
|
||||||
|
super(user, channel);
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isPermissionSet(String name) {
|
public boolean isPermissionSet(String name) {
|
||||||
return perm.isPermissionSet(name);
|
return perm.isPermissionSet(name);
|
||||||
|
@ -92,7 +99,7 @@ public class DiscordSender extends DiscordSenderBase implements CommandSender {
|
||||||
public String getName() {
|
public String getName() {
|
||||||
if (user == null)
|
if (user == null)
|
||||||
return "Discord user";
|
return "Discord user";
|
||||||
return user.getDisplayName(DiscordPlugin.mainServer);
|
return name == null ? user.getDisplayName(DiscordPlugin.mainServer) : name;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
package buttondevteam.discordplugin;
|
||||||
|
|
||||||
|
import sx.blah.discord.handle.obj.IDiscordObject;
|
||||||
|
import sx.blah.discord.util.DiscordException;
|
||||||
|
import sx.blah.discord.util.MissingPermissionsException;
|
||||||
|
import sx.blah.discord.util.RateLimitException;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface DiscordSupplier<T extends IDiscordObject<T>> {
|
||||||
|
public abstract T get() throws DiscordException, RateLimitException, MissingPermissionsException;
|
||||||
|
}
|
|
@ -9,6 +9,7 @@ import com.google.common.collect.HashBiMap;
|
||||||
import buttondevteam.discordplugin.DiscordPlayer;
|
import buttondevteam.discordplugin.DiscordPlayer;
|
||||||
import buttondevteam.discordplugin.DiscordPlugin;
|
import buttondevteam.discordplugin.DiscordPlugin;
|
||||||
import buttondevteam.lib.TBMCCoreAPI;
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
|
import buttondevteam.lib.player.ChromaGamerBase;
|
||||||
import buttondevteam.lib.player.TBMCPlayer;
|
import buttondevteam.lib.player.TBMCPlayer;
|
||||||
import buttondevteam.lib.player.TBMCPlayerBase;
|
import buttondevteam.lib.player.TBMCPlayerBase;
|
||||||
import sx.blah.discord.handle.obj.IMessage;
|
import sx.blah.discord.handle.obj.IMessage;
|
||||||
|
|
|
@ -10,7 +10,7 @@ public abstract class DiscordCommandBase {
|
||||||
public abstract String getCommandName();
|
public abstract String getCommandName();
|
||||||
|
|
||||||
public abstract void run(IMessage message, String args);
|
public abstract void run(IMessage message, String args);
|
||||||
|
|
||||||
public abstract String[] getHelpText();
|
public abstract String[] getHelpText();
|
||||||
|
|
||||||
static final HashMap<String, DiscordCommandBase> commands = new HashMap<String, DiscordCommandBase>();
|
static final HashMap<String, DiscordCommandBase> commands = new HashMap<String, DiscordCommandBase>();
|
||||||
|
@ -19,6 +19,7 @@ public abstract class DiscordCommandBase {
|
||||||
commands.put("connect", new ConnectCommand()); // TODO: API for adding commands?
|
commands.put("connect", new ConnectCommand()); // TODO: API for adding commands?
|
||||||
commands.put("userinfo", new UserinfoCommand());
|
commands.put("userinfo", new UserinfoCommand());
|
||||||
commands.put("help", new HelpCommand());
|
commands.put("help", new HelpCommand());
|
||||||
|
commands.put("role", new RoleCommand());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void runCommand(String cmd, String args, IMessage message) {
|
public static void runCommand(String cmd, String args, IMessage message) {
|
||||||
|
|
|
@ -0,0 +1,103 @@
|
||||||
|
package buttondevteam.discordplugin.commands;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import buttondevteam.discordplugin.DiscordPlugin;
|
||||||
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
|
import sx.blah.discord.handle.obj.IMessage;
|
||||||
|
import sx.blah.discord.handle.obj.IRole;
|
||||||
|
|
||||||
|
public class RoleCommand extends DiscordCommandBase {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getCommandName() {
|
||||||
|
return "role";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(IMessage message, String args) {
|
||||||
|
final String usagemsg = "Subcommands: add, remove, list";
|
||||||
|
if (args.length() == 0) {
|
||||||
|
DiscordPlugin.sendMessageToChannel(message.getChannel(), usagemsg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String[] argsa = args.split(" ");
|
||||||
|
if (argsa[0].equalsIgnoreCase("add")) {
|
||||||
|
final IRole role = checkAndGetRole(message, argsa, "This command adds a game role to your account.");
|
||||||
|
if (role == null)
|
||||||
|
return;
|
||||||
|
try {
|
||||||
|
DiscordPlugin.perform(() -> message.getAuthor().addRole(role));
|
||||||
|
DiscordPlugin.sendMessageToChannel(message.getChannel(), "Added game role.");
|
||||||
|
} catch (Exception e) {
|
||||||
|
TBMCCoreAPI.SendException("Error while adding role!", e);
|
||||||
|
DiscordPlugin.sendMessageToChannel(message.getChannel(), "An error occured while adding the role.");
|
||||||
|
}
|
||||||
|
} else if (argsa[0].equalsIgnoreCase("remove")) {
|
||||||
|
final IRole role = checkAndGetRole(message, argsa, "This command removes a game role from your account.");
|
||||||
|
if (role == null)
|
||||||
|
return;
|
||||||
|
try {
|
||||||
|
DiscordPlugin.perform(() -> message.getAuthor().removeRole(role));
|
||||||
|
DiscordPlugin.sendMessageToChannel(message.getChannel(), "Removed game role.");
|
||||||
|
} catch (Exception e) {
|
||||||
|
TBMCCoreAPI.SendException("Error while removing role!", e);
|
||||||
|
DiscordPlugin.sendMessageToChannel(message.getChannel(), "An error occured while removing the role.");
|
||||||
|
}
|
||||||
|
} else if (argsa[0].equalsIgnoreCase("list")) {
|
||||||
|
DiscordPlugin.sendMessageToChannel(message.getChannel(),
|
||||||
|
"List of game roles:\n" + DiscordPlugin.GameRoles.stream().collect(Collectors.joining("\n")));
|
||||||
|
} else if (argsa[0].equalsIgnoreCase("addrole")) {
|
||||||
|
final List<IRole> roles = (TBMCCoreAPI.IsTestServer() ? DiscordPlugin.devServer : DiscordPlugin.mainServer)
|
||||||
|
.getRolesByName(argsa[1]);
|
||||||
|
if (roles.size() == 0) {
|
||||||
|
DiscordPlugin.sendMessageToChannel(message.getChannel(), "That role cannot be found on Discord.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (roles.size() > 1) {
|
||||||
|
DiscordPlugin.sendMessageToChannel(message.getChannel(),
|
||||||
|
"There are more roles with this name. Why are there more roles with this name?");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
DiscordPlugin.GameRoles.add(roles.get(0).getName());
|
||||||
|
DiscordPlugin.sendMessageToChannel(message.getChannel(), "Game role added.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private IRole checkAndGetRole(IMessage message, String[] argsa, String usage) {
|
||||||
|
if (argsa.length < 2) {
|
||||||
|
DiscordPlugin.sendMessageToChannel(message.getChannel(), usage + "\nUsage: " + argsa[0] + " <rolename>");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (!DiscordPlugin.GameRoles.contains(argsa[1])) {
|
||||||
|
DiscordPlugin.sendMessageToChannel(message.getChannel(),
|
||||||
|
"That game role cannot be found.\nList of game roles:\n"
|
||||||
|
+ DiscordPlugin.GameRoles.stream().collect(Collectors.joining("\n")));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
final List<IRole> roles = (TBMCCoreAPI.IsTestServer() ? DiscordPlugin.devServer : DiscordPlugin.mainServer)
|
||||||
|
.getRolesByName(argsa[1]);
|
||||||
|
if (roles.size() == 0) {
|
||||||
|
DiscordPlugin.sendMessageToChannel(message.getChannel(),
|
||||||
|
"The specified role cannot be found on Discord! Removing from the list.");
|
||||||
|
DiscordPlugin.GameRoles.remove(argsa[1].toLowerCase());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (roles.size() > 1) {
|
||||||
|
DiscordPlugin.sendMessageToChannel(message.getChannel(),
|
||||||
|
"There are more roles with this name. Why are there more roles with this name?");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return roles.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String[] getHelpText() {
|
||||||
|
return new String[] { //
|
||||||
|
"Add or remove game roles from yourself.", //
|
||||||
|
"Usage: role add|remove <name> or role list", //
|
||||||
|
"Mods can use role addrole <name> to add a role as a game role" };
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -31,7 +31,6 @@ public class UserinfoCommand extends DiscordCommandBase {
|
||||||
if (args.length() == 0)
|
if (args.length() == 0)
|
||||||
target = message.getAuthor();
|
target = message.getAuthor();
|
||||||
else {
|
else {
|
||||||
|
|
||||||
final Optional<IUser> firstmention = message.getMentions().stream()
|
final Optional<IUser> firstmention = message.getMentions().stream()
|
||||||
.filter(m -> !m.getID().equals(DiscordPlugin.dc.getOurUser().getID())).findFirst();
|
.filter(m -> !m.getID().equals(DiscordPlugin.dc.getOurUser().getID())).findFirst();
|
||||||
if (firstmention.isPresent())
|
if (firstmention.isPresent())
|
||||||
|
@ -77,7 +76,7 @@ public class UserinfoCommand extends DiscordCommandBase {
|
||||||
DiscordPlugin.sendMessageToChannel(message.getChannel(), uinfo.toString());
|
DiscordPlugin.sendMessageToChannel(message.getChannel(), uinfo.toString());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
DiscordPlugin.sendMessageToChannel(message.getChannel(), "An error occured while getting the user!");
|
DiscordPlugin.sendMessageToChannel(message.getChannel(), "An error occured while getting the user!");
|
||||||
TBMCCoreAPI.SendException("Error while getting DiscordPlayer info!", e);
|
TBMCCoreAPI.SendException("Error while getting info about " + target.getName() + "!", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,6 @@ import sx.blah.discord.api.events.IListener;
|
||||||
import sx.blah.discord.handle.impl.events.MessageReceivedEvent;
|
import sx.blah.discord.handle.impl.events.MessageReceivedEvent;
|
||||||
import sx.blah.discord.handle.obj.IEmbed;
|
import sx.blah.discord.handle.obj.IEmbed;
|
||||||
import sx.blah.discord.util.EmbedBuilder;
|
import sx.blah.discord.util.EmbedBuilder;
|
||||||
import sx.blah.discord.util.RateLimitException;
|
|
||||||
|
|
||||||
public class AutoUpdaterListener implements IListener<MessageReceivedEvent> {
|
public class AutoUpdaterListener implements IListener<MessageReceivedEvent> {
|
||||||
@Override
|
@Override
|
||||||
|
@ -34,7 +33,7 @@ public class AutoUpdaterListener implements IListener<MessageReceivedEvent> {
|
||||||
new DiscordSender(null,
|
new DiscordSender(null,
|
||||||
TBMCCoreAPI.IsTestServer() //
|
TBMCCoreAPI.IsTestServer() //
|
||||||
? DiscordPlugin.chatchannel //
|
? DiscordPlugin.chatchannel //
|
||||||
: DiscordPlugin.updatechannel),
|
: DiscordPlugin.botroomchannel),
|
||||||
branch)
|
branch)
|
||||||
&& ((Supplier<Boolean>) () -> { // Best looking code I've ever written
|
&& ((Supplier<Boolean>) () -> { // Best looking code I've ever written
|
||||||
try {
|
try {
|
||||||
|
@ -57,18 +56,10 @@ public class AutoUpdaterListener implements IListener<MessageReceivedEvent> {
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}).get() && (!TBMCCoreAPI.IsTestServer() || !branch.equals("master")))
|
}).get() && (!TBMCCoreAPI.IsTestServer() || !branch.equals("master")))
|
||||||
while (true)
|
try {
|
||||||
try {
|
DiscordPlugin.perform(() -> event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION));
|
||||||
event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION);
|
} catch (Exception e) {
|
||||||
break;
|
TBMCCoreAPI.SendException("An error occured while reacting to plugin update!", e);
|
||||||
} catch (RateLimitException e) {
|
}
|
||||||
try {
|
|
||||||
if (e.getRetryDelay() > 0)
|
|
||||||
Thread.sleep(e.getRetryDelay());
|
|
||||||
} catch (InterruptedException ie) {
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
TBMCCoreAPI.SendException("An error occured while reacting to plugin update!", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,8 @@ public class CommandListener {
|
||||||
final IChannel channel = event.getMessage().getChannel();
|
final IChannel channel = event.getMessage().getChannel();
|
||||||
if (!channel.getID().equals(DiscordPlugin.botchannel.getID()) && !channel.isPrivate())
|
if (!channel.getID().equals(DiscordPlugin.botchannel.getID()) && !channel.isPrivate())
|
||||||
return;
|
return;
|
||||||
|
if (channel.getID().equals(DiscordPlugin.chatchannel.getID()))
|
||||||
|
return; // The chat code already handles this - Right now while testing botchannel is the same as chatchannel
|
||||||
runCommand(event.getMessage(), true);
|
runCommand(event.getMessage(), true);
|
||||||
}
|
}
|
||||||
}, new IListener<MessageReceivedEvent>() {
|
}, new IListener<MessageReceivedEvent>() {
|
||||||
|
|
|
@ -10,7 +10,9 @@ import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
|
|
||||||
import buttondevteam.discordplugin.DiscordPlugin;
|
import buttondevteam.discordplugin.DiscordPlugin;
|
||||||
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
import buttondevteam.lib.TBMCExceptionEvent;
|
import buttondevteam.lib.TBMCExceptionEvent;
|
||||||
|
import sx.blah.discord.handle.obj.IRole;
|
||||||
|
|
||||||
public class ExceptionListener implements Listener {
|
public class ExceptionListener implements Listener {
|
||||||
private List<Throwable> lastthrown = new ArrayList<>();
|
private List<Throwable> lastthrown = new ArrayList<>();
|
||||||
|
@ -35,9 +37,14 @@ public class ExceptionListener implements Listener {
|
||||||
e.setHandled();
|
e.setHandled();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static IRole coderRole;
|
||||||
|
|
||||||
private static void SendException(Throwable e, String sourcemessage) {
|
private static void SendException(Throwable e, String sourcemessage) {
|
||||||
try {
|
try {
|
||||||
StringBuilder sb = new StringBuilder();
|
if (coderRole == null)
|
||||||
|
coderRole = DiscordPlugin.devServer.getRolesByName("Coder").get(0);
|
||||||
|
StringBuilder sb = TBMCCoreAPI.IsTestServer() ? new StringBuilder()
|
||||||
|
: new StringBuilder(coderRole.mention()).append("\n");
|
||||||
sb.append(sourcemessage).append("\n");
|
sb.append(sourcemessage).append("\n");
|
||||||
sb.append("```").append("\n");
|
sb.append("```").append("\n");
|
||||||
String stackTrace = Arrays.stream(ExceptionUtils.getStackTrace(e).split("\\n"))
|
String stackTrace = Arrays.stream(ExceptionUtils.getStackTrace(e).split("\\n"))
|
||||||
|
|
|
@ -13,6 +13,7 @@ import buttondevteam.discordplugin.*;
|
||||||
import buttondevteam.lib.*;
|
import buttondevteam.lib.*;
|
||||||
import buttondevteam.lib.chat.Channel;
|
import buttondevteam.lib.chat.Channel;
|
||||||
import buttondevteam.lib.chat.TBMCChatAPI;
|
import buttondevteam.lib.chat.TBMCChatAPI;
|
||||||
|
import buttondevteam.lib.player.ChromaGamerBase;
|
||||||
import buttondevteam.lib.player.TBMCPlayer;
|
import buttondevteam.lib.player.TBMCPlayer;
|
||||||
import sx.blah.discord.api.events.IListener;
|
import sx.blah.discord.api.events.IListener;
|
||||||
import sx.blah.discord.api.internal.json.objects.EmbedObject;
|
import sx.blah.discord.api.internal.json.objects.EmbedObject;
|
||||||
|
@ -46,8 +47,8 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
try {
|
try {
|
||||||
embedObject.description = lastmessage.getEmbedded().get(0).getDescription() + "\n"
|
embedObject.description = lastmessage.getEmbedded().get(0).getDescription() + "\n"
|
||||||
+ embedObject.description;
|
+ embedObject.description;
|
||||||
lastmessage.edit("", embedObject);
|
DiscordPlugin.perform(() -> lastmessage.edit("", embedObject));
|
||||||
} catch (MissingPermissionsException | RateLimitException | DiscordException e1) {
|
} catch (MissingPermissionsException | DiscordException e1) {
|
||||||
TBMCCoreAPI.SendException("An error occured while editing chat message!", e1);
|
TBMCCoreAPI.SendException("An error occured while editing chat message!", e1);
|
||||||
}
|
}
|
||||||
} // TODO: Author URL
|
} // TODO: Author URL
|
||||||
|
@ -83,20 +84,21 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
return;
|
return;
|
||||||
String dmessage = event.getMessage().getContent();
|
String dmessage = event.getMessage().getContent();
|
||||||
try {
|
try {
|
||||||
Optional<? extends Player> player = Bukkit.getOnlinePlayers().stream().filter(p -> { // TODO: Support offline players
|
DiscordPlayer dp = ChromaGamerBase.getUser(author.getID(), DiscordPlayer.class);
|
||||||
DiscordPlayer dp = TBMCPlayer.getPlayer(p.getUniqueId(), TBMCPlayer.class).getAs(DiscordPlayer.class); // Not changing any data, don't need to save
|
|
||||||
return dp != null && author.getID().equals(dp.getDiscordID());
|
|
||||||
}).findAny();
|
|
||||||
final DiscordSenderBase dsender;
|
final DiscordSenderBase dsender;
|
||||||
if (player.isPresent()) // Connected?
|
Player mcp = null; // Offline players can't really run commands
|
||||||
{ // Execute as ingame player
|
final String cid;
|
||||||
|
if ((cid = dp.getConnectedID(TBMCPlayer.class)) != null // Connected?
|
||||||
|
&& (mcp = Bukkit.getPlayer(cid)) != null) { // Execute as ingame player, if online
|
||||||
if (!ConnectedSenders.containsKey(author.getID()))
|
if (!ConnectedSenders.containsKey(author.getID()))
|
||||||
ConnectedSenders.put(author.getID(),
|
ConnectedSenders.put(author.getID(),
|
||||||
new DiscordPlayerSender(author, event.getMessage().getChannel(), player.get()));
|
new DiscordPlayerSender(author, event.getMessage().getChannel(), mcp));
|
||||||
dsender = ConnectedSenders.get(author.getID());
|
dsender = ConnectedSenders.get(author.getID());
|
||||||
} else {
|
} else {
|
||||||
|
TBMCPlayer p = dp.getAs(TBMCPlayer.class);
|
||||||
if (!UnconnectedSenders.containsKey(author.getID()))
|
if (!UnconnectedSenders.containsKey(author.getID()))
|
||||||
UnconnectedSenders.put(author.getID(), new DiscordSender(author, event.getMessage().getChannel()));
|
UnconnectedSenders.put(author.getID(), new DiscordSender(author, event.getMessage().getChannel(),
|
||||||
|
p == null ? null : p.PlayerName().get())); // Display the playername, if found
|
||||||
dsender = UnconnectedSenders.get(author.getID());
|
dsender = UnconnectedSenders.get(author.getID());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,7 +110,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
|
|
||||||
if (dmessage.startsWith("/")) {
|
if (dmessage.startsWith("/")) {
|
||||||
final String cmd = dmessage.substring(1).toLowerCase();
|
final String cmd = dmessage.substring(1).toLowerCase();
|
||||||
if (!player.isPresent()
|
if (mcp == null
|
||||||
&& !Arrays.stream(UnconnectedCmds).anyMatch(s -> cmd.equals(s) || cmd.startsWith(s + " "))) {
|
&& !Arrays.stream(UnconnectedCmds).anyMatch(s -> cmd.equals(s) || cmd.startsWith(s + " "))) {
|
||||||
// Command not whitelisted
|
// Command not whitelisted
|
||||||
DiscordPlugin.sendMessageToChannel(event.getMessage().getChannel(), // TODO
|
DiscordPlugin.sendMessageToChannel(event.getMessage().getChannel(), // TODO
|
||||||
|
@ -130,39 +132,24 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
} else
|
} else
|
||||||
Bukkit.dispatchCommand(dsender, cmd);
|
Bukkit.dispatchCommand(dsender, cmd);
|
||||||
lastlistp = (short) Bukkit.getOnlinePlayers().size();
|
lastlistp = (short) Bukkit.getOnlinePlayers().size();
|
||||||
} else
|
if (!event.getMessage().isDeleted())
|
||||||
|
event.getMessage().delete();
|
||||||
|
} else {
|
||||||
TBMCChatAPI.SendChatMessage(Channel.GlobalChat, dsender,
|
TBMCChatAPI.SendChatMessage(Channel.GlobalChat, dsender,
|
||||||
dmessage + (event.getMessage().getAttachments().size() > 0 ? "\n" + event.getMessage()
|
dmessage + (event.getMessage().getAttachments().size() > 0 ? "\n" + event.getMessage()
|
||||||
.getAttachments().stream().map(a -> a.getUrl()).collect(Collectors.joining("\n"))
|
.getAttachments().stream().map(a -> a.getUrl()).collect(Collectors.joining("\n"))
|
||||||
: ""));
|
: ""));
|
||||||
event.getMessage().getChannel().getMessages().stream().forEach(m -> {
|
event.getMessage().getChannel().getMessages().stream().forEach(m -> {
|
||||||
try {
|
try {
|
||||||
final IReaction reaction = m.getReactionByName(DiscordPlugin.DELIVERED_REACTION);
|
final IReaction reaction = m.getReactionByName(DiscordPlugin.DELIVERED_REACTION);
|
||||||
if (reaction != null) {
|
if (reaction != null)
|
||||||
while (true)
|
DiscordPlugin.perform(() -> m.removeReaction(reaction));
|
||||||
try {
|
} catch (Exception e) {
|
||||||
m.removeReaction(reaction);
|
TBMCCoreAPI.SendException("An error occured while removing reactions from chat!", e);
|
||||||
Thread.sleep(100);
|
|
||||||
break;
|
|
||||||
} catch (RateLimitException e) {
|
|
||||||
if (e.getRetryDelay() > 0)
|
|
||||||
Thread.sleep(e.getRetryDelay());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
});
|
||||||
TBMCCoreAPI.SendException("An error occured while removing reactions from chat!", e);
|
DiscordPlugin.perform(() -> event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION));
|
||||||
}
|
}
|
||||||
});
|
|
||||||
while (true)
|
|
||||||
try {
|
|
||||||
event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION);
|
|
||||||
break;
|
|
||||||
} catch (RateLimitException e) {
|
|
||||||
if (e.getRetryDelay() > 0)
|
|
||||||
Thread.sleep(e.getRetryDelay());
|
|
||||||
else
|
|
||||||
Thread.sleep(100);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
TBMCCoreAPI.SendException("An error occured while handling message \"" + dmessage + "\"!", e);
|
TBMCCoreAPI.SendException("An error occured while handling message \"" + dmessage + "\"!", e);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -11,10 +11,8 @@ import org.bukkit.event.server.ServerCommandEvent;
|
||||||
import buttondevteam.discordplugin.DiscordPlayer;
|
import buttondevteam.discordplugin.DiscordPlayer;
|
||||||
import buttondevteam.discordplugin.DiscordPlugin;
|
import buttondevteam.discordplugin.DiscordPlugin;
|
||||||
import buttondevteam.discordplugin.commands.ConnectCommand;
|
import buttondevteam.discordplugin.commands.ConnectCommand;
|
||||||
import buttondevteam.lib.player.TBMCPlayerGetInfoEvent;
|
import buttondevteam.lib.player.*;
|
||||||
import buttondevteam.lib.player.TBMCPlayerJoinEvent;
|
import net.ess3.api.events.*;
|
||||||
import buttondevteam.lib.player.TBMCPlayerQuitEvent;
|
|
||||||
import net.ess3.api.events.AfkStatusChangeEvent;
|
|
||||||
import sx.blah.discord.handle.obj.IUser;
|
import sx.blah.discord.handle.obj.IUser;
|
||||||
import sx.blah.discord.handle.obj.Status.StatusType;
|
import sx.blah.discord.handle.obj.Status.StatusType;
|
||||||
|
|
||||||
|
@ -45,6 +43,9 @@ public class MCListener implements Listener {
|
||||||
if (DiscordPlugin.SafeMode)
|
if (DiscordPlugin.SafeMode)
|
||||||
return;
|
return;
|
||||||
DiscordPlayer dp = e.getPlayer().getAs(DiscordPlayer.class);
|
DiscordPlayer dp = e.getPlayer().getAs(DiscordPlayer.class);
|
||||||
|
/*System.out.println("dp: " + dp);
|
||||||
|
if (dp != null)
|
||||||
|
System.out.println("dp.did: " + dp.getDiscordID());*/
|
||||||
if (dp == null || dp.getDiscordID() == null || dp.getDiscordID() == "")
|
if (dp == null || dp.getDiscordID() == null || dp.getDiscordID() == "")
|
||||||
return;
|
return;
|
||||||
IUser user = DiscordPlugin.dc.getUserByID(dp.getDiscordID());
|
IUser user = DiscordPlugin.dc.getUserByID(dp.getDiscordID());
|
||||||
|
@ -74,4 +75,8 @@ public class MCListener implements Listener {
|
||||||
public void onServerCommand(ServerCommandEvent e) {
|
public void onServerCommand(ServerCommandEvent e) {
|
||||||
DiscordPlugin.Restart = !e.getCommand().equalsIgnoreCase("stop"); // The variable is always true except if stopped
|
DiscordPlugin.Restart = !e.getCommand().equalsIgnoreCase("stop"); // The variable is always true except if stopped
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @EventHandler public void onPlayerMute(MuteStatusChangeEvent e) { e.getAffected() }
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,7 +46,7 @@ public class AcceptMCCommand extends DiscordMCCommandBase {
|
||||||
}
|
}
|
||||||
DiscordPlayer dp = ChromaGamerBase.getUser(did, DiscordPlayer.class);
|
DiscordPlayer dp = ChromaGamerBase.getUser(did, DiscordPlayer.class);
|
||||||
TBMCPlayer mcp = TBMCPlayerBase.getPlayer(((Player) sender).getUniqueId(), TBMCPlayer.class);
|
TBMCPlayer mcp = TBMCPlayerBase.getPlayer(((Player) sender).getUniqueId(), TBMCPlayer.class);
|
||||||
dp.connectWith(mcp); // TODO: Fix null.yml and empty playerinfos
|
dp.connectWith(mcp);
|
||||||
dp.save();
|
dp.save();
|
||||||
mcp.save();
|
mcp.save();
|
||||||
ConnectCommand.WaitingToConnect.remove(sender.getName());
|
ConnectCommand.WaitingToConnect.remove(sender.getName());
|
||||||
|
|
Loading…
Reference in a new issue