Updated autouppdater, colorized chat & test check

Also using RequestBuffer...
This commit is contained in:
Norbi Peti 2017-07-01 01:31:31 +02:00
parent 6dd21b03c1
commit 92e7e6abc4
4 changed files with 42 additions and 95 deletions

View file

@ -13,9 +13,6 @@ import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import com.github.xaanit.d4j.oauth.Scope;
import com.github.xaanit.d4j.oauth.handle.IDiscordOAuth;
import com.github.xaanit.d4j.oauth.util.DiscordOAuthBuilder;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.google.gson.*; import com.google.gson.*;
@ -23,18 +20,15 @@ import buttondevteam.discordplugin.listeners.*;
import buttondevteam.discordplugin.mccommands.DiscordMCCommandBase; import buttondevteam.discordplugin.mccommands.DiscordMCCommandBase;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.chat.TBMCChatAPI;
import io.vertx.core.http.HttpServerOptions;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
import sx.blah.discord.api.*; import sx.blah.discord.api.*;
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;
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.*;
import sx.blah.discord.util.EmbedBuilder; import sx.blah.discord.util.RequestBuffer.IVoidRequest;
import sx.blah.discord.util.MissingPermissionsException; import sx.blah.discord.util.RequestBuffer.RequestFuture;
import sx.blah.discord.util.RateLimitException;
import sx.blah.discord.util.RequestBuffer;
public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> { public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
private static final String SubredditURL = "https://www.reddit.com/r/ChromaGamers"; private static final String SubredditURL = "https://www.reddit.com/r/ChromaGamers";
@ -134,15 +128,16 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
MCChatListener mcchat = new MCChatListener(); MCChatListener mcchat = new MCChatListener();
dc.getDispatcher().registerListener(mcchat); dc.getDispatcher().registerListener(mcchat);
TBMCCoreAPI.RegisterEventsForExceptions(mcchat, this); TBMCCoreAPI.RegisterEventsForExceptions(mcchat, this);
dc.getDispatcher().registerListener(new AutoUpdaterListener()); TBMCCoreAPI.RegisterEventsForExceptions(new AutoUpdaterListener(), this);
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);
TBMCCoreAPI.RegisterUserClass(DiscordPlayer.class); TBMCCoreAPI.RegisterUserClass(DiscordPlayer.class);
new Thread(() -> AnnouncementGetterThreadMethod()).start(); new Thread(this::AnnouncementGetterThreadMethod).start();
setupProviders(); setupProviders();
TBMCCoreAPI.SendUnsentExceptions(); TBMCCoreAPI.SendUnsentExceptions();
TBMCCoreAPI.SendUnsentDebugMessages(); TBMCCoreAPI.SendUnsentDebugMessages();
if (!TBMCCoreAPI.IsTestServer()) {
final Calendar currentCal = Calendar.getInstance(); final Calendar currentCal = Calendar.getInstance();
final Calendar newCal = Calendar.getInstance(); final Calendar newCal = Calendar.getInstance();
currentCal.set(currentCal.get(Calendar.YEAR), currentCal.get(Calendar.MONTH), currentCal.set(currentCal.get(Calendar.YEAR), currentCal.get(Calendar.MONTH),
@ -152,7 +147,8 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
sendMessageToChannel(dc.getChannels().get(rand.nextInt(dc.getChannels().size())), sendMessageToChannel(dc.getChannels().get(rand.nextInt(dc.getChannels().size())),
"You could make a religion out of this"); "You could make a religion out of this");
} }
IDiscordOAuth doa = new DiscordOAuthBuilder(dc).withClientID("226443037893591041") }
/*IDiscordOAuth doa = new DiscordOAuthBuilder(dc).withClientID("226443037893591041")
.withClientSecret(getConfig().getString("appsecret")) .withClientSecret(getConfig().getString("appsecret"))
.withRedirectUrl("https://" + (TBMCCoreAPI.IsTestServer() ? "localhost" : "server.figytuna.com") .withRedirectUrl("https://" + (TBMCCoreAPI.IsTestServer() ? "localhost" : "server.figytuna.com")
+ ":8081/callback") + ":8081/callback")
@ -172,9 +168,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
rc.response().end("Redirecting"); rc.response().end("Redirecting");
rc.response().close(); rc.response().close();
}).build(); }).build();
getLogger().info("Auth URL: " + doa.buildAuthUrl()); getLogger().info("Auth URL: " + doa.buildAuthUrl());*/
Void v = RequestBuffer.request(System.out::println).get(); // TODO: Remove
System.out.println(v);
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while enabling DiscordPlugin!", e); TBMCCoreAPI.SendException("An error occured while enabling DiscordPlugin!", e);
} }
@ -343,19 +337,10 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
/** /**
* Performs Discord actions, retrying when ratelimited. * Performs Discord actions, retrying when ratelimited.
*/ */
public static void perform(DiscordRunnable action) throws DiscordException, MissingPermissionsException { public static <T> RequestFuture<Void> perform(IVoidRequest action)
for (int i = 0; i < 20; i++) throws DiscordException, MissingPermissionsException {
try {
if (SafeMode) if (SafeMode)
return; return null;
action.run(); return RequestBuffer.request(action); // Let the pros handle this
return; // Gotta escape that loop
} catch (RateLimitException e) {
try {
Thread.sleep(e.getRetryDelay() > 0 ? e.getRetryDelay() : 10);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
} }
} }

View file

@ -1,65 +1,22 @@
package buttondevteam.discordplugin.listeners; package buttondevteam.discordplugin.listeners;
import java.awt.Color; import org.bukkit.event.EventHandler;
import java.util.function.Supplier; import org.bukkit.event.Listener;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.DiscordSender;
import buttondevteam.lib.PluginUpdater; import buttondevteam.lib.PluginUpdater;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import sx.blah.discord.api.events.IListener;
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
import sx.blah.discord.handle.obj.IEmbed;
import sx.blah.discord.util.EmbedBuilder;
public class AutoUpdaterListener implements IListener<MessageReceivedEvent> { public class AutoUpdaterListener implements Listener {
@Override @EventHandler
public void handle(MessageReceivedEvent event) { public void handle(PluginUpdater.UpdatedEvent event) {
if (DiscordPlugin.SafeMode) if (DiscordPlugin.SafeMode)
return; return;
if (!event.getMessage().getChannel().getStringID().equals(DiscordPlugin.officechannel.getStringID()))
return;
if (239123781401051138L != event.getMessage().getWebhookLongID())
return;
if (event.getMessage().getEmbeds().size() == 0)
return;
final IEmbed embed = event.getMessage().getEmbeds().get(0);
final String title = embed.getTitle();
if (!title.contains("new commit"))
return;
String branch = title.substring(title.indexOf(':') + 1, title.indexOf(']'));
String project = title.substring(title.indexOf('[') + 1, title.indexOf(':'));
if ((branch.equals("master") || (TBMCCoreAPI.IsTestServer() && branch.equals("dev")))
&& PluginUpdater.isMaven(project, branch)
&& TBMCCoreAPI.UpdatePlugin(project,
new DiscordSender(null,
TBMCCoreAPI.IsTestServer() //
? DiscordPlugin.chatchannel //
: DiscordPlugin.botroomchannel),
branch)
&& ((Supplier<Boolean>) () -> {
try { try {
int hi, ei, prnum; DiscordPlugin.perform(() -> DiscordPlugin.officechannel.getMessageHistory(10).stream()
if ((hi = embed.getDescription().indexOf('#')) > -1 .filter(m -> m.getWebhookLongID() == 239123781401051138L && m.getEmbeds().get(0).getTitle()
&& ((ei = embed.getDescription().indexOf(' ', hi + 1)) > -1 .contains(event.getData().get("repository").getAsJsonObject().get("name").getAsString()))
|| (ei = embed.getDescription().indexOf(".", hi + 1)) > -1 .findFirst().get().addReaction(DiscordPlugin.DELIVERED_REACTION));
|| (ei = embed.getDescription().length()) > -1)
&& (prnum = Integer.parseInt(embed.getDescription().substring(hi + 1, ei))) > -1)
DiscordPlugin.sendMessageToChannel(DiscordPlugin.updatechannel, "",
new EmbedBuilder().withColor(Color.WHITE).withTitle("Update details")
.withUrl("https://github.com/TBMCPlugins/" + project + "/pull/" + prnum)
.build());
else
throw new Exception("No PR found");
} catch (Exception e) {
DiscordPlugin.sendMessageToChannel(DiscordPlugin.updatechannel, "",
new EmbedBuilder().withColor(Color.WHITE).withTitle("Update details:")
.withDescription(embed.getDescription() + " (" + e.getMessage() + ")").build());
}
return true;
}).get() && (!TBMCCoreAPI.IsTestServer() || !branch.equals("master")))
try {
DiscordPlugin.perform(() -> event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION));
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while reacting to plugin update!", e); TBMCCoreAPI.SendException("An error occured while reacting to plugin update!", e);
} }

View file

@ -7,6 +7,7 @@ import java.util.Random;
import buttondevteam.discordplugin.DiscordPlayer; import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.commands.DiscordCommandBase; import buttondevteam.discordplugin.commands.DiscordCommandBase;
import buttondevteam.lib.TBMCCoreAPI;
import sx.blah.discord.api.events.IListener; import sx.blah.discord.api.events.IListener;
import sx.blah.discord.handle.impl.events.guild.channel.message.MentionEvent; import sx.blah.discord.handle.impl.events.guild.channel.message.MentionEvent;
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent; import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
@ -73,7 +74,8 @@ public class CommandListener {
if (DiscordPlugin.SafeMode) if (DiscordPlugin.SafeMode)
return; return;
final String msglowercase = event.getMessage().getContent().toLowerCase(); final String msglowercase = event.getMessage().getContent().toLowerCase();
if (Arrays.stream(serverReadyQuestions).anyMatch(s -> msglowercase.contains(s))) { if (!TBMCCoreAPI.IsTestServer()
&& Arrays.stream(serverReadyQuestions).anyMatch(s -> msglowercase.contains(s))) {
int next; int next;
if (usableServerReadyStrings.size() == 0) if (usableServerReadyStrings.size() == 0)
createUsableServerReadyStrings(usableServerReadyStrings); createUsableServerReadyStrings(usableServerReadyStrings);

View file

@ -1,5 +1,6 @@
package buttondevteam.discordplugin.listeners; package buttondevteam.discordplugin.listeners;
import java.awt.Color;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -33,7 +34,9 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
final String authorPlayer = DiscordPlugin.sanitizeString(e.getSender() instanceof Player // final String authorPlayer = DiscordPlugin.sanitizeString(e.getSender() instanceof Player //
? ((Player) e.getSender()).getDisplayName() // ? ((Player) e.getSender()).getDisplayName() //
: e.getSender().getName()); : e.getSender().getName());
final EmbedBuilder embed = new EmbedBuilder().withAuthorName(authorPlayer).withDescription(e.getMessage()); final EmbedBuilder embed = new EmbedBuilder().withAuthorName(authorPlayer).withDescription(e.getMessage())
.withColor(new Color(e.getChannel().color.getRed(), e.getChannel().color.getGreen(),
e.getChannel().color.getBlue()));
if (e.getSender() instanceof Player) if (e.getSender() instanceof Player)
embed.withAuthorIcon("https://minotar.net/avatar/" + ((Player) e.getSender()).getName() + "/32.png"); embed.withAuthorIcon("https://minotar.net/avatar/" + ((Player) e.getSender()).getName() + "/32.png");
final long nanoTime = System.nanoTime(); final long nanoTime = System.nanoTime();