Added PR linking - in the condition

This commit is contained in:
Norbi Peti 2016-12-31 03:08:20 +01:00
parent 6cdac8696d
commit 7aeff53967

View file

@ -1,10 +1,16 @@
package buttondevteam.discordplugin.listeners; package buttondevteam.discordplugin.listeners;
import java.awt.Color;
import java.util.function.Predicate;
import java.util.function.Supplier;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.DiscordSender; import buttondevteam.discordplugin.DiscordSender;
import buttondevteam.lib.TBMCCoreAPI; 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.MessageReceivedEvent; import sx.blah.discord.handle.impl.events.MessageReceivedEvent;
import sx.blah.discord.handle.obj.IEmbed;
import sx.blah.discord.util.EmbedBuilder;
import sx.blah.discord.util.RateLimitException; import sx.blah.discord.util.RateLimitException;
public class AutoUpdaterListener implements IListener<MessageReceivedEvent> { public class AutoUpdaterListener implements IListener<MessageReceivedEvent> {
@ -16,7 +22,8 @@ public class AutoUpdaterListener implements IListener<MessageReceivedEvent> {
return; return;
if (event.getMessage().getEmbedded().size() == 0) if (event.getMessage().getEmbedded().size() == 0)
return; return;
final String title = event.getMessage().getEmbedded().get(0).getTitle(); final IEmbed embed = event.getMessage().getEmbedded().get(0);
final String title = embed.getTitle();
if (!title.contains("new commit")) if (!title.contains("new commit"))
return; return;
String branch = title.substring(title.indexOf(':') + 1, title.indexOf(']')); String branch = title.substring(title.indexOf(':') + 1, title.indexOf(']'));
@ -28,12 +35,40 @@ public class AutoUpdaterListener implements IListener<MessageReceivedEvent> {
? DiscordPlugin.chatchannel // ? DiscordPlugin.chatchannel //
: DiscordPlugin.updatechannel), : DiscordPlugin.updatechannel),
branch) branch)
&& (!TBMCCoreAPI.IsTestServer() || !branch.equals("master"))) && ((Supplier<Boolean>) () -> { // Best looking code I've ever written
try { try {
event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION); int hi, ei, prnum;
} catch (RateLimitException e) { // TODO: Handle if ((hi = embed.getDescription().indexOf('#')) > -1
} catch (Exception e) { && ((ei = embed.getDescription().indexOf(' ', hi + 1)) > -1
TBMCCoreAPI.SendException("An error occured while reacting to plugin update!", e); || (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 detauls: " + embed.getDescription() + " (" + e.getMessage() + ")")
.build());
}
return true;
}).get() && (!TBMCCoreAPI.IsTestServer() || !branch.equals("master")))
while (true)
try {
event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION);
break;
} 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);
}
} }
} }