mirror of
https://github.com/TheDeus-Group/TFM-4.3-Reloaded.git
synced 2024-12-22 22:44:58 +00:00
Stop disconnecting
This commit is contained in:
parent
25b7c61d21
commit
27f939f26e
1 changed files with 206 additions and 190 deletions
|
@ -1,11 +1,12 @@
|
|||
package me.StevenLawson.TotalFreedomMod.discord.bridge;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
import me.StevenLawson.TotalFreedomMod.Log;
|
||||
import me.StevenLawson.TotalFreedomMod.admin.AdminList;
|
||||
import me.StevenLawson.TotalFreedomMod.config.ConfigurationEntry;
|
||||
import me.StevenLawson.TotalFreedomMod.config.MainConfig;
|
||||
import me.StevenLawson.TotalFreedomMod.discord.command.DiscordCommandManager;
|
||||
import me.StevenLawson.TotalFreedomMod.player.PlayerRank;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.ClickEvent;
|
||||
import net.md_5.bungee.api.chat.HoverEvent;
|
||||
|
@ -21,14 +22,9 @@ import org.javacord.api.entity.message.MessageAuthor;
|
|||
import org.javacord.api.entity.server.Server;
|
||||
import org.javacord.api.entity.user.User;
|
||||
import org.javacord.api.event.message.MessageCreateEvent;
|
||||
import org.javacord.api.event.message.MessageEvent;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class DiscordBridge {
|
||||
|
||||
private static DiscordApi DISCORD_API;
|
||||
private static TextChannel CHANNEL;
|
||||
private static TextChannel ADMIN_CHANNEL;
|
||||
|
@ -38,14 +34,18 @@ public class DiscordBridge {
|
|||
boolean isAdmin = false;
|
||||
|
||||
try {
|
||||
isAdmin = message.getChannel().getIdAsString().equalsIgnoreCase(ADMIN_CHANNEL.getIdAsString());
|
||||
} catch (Exception ignored) {}
|
||||
isAdmin = message.getChannel().getIdAsString()
|
||||
.equalsIgnoreCase(ADMIN_CHANNEL.getIdAsString());
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
||||
String content = message.getMessage().getReadableContent();
|
||||
String prefix = MainConfig.getString(ConfigurationEntry.DISCORD_PREFIX);
|
||||
MessageAuthor author = message.getMessage().getAuthor();
|
||||
|
||||
if (author.isBotUser() || !message.isServerMessage()) return;
|
||||
if (author.isBotUser() || !message.isServerMessage()) {
|
||||
return;
|
||||
}
|
||||
Optional<Server> server = message.getServer();
|
||||
Optional<User> user = author.asUser();
|
||||
|
||||
|
@ -56,12 +56,14 @@ public class DiscordBridge {
|
|||
}
|
||||
|
||||
if (!server.isPresent()) {
|
||||
Log.warning("Discord server wasn't present in message, this may be a sign you've not properly configured the intents for your bot.");
|
||||
Log.warning(
|
||||
"Discord server wasn't present in message, this may be a sign you've not properly configured the intents for your bot.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!user.isPresent()) {
|
||||
Log.warning("Unable to get user of message author. This may be a sign you've not properly configured the intents for your bot.");
|
||||
Log.warning(
|
||||
"Unable to get user of message author. This may be a sign you've not properly configured the intents for your bot.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -73,13 +75,18 @@ public class DiscordBridge {
|
|||
String reply = "";
|
||||
if (referenced.isPresent()) {
|
||||
Message replyingTo = referenced.get();
|
||||
reply = String.format(ChatColor.GRAY + "┌ " + ChatColor.YELLOW + "%s" + ChatColor.GRAY + " %s\n" + ChatColor.WHITE, replyingTo.getAuthor().getDiscriminatedName(), replyingTo.getReadableContent());
|
||||
reply = String.format(
|
||||
ChatColor.GRAY + "┌ " + ChatColor.YELLOW + "%s" + ChatColor.GRAY + " %s\n"
|
||||
+ ChatColor.WHITE, replyingTo.getAuthor().getDiscriminatedName(),
|
||||
replyingTo.getReadableContent());
|
||||
}
|
||||
|
||||
String format = MainConfig.getString((isAdmin) ? ConfigurationEntry.DISCORD_ADMIN_FORMAT : ConfigurationEntry.DISCORD_FORMAT);
|
||||
String format = MainConfig.getString((isAdmin) ? ConfigurationEntry.DISCORD_ADMIN_FORMAT
|
||||
: ConfigurationEntry.DISCORD_FORMAT);
|
||||
format = format.replace("{TAG}", author.getDiscriminatedName());
|
||||
format = format.replace("{USERNAME}", author.getName());
|
||||
BaseComponent[] components = TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', String.format(format, content)));
|
||||
BaseComponent[] components = TextComponent.fromLegacyText(
|
||||
ChatColor.translateAlternateColorCodes('&', String.format(format, content)));
|
||||
TextComponent component = new TextComponent(reply);
|
||||
|
||||
for (BaseComponent baseComponent : components) {
|
||||
|
@ -91,7 +98,8 @@ public class DiscordBridge {
|
|||
for (MessageAttachment messageAttachment : message.getMessageAttachments()) {
|
||||
String url = messageAttachment.getProxyUrl().toString();
|
||||
ClickEvent clickEvent = new ClickEvent(ClickEvent.Action.OPEN_URL, url);
|
||||
TextComponent warningComponent = new TextComponent("WARNING: By clicking on this text, your client will open:\n\n");
|
||||
TextComponent warningComponent = new TextComponent(
|
||||
"WARNING: By clicking on this text, your client will open:\n\n");
|
||||
warningComponent.setColor(net.md_5.bungee.api.ChatColor.RED);
|
||||
warningComponent.setBold(true);
|
||||
TextComponent urlComponent = new TextComponent(url);
|
||||
|
@ -99,8 +107,10 @@ public class DiscordBridge {
|
|||
urlComponent.setUnderlined(true);
|
||||
urlComponent.setBold(false);
|
||||
warningComponent.addExtra(urlComponent);
|
||||
HoverEvent hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[]{warningComponent});
|
||||
TextComponent mediaComponent = new TextComponent((i == 0 && content.isEmpty()) ? "[Media]" : " [Media]");
|
||||
HoverEvent hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT,
|
||||
new BaseComponent[]{warningComponent});
|
||||
TextComponent mediaComponent = new TextComponent(
|
||||
(i == 0 && content.isEmpty()) ? "[Media]" : " [Media]");
|
||||
mediaComponent.setColor(net.md_5.bungee.api.ChatColor.YELLOW);
|
||||
mediaComponent.setClickEvent(clickEvent);
|
||||
mediaComponent.setHoverEvent(hoverEvent);
|
||||
|
@ -110,10 +120,8 @@ public class DiscordBridge {
|
|||
}
|
||||
|
||||
if (isAdmin) {
|
||||
for (org.bukkit.entity.Player player : Bukkit.getOnlinePlayers())
|
||||
{
|
||||
if (AdminList.isSuperAdmin(player))
|
||||
{
|
||||
for (org.bukkit.entity.Player player : Bukkit.getOnlinePlayers()) {
|
||||
if (AdminList.isSuperAdmin(player)) {
|
||||
player.spigot().sendMessage(component);
|
||||
}
|
||||
}
|
||||
|
@ -136,8 +144,10 @@ public class DiscordBridge {
|
|||
.login()
|
||||
.join();
|
||||
|
||||
Optional<TextChannel> channelFuture = DISCORD_API.getTextChannelById(MainConfig.getString(ConfigurationEntry.DISCORD_CHANNEL));
|
||||
Optional<TextChannel> adminChannelFuture = DISCORD_API.getTextChannelById(MainConfig.getString(ConfigurationEntry.DISCORD_ADMIN_CHANNEL));
|
||||
Optional<TextChannel> channelFuture = DISCORD_API.getTextChannelById(
|
||||
MainConfig.getString(ConfigurationEntry.DISCORD_CHANNEL));
|
||||
Optional<TextChannel> adminChannelFuture = DISCORD_API.getTextChannelById(
|
||||
MainConfig.getString(ConfigurationEntry.DISCORD_ADMIN_CHANNEL));
|
||||
|
||||
if (!channelFuture.isPresent()) {
|
||||
Log.warning("TFM 4.3 Reloaded could not find your channel, stopping!");
|
||||
|
@ -155,9 +165,12 @@ public class DiscordBridge {
|
|||
ADMIN_CHANNEL.addMessageCreateListener(DiscordBridge::onMessageCreateEvent);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.warning("Uh oh! It looks like TFM 4.3 Reloaded Discord couldn't start! Please check you have defined the bot's token & channel and also given it the correct permissions! (Read Messages and Send Messages)");
|
||||
Log.warning("If you've already set that up however, you may to read the exception below.");
|
||||
Log.warning("If this is a bug with TFM 4.3 Reloaded, please report it at https://github.com/TheDeus-Group/TFM-4.3-Reloaded/issues or https://code.cat.casa/TheDeus-Group/TFM-4.3-Reloaded/issues");
|
||||
Log.warning(
|
||||
"Uh oh! It looks like TFM 4.3 Reloaded Discord couldn't start! Please check you have defined the bot's token & channel and also given it the correct permissions! (Read Messages and Send Messages)");
|
||||
Log.warning(
|
||||
"If you've already set that up however, you may to read the exception below.");
|
||||
Log.warning(
|
||||
"If this is a bug with TFM 4.3 Reloaded, please report it at https://github.com/TheDeus-Group/TFM-4.3-Reloaded/issues or https://code.cat.casa/TheDeus-Group/TFM-4.3-Reloaded/issues");
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
@ -183,7 +196,9 @@ public class DiscordBridge {
|
|||
}
|
||||
|
||||
public static void transmitMessage(String message, boolean disconnectAfterwards) {
|
||||
if (CHANNEL == null) return;
|
||||
if (CHANNEL == null) {
|
||||
return;
|
||||
}
|
||||
if (!disconnectAfterwards) {
|
||||
CHANNEL.sendMessage(sanitizeMessage(message));
|
||||
} else {
|
||||
|
@ -191,7 +206,7 @@ public class DiscordBridge {
|
|||
CHANNEL.sendMessage(sanitizeMessage(message)).get();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
CountDownLatch shutdownWaiter = new CountDownLatch(1);
|
||||
/*CountDownLatch shutdownWaiter = new CountDownLatch(1);
|
||||
|
||||
Thread t = new Thread(() -> {
|
||||
DISCORD_API.addLostConnectionListener(lostConnectionEvent -> shutdownWaiter.countDown());
|
||||
|
@ -202,7 +217,7 @@ public class DiscordBridge {
|
|||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
t.start();*/
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -211,7 +226,9 @@ public class DiscordBridge {
|
|||
}
|
||||
|
||||
public static void transmitAdminMessage(String message, boolean disconnectAfterwards) {
|
||||
if (ADMIN_CHANNEL == null) return;
|
||||
if (ADMIN_CHANNEL == null) {
|
||||
return;
|
||||
}
|
||||
if (!disconnectAfterwards) {
|
||||
ADMIN_CHANNEL.sendMessage(sanitizeMessage(message));
|
||||
} else {
|
||||
|
@ -219,7 +236,7 @@ public class DiscordBridge {
|
|||
ADMIN_CHANNEL.sendMessage(sanitizeMessage(message)).get();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
DISCORD_API.disconnect();
|
||||
//DISCORD_API.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -234,8 +251,7 @@ public class DiscordBridge {
|
|||
Log.info("TFM 4.3 Reloaded Discord stopped.");
|
||||
}
|
||||
|
||||
public static String fullySanitizeDiscordMessage(String message)
|
||||
{
|
||||
public static String fullySanitizeDiscordMessage(String message) {
|
||||
String partiallySanitized = sanitizeMessage(message);
|
||||
return partiallySanitized.replaceAll("([`_~*|\\\\])", "\\\\$1");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue