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.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
@ -32,24 +33,18 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
public static IDiscordClient dc;
public static DiscordPlugin plugin;
public static boolean SafeMode = true;
public static List<String> GameRoles;
@SuppressWarnings("unchecked")
@Override
public void onEnable() {
try {
Bukkit.getLogger().info("Initializing DiscordPlugin...");
plugin = this;
final File file = new File("TBMC", "DiscordRedditLastAnnouncement.txt");
if (file.exists()) {
BufferedReader reader = Files.newReader(file, StandardCharsets.UTF_8);
String line = reader.readLine();
lastannouncementtime = Long.parseLong(line);
reader.close();
file.delete();
} else {
lastannouncementtime = getConfig().getLong("lastannouncementtime");
lastseentime = getConfig().getLong("lastseentime");
saveConfig();
}
lastannouncementtime = getConfig().getLong("lastannouncementtime");
lastseentime = getConfig().getLong("lastseentime");
GameRoles = (List<String>) getConfig().getList("gameroles", new ArrayList<String>());
saveConfig();
ClientBuilder cb = new ClientBuilder();
cb.withToken(Files.readFirstLine(new File("TBMC", "Token.txt"), StandardCharsets.UTF_8));
dc = cb.login();
@ -114,6 +109,15 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
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);
@ -126,17 +130,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
Bukkit.getPluginManager().registerEvents(new ExceptionListener(), this);
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this);
TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class);
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));
}
new Thread(() -> AnnouncementGetterThreadMethod()).start();
setupProviders();
TBMCCoreAPI.SendUnsentExceptions();
TBMCCoreAPI.SendUnsentDebugMessages();
@ -155,6 +149,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
stop = true;
getConfig().set("lastannouncementtime", lastannouncementtime);
getConfig().set("lastseentime", lastseentime);
getConfig().set("gameroles", GameRoles);
saveConfig();
sendMessageToChannel(chatchannel, "", new EmbedBuilder().withColor(Restart ? Color.ORANGE : Color.RED)
.withTitle(Restart ? "Server restarting" : "Server stopping").build());

View file

@ -1,7 +1,13 @@
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;
import sx.blah.discord.util.RateLimitException;
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>");
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")) {
if (argsa.length < 2) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
@ -31,7 +71,8 @@ public class RoleCommand extends DiscordCommandBase {
return;
}
} 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")));
}
}