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;
import java.awt.Color;
import java.util.function.Predicate;
import java.util.function.Supplier;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.DiscordSender;
import buttondevteam.lib.TBMCCoreAPI;
import sx.blah.discord.api.events.IListener;
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;
public class AutoUpdaterListener implements IListener<MessageReceivedEvent> {
@ -16,7 +22,8 @@ public class AutoUpdaterListener implements IListener<MessageReceivedEvent> {
return;
if (event.getMessage().getEmbedded().size() == 0)
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"))
return;
String branch = title.substring(title.indexOf(':') + 1, title.indexOf(']'));
@ -28,12 +35,40 @@ public class AutoUpdaterListener implements IListener<MessageReceivedEvent> {
? DiscordPlugin.chatchannel //
: DiscordPlugin.updatechannel),
branch)
&& (!TBMCCoreAPI.IsTestServer() || !branch.equals("master")))
try {
event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION);
} catch (RateLimitException e) { // TODO: Handle
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while reacting to plugin update!", e);
}
&& ((Supplier<Boolean>) () -> { // Best looking code I've ever written
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 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);
}
}
}