Merge pull request #23 from TBMCPlugins/dev
Started using embeds for a nicer chat
This commit is contained in:
commit
bf33c55205
4 changed files with 78 additions and 27 deletions
|
@ -1,5 +1,6 @@
|
|||
package buttondevteam.discordplugin;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
@ -18,8 +19,10 @@ import buttondevteam.lib.chat.TBMCChatAPI;
|
|||
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.EmbedBuilder;
|
||||
import sx.blah.discord.util.RateLimitException;
|
||||
|
||||
/**
|
||||
|
@ -53,15 +56,6 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
|||
cb.withToken(Files.readFirstLine(new File("TBMC", "Token.txt"), StandardCharsets.UTF_8));
|
||||
dc = cb.login();
|
||||
dc.getDispatcher().registerListener(this);
|
||||
for (IListener<?> listener : CommandListener.getListeners())
|
||||
dc.getDispatcher().registerListener(listener);
|
||||
MCChatListener mcchat = new MCChatListener();
|
||||
dc.getDispatcher().registerListener(mcchat);
|
||||
TBMCCoreAPI.RegisterEventsForExceptions(mcchat, this);
|
||||
dc.getDispatcher().registerListener(new AutoUpdaterListener());
|
||||
Bukkit.getPluginManager().registerEvents(new ExceptionListener(), this);
|
||||
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this);
|
||||
TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Bukkit.getPluginManager().disablePlugin(this);
|
||||
|
@ -75,6 +69,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
|||
public static IChannel issuechannel;
|
||||
public static IChannel botroomchannel;
|
||||
public static IChannel officechannel;
|
||||
public static IChannel coffeechannel;
|
||||
public static IGuild mainServer;
|
||||
public static IGuild devServer;
|
||||
|
||||
|
@ -95,6 +90,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
|||
issuechannel = devServer.getChannelByID("219643416496046081"); // server-issues
|
||||
botroomchannel = devServer.getChannelByID("239519012529111040"); // bot-room
|
||||
officechannel = devServer.getChannelByID("219626707458457603"); // developers-office
|
||||
coffeechannel = devServer.getChannelByID("219530035365675010"); // coffee-table
|
||||
dc.changeStatus(Status.game("on TBMC"));
|
||||
} else {
|
||||
botchannel = devServer.getChannelByID("239519012529111040"); // bot-room
|
||||
|
@ -103,11 +99,23 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
|||
botroomchannel = botchannel;// bot-room
|
||||
issuechannel = botchannel; // bot-room
|
||||
chatchannel = devServer.getChannelByID("248185455508455424"); // minecraft_chat_test
|
||||
officechannel = devServer.getChannelByID("219626707458457603"); // developers-office
|
||||
officechannel = botchannel; // bot-room
|
||||
coffeechannel = botchannel; // bot-room
|
||||
dc.changeStatus(Status.game("testing"));
|
||||
}
|
||||
Bukkit.getScheduler().runTaskAsynchronously(this,
|
||||
() -> sendMessageToChannel(chatchannel, "Server started - chat connected."));
|
||||
|
||||
for (IListener<?> listener : CommandListener.getListeners())
|
||||
dc.getDispatcher().registerListener(listener);
|
||||
MCChatListener mcchat = new MCChatListener();
|
||||
dc.getDispatcher().registerListener(mcchat);
|
||||
TBMCCoreAPI.RegisterEventsForExceptions(mcchat, this);
|
||||
dc.getDispatcher().registerListener(new AutoUpdaterListener());
|
||||
Bukkit.getPluginManager().registerEvents(new ExceptionListener(), this);
|
||||
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this);
|
||||
TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class);
|
||||
|
||||
Bukkit.getScheduler().runTaskAsynchronously(this, () -> sendMessageToChannel(chatchannel, "",
|
||||
new EmbedBuilder().withColor(Color.GREEN).withTitle("Server started - chat connected.").build()));
|
||||
Runnable r = new Runnable() {
|
||||
public void run() {
|
||||
AnnouncementGetterThreadMethod();
|
||||
|
@ -127,13 +135,19 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Always true, except when running "stop" from console
|
||||
*/
|
||||
public static boolean Restart;
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
stop = true;
|
||||
getConfig().set("lastannouncementtime", lastannouncementtime);
|
||||
getConfig().set("lastseentime", lastseentime);
|
||||
saveConfig();
|
||||
sendMessageToChannel(chatchannel, "Server restarting/stopping");
|
||||
sendMessageToChannel(chatchannel, "", new EmbedBuilder().withColor(Restart ? Color.ORANGE : Color.RED)
|
||||
.withTitle(Restart ? "Server restarting" : "Server stopping").build());
|
||||
try {
|
||||
dc.changeStatus(Status.game("on TBMC"));
|
||||
dc.logout();
|
||||
|
@ -198,6 +212,10 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
|||
}
|
||||
|
||||
public static IMessage sendMessageToChannel(IChannel channel, String message) {
|
||||
return sendMessageToChannel(channel, message, null);
|
||||
}
|
||||
|
||||
public static IMessage sendMessageToChannel(IChannel channel, String message, EmbedObject embed) {
|
||||
if (message.length() > 1900) {
|
||||
message = message.substring(0, 1900);
|
||||
Bukkit.getLogger()
|
||||
|
@ -210,8 +228,9 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
|||
e2.printStackTrace();
|
||||
}
|
||||
try {
|
||||
return channel.sendMessage(TBMCCoreAPI.IsTestServer() && channel != chatchannel
|
||||
? "*The following message is from a test server*\n" + message : message);
|
||||
final String content = TBMCCoreAPI.IsTestServer() && channel != chatchannel
|
||||
? "*The following message is from a test server*\n" + message : message;
|
||||
return embed == null ? channel.sendMessage(content) : channel.sendMessage(content, embed, false);
|
||||
} catch (RateLimitException e) {
|
||||
try {
|
||||
Thread.sleep(e.getRetryDelay());
|
||||
|
|
|
@ -22,7 +22,7 @@ public class AutoUpdaterListener implements IListener<MessageReceivedEvent> {
|
|||
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"))
|
||||
&& TBMCCoreAPI.UpdatePlugin(project, new DiscordSender(null, DiscordPlugin.chatchannel), branch)
|
||||
&& TBMCCoreAPI.UpdatePlugin(project, new DiscordSender(null, TBMCCoreAPI.IsTestServer()?DiscordPlugin.chatchannel:DiscordPlugin.coffeechannel), branch)
|
||||
&& (!TBMCCoreAPI.IsTestServer() || !branch.equals("master")))
|
||||
try {
|
||||
event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION);
|
||||
|
|
|
@ -10,16 +10,14 @@ import org.bukkit.event.EventHandler;
|
|||
import org.bukkit.event.Listener;
|
||||
|
||||
import buttondevteam.discordplugin.*;
|
||||
import buttondevteam.lib.TBMCChatEvent;
|
||||
import buttondevteam.lib.TBMCCoreAPI;
|
||||
import buttondevteam.lib.TBMCPlayer;
|
||||
import buttondevteam.lib.*;
|
||||
import buttondevteam.lib.chat.Channel;
|
||||
import buttondevteam.lib.chat.TBMCChatAPI;
|
||||
import sx.blah.discord.api.events.IListener;
|
||||
import sx.blah.discord.api.internal.json.objects.EmbedObject;
|
||||
import sx.blah.discord.handle.impl.events.MessageReceivedEvent;
|
||||
import sx.blah.discord.handle.obj.IReaction;
|
||||
import sx.blah.discord.handle.obj.IUser;
|
||||
import sx.blah.discord.util.RateLimitException;
|
||||
import sx.blah.discord.handle.obj.*;
|
||||
import sx.blah.discord.util.*;
|
||||
|
||||
public class MCChatListener implements Listener, IListener<MessageReceivedEvent> {
|
||||
@EventHandler // Minecraft
|
||||
|
@ -28,16 +26,42 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
|||
return;
|
||||
if (e.getSender() instanceof DiscordSender || e.getSender() instanceof DiscordPlayerSender)
|
||||
return;
|
||||
if (e.getChannel().equals(Channel.GlobalChat))
|
||||
DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel,
|
||||
DiscordPlugin.sanitizeString("<" + (e.getSender() instanceof Player //
|
||||
if (e.getChannel().equals(Channel.GlobalChat)) {
|
||||
final String authorPlayer = DiscordPlugin.sanitizeString(e.getSender() instanceof Player //
|
||||
? ((Player) e.getSender()).getDisplayName() //
|
||||
: e.getSender().getName()) + "> " + e.getMessage()));
|
||||
: e.getSender().getName());
|
||||
final EmbedBuilder embed = new EmbedBuilder().withAuthorName(authorPlayer).withDescription(e.getMessage());
|
||||
final EmbedObject embedObject = e.getSender() instanceof Player
|
||||
? embed.withAuthorIcon(
|
||||
"https://minotar.net/avatar/" + ((Player) e.getSender()).getName() + "/32.png").build()
|
||||
: embed.build();
|
||||
final long nanoTime = System.nanoTime();
|
||||
if (lastmessage == null || lastmessage.isDeleted()
|
||||
|| !authorPlayer.equals(lastmessage.getEmbedded().get(0).getAuthor().getName())
|
||||
|| lastmsgtime / 1000000000f < nanoTime / 1000000000f - 120) {
|
||||
/*System.out.println("lastmsgtime: " + lastmsgtime);
|
||||
System.out.println("Current: " + nanoTime);
|
||||
System.out.println("2 mins before: " + (nanoTime - 120 * 1000000000));
|
||||
System.out.println("Diff: " + (nanoTime - (nanoTime - 120 * 1000000000)));*/
|
||||
lastmessage = DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel, "", embedObject);
|
||||
lastmsgtime = nanoTime;
|
||||
} else
|
||||
try {
|
||||
embedObject.description = lastmessage.getEmbedded().get(0).getDescription() + "\n"
|
||||
+ embedObject.description;
|
||||
lastmessage.edit("", embedObject);
|
||||
} catch (MissingPermissionsException | RateLimitException | DiscordException e1) {
|
||||
TBMCCoreAPI.SendException("An error occured while editing chat message!", e1);
|
||||
}
|
||||
} // TODO: Author URL
|
||||
}
|
||||
|
||||
private static final String[] UnconnectedCmds = new String[] { "list", "u", "shrug", "tableflip", "unflip", "mwiki",
|
||||
"yeehaw" };
|
||||
|
||||
private static IMessage lastmessage = null;
|
||||
private static long lastmsgtime = 0;
|
||||
|
||||
public static final HashMap<String, DiscordSender> UnconnectedSenders = new HashMap<>();
|
||||
public static final HashMap<String, DiscordPlayerSender> ConnectedSenders = new HashMap<>();
|
||||
|
||||
|
@ -49,6 +73,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
|||
if (!event.getMessage().getChannel().getID().equals(DiscordPlugin.chatchannel.getID())
|
||||
/* && !(event.getMessage().getChannel().isPrivate() && privatechat) */)
|
||||
return;
|
||||
lastmessage = null;
|
||||
if (CommandListener.runCommand(event.getMessage(), true))
|
||||
return;
|
||||
String dmessage = event.getMessage().getContent();
|
||||
|
|
|
@ -6,6 +6,8 @@ import org.bukkit.event.EventHandler;
|
|||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||
import org.bukkit.event.server.ServerCommandEvent;
|
||||
|
||||
import buttondevteam.discordplugin.DiscordPlayer;
|
||||
import buttondevteam.discordplugin.DiscordPlugin;
|
||||
import buttondevteam.discordplugin.commands.ConnectCommand;
|
||||
|
@ -61,4 +63,9 @@ public class MCListener implements Listener {
|
|||
DiscordPlugin.sanitizeString(e.getAffected().getBase().getDisplayName()) + " is "
|
||||
+ (e.getValue() ? "now" : "no longer") + " AFK.");
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onServerCommand(ServerCommandEvent e) {
|
||||
DiscordPlugin.Restart = !e.getCommand().equalsIgnoreCase("stop"); // The variable is always true except if stopped
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue