Fixes and probably added add cmd

This commit is contained in:
Norbi Peti 2017-01-14 13:32:33 +01:00
parent 519e0f468e
commit 2b9164ebc6
2 changed files with 60 additions and 24 deletions

View file

@ -4,6 +4,7 @@ import java.awt.Color;
import java.io.BufferedReader; 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;
@ -32,24 +33,18 @@ 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(); saveConfig();
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();
@ -114,6 +109,15 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
if (!sent) { if (!sent) {
sendMessageToChannel(chatchannel, "", new EmbedBuilder().withColor(Color.GREEN) sendMessageToChannel(chatchannel, "", new EmbedBuilder().withColor(Color.GREEN)
.withTitle("Server started - chat connected.").build()); .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; sent = true;
} }
}, 0, 10); }, 0, 10);
@ -126,17 +130,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
Bukkit.getPluginManager().registerEvents(new ExceptionListener(), this); Bukkit.getPluginManager().registerEvents(new ExceptionListener(), this);
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this); TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this);
TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class); TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class);
Runnable r = new Runnable() { new Thread(() -> AnnouncementGetterThreadMethod()).start();
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();
@ -155,6 +149,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());

View file

@ -1,7 +1,13 @@
package buttondevteam.discordplugin.commands; package buttondevteam.discordplugin.commands;
import java.util.List;
import java.util.stream.Collectors;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.TBMCCoreAPI;
import sx.blah.discord.handle.obj.IMessage; import sx.blah.discord.handle.obj.IMessage;
import sx.blah.discord.handle.obj.IRole;
import sx.blah.discord.util.RateLimitException;
public class RoleCommand extends DiscordCommandBase { public class RoleCommand extends DiscordCommandBase {
@ -24,6 +30,40 @@ public class RoleCommand extends DiscordCommandBase {
"This command adds a game role to your account.\nUsage: add <rolename>"); "This command adds a game role to your account.\nUsage: add <rolename>");
return; return;
} }
if (!DiscordPlugin.GameRoles.contains(argsa[1].toLowerCase())) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"That game role cannot be found.\nList of game roles:\n"
+ DiscordPlugin.GameRoles.stream().collect(Collectors.joining("\n")));
return;
}
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;
}
if (roles.size() > 1) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"There are more roles with this name. Why are there more roles with this name?");
return;
}
while (true) {
try {
message.getAuthor().addRole(roles.get(0));
break;
} catch (RateLimitException e) {
try {
Thread.sleep(e.getRetryDelay() > 0 ? e.getRetryDelay() : 10);
} catch (InterruptedException e1) {
}
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while adding role!", e);
DiscordPlugin.sendMessageToChannel(message.getChannel(), "An error occured while adding the role.");
break;
}
}
} else if (argsa[0].equalsIgnoreCase("remove")) { } else if (argsa[0].equalsIgnoreCase("remove")) {
if (argsa.length < 2) { if (argsa.length < 2) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), DiscordPlugin.sendMessageToChannel(message.getChannel(),
@ -31,7 +71,8 @@ public class RoleCommand extends DiscordCommandBase {
return; return;
} }
} else if (argsa[0].equalsIgnoreCase("list")) { } else if (argsa[0].equalsIgnoreCase("list")) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), "List of game roles:"); DiscordPlugin.sendMessageToChannel(message.getChannel(),
"List of game roles:\n" + DiscordPlugin.GameRoles.stream().collect(Collectors.joining("\n")));
} }
} }