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.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.gson.*;
@ -23,18 +20,15 @@ import buttondevteam.discordplugin.listeners.*;
import buttondevteam.discordplugin.mccommands.DiscordMCCommandBase;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.TBMCChatAPI;
import io.vertx.core.http.HttpServerOptions;
import net.milkbowl.vault.permission.Permission;
import sx.blah.discord.api.*;
import sx.blah.discord.api.events.IListener;
import sx.blah.discord.api.internal.json.objects.EmbedObject;
import sx.blah.discord.handle.impl.events.ReadyEvent;
import sx.blah.discord.handle.obj.*;
import sx.blah.discord.util.DiscordException;
import sx.blah.discord.util.EmbedBuilder;
import sx.blah.discord.util.MissingPermissionsException;
import sx.blah.discord.util.RateLimitException;
import sx.blah.discord.util.RequestBuffer;
import sx.blah.discord.util.*;
import sx.blah.discord.util.RequestBuffer.IVoidRequest;
import sx.blah.discord.util.RequestBuffer.RequestFuture;
public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
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();
dc.getDispatcher().registerListener(mcchat);
TBMCCoreAPI.RegisterEventsForExceptions(mcchat, this);
dc.getDispatcher().registerListener(new AutoUpdaterListener());
TBMCCoreAPI.RegisterEventsForExceptions(new AutoUpdaterListener(), this);
Bukkit.getPluginManager().registerEvents(new ExceptionListener(), this);
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this);
TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class);
TBMCCoreAPI.RegisterUserClass(DiscordPlayer.class);
new Thread(() -> AnnouncementGetterThreadMethod()).start();
new Thread(this::AnnouncementGetterThreadMethod).start();
setupProviders();
TBMCCoreAPI.SendUnsentExceptions();
TBMCCoreAPI.SendUnsentDebugMessages();
if (!TBMCCoreAPI.IsTestServer()) {
final Calendar currentCal = Calendar.getInstance();
final Calendar newCal = Calendar.getInstance();
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())),
"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"))
.withRedirectUrl("https://" + (TBMCCoreAPI.IsTestServer() ? "localhost" : "server.figytuna.com")
+ ":8081/callback")
@ -172,9 +168,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
rc.response().end("Redirecting");
rc.response().close();
}).build();
getLogger().info("Auth URL: " + doa.buildAuthUrl());
Void v = RequestBuffer.request(System.out::println).get(); // TODO: Remove
System.out.println(v);
getLogger().info("Auth URL: " + doa.buildAuthUrl());*/
} catch (Exception 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.
*/
public static void perform(DiscordRunnable action) throws DiscordException, MissingPermissionsException {
for (int i = 0; i < 20; i++)
try {
public static <T> RequestFuture<Void> perform(IVoidRequest action)
throws DiscordException, MissingPermissionsException {
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();
}
}
return null;
return RequestBuffer.request(action); // Let the pros handle this
}
}

View file

@ -1,65 +1,22 @@
package buttondevteam.discordplugin.listeners;
import java.awt.Color;
import java.util.function.Supplier;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.DiscordSender;
import buttondevteam.lib.PluginUpdater;
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> {
@Override
public void handle(MessageReceivedEvent event) {
public class AutoUpdaterListener implements Listener {
@EventHandler
public void handle(PluginUpdater.UpdatedEvent event) {
if (DiscordPlugin.SafeMode)
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 {
int hi, ei, prnum;
if ((hi = embed.getDescription().indexOf('#')) > -1
&& ((ei = embed.getDescription().indexOf(' ', hi + 1)) > -1
|| (ei = embed.getDescription().indexOf(".", hi + 1)) > -1
|| (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));
DiscordPlugin.perform(() -> DiscordPlugin.officechannel.getMessageHistory(10).stream()
.filter(m -> m.getWebhookLongID() == 239123781401051138L && m.getEmbeds().get(0).getTitle()
.contains(event.getData().get("repository").getAsJsonObject().get("name").getAsString()))
.findFirst().get().addReaction(DiscordPlugin.DELIVERED_REACTION));
} catch (Exception 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.DiscordPlugin;
import buttondevteam.discordplugin.commands.DiscordCommandBase;
import buttondevteam.lib.TBMCCoreAPI;
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.MessageReceivedEvent;
@ -73,7 +74,8 @@ public class CommandListener {
if (DiscordPlugin.SafeMode)
return;
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;
if (usableServerReadyStrings.size() == 0)
createUsableServerReadyStrings(usableServerReadyStrings);

View file

@ -1,5 +1,6 @@
package buttondevteam.discordplugin.listeners;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@ -33,7 +34,9 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
final String authorPlayer = DiscordPlugin.sanitizeString(e.getSender() instanceof Player //
? ((Player) e.getSender()).getDisplayName() //
: 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)
embed.withAuthorIcon("https://minotar.net/avatar/" + ((Player) e.getSender()).getName() + "/32.png");
final long nanoTime = System.nanoTime();