mirror of
https://github.com/TheDeus-Group/TFM-4.3-Reloaded.git
synced 2024-12-23 02:04:57 +00:00
Add support for admin chat discord bridge
This commit is contained in:
parent
7539c9e117
commit
68a93fc9c1
4 changed files with 121 additions and 66 deletions
|
@ -92,8 +92,10 @@ public enum ConfigurationEntry
|
||||||
DISCORD_IS_ENABLED(Boolean.class, "discord.is_enabled"),
|
DISCORD_IS_ENABLED(Boolean.class, "discord.is_enabled"),
|
||||||
DISCORD_TOKEN(String.class, "discord.token"),
|
DISCORD_TOKEN(String.class, "discord.token"),
|
||||||
DISCORD_CHANNEL(String.class, "discord.channel"),
|
DISCORD_CHANNEL(String.class, "discord.channel"),
|
||||||
|
DISCORD_ADMIN_CHANNEL(String.class, "discord.admin_channel"),
|
||||||
DISCORD_PREFIX(String.class, "discord.prefix"),
|
DISCORD_PREFIX(String.class, "discord.prefix"),
|
||||||
DISCORD_FORMAT(String.class, "discord.format"),
|
DISCORD_FORMAT(String.class, "discord.format"),
|
||||||
|
DISCORD_ADMIN_FORMAT(String.class, "discord.admin_format"),
|
||||||
//
|
//
|
||||||
CHAT_FORMAT(String.class, "chat.format");
|
CHAT_FORMAT(String.class, "chat.format");
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package me.StevenLawson.TotalFreedomMod.discord.bridge;
|
package me.StevenLawson.TotalFreedomMod.discord.bridge;
|
||||||
|
|
||||||
import me.StevenLawson.TotalFreedomMod.Log;
|
import me.StevenLawson.TotalFreedomMod.Log;
|
||||||
|
import me.StevenLawson.TotalFreedomMod.admin.AdminList;
|
||||||
import me.StevenLawson.TotalFreedomMod.config.ConfigurationEntry;
|
import me.StevenLawson.TotalFreedomMod.config.ConfigurationEntry;
|
||||||
import me.StevenLawson.TotalFreedomMod.config.MainConfig;
|
import me.StevenLawson.TotalFreedomMod.config.MainConfig;
|
||||||
import me.StevenLawson.TotalFreedomMod.discord.command.DiscordCommandManager;
|
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.BaseComponent;
|
||||||
import net.md_5.bungee.api.chat.ClickEvent;
|
import net.md_5.bungee.api.chat.ClickEvent;
|
||||||
import net.md_5.bungee.api.chat.HoverEvent;
|
import net.md_5.bungee.api.chat.HoverEvent;
|
||||||
|
@ -17,6 +19,8 @@ import org.javacord.api.entity.message.MessageAttachment;
|
||||||
import org.javacord.api.entity.message.MessageAuthor;
|
import org.javacord.api.entity.message.MessageAuthor;
|
||||||
import org.javacord.api.entity.server.Server;
|
import org.javacord.api.entity.server.Server;
|
||||||
import org.javacord.api.entity.user.User;
|
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.Optional;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
@ -24,8 +28,94 @@ import java.util.regex.Pattern;
|
||||||
public class DiscordBridge {
|
public class DiscordBridge {
|
||||||
private static DiscordApi DISCORD_API;
|
private static DiscordApi DISCORD_API;
|
||||||
private static TextChannel CHANNEL;
|
private static TextChannel CHANNEL;
|
||||||
|
private static TextChannel ADMIN_CHANNEL;
|
||||||
public static DiscordCommandManager COMMAND_MANAGER;
|
public static DiscordCommandManager COMMAND_MANAGER;
|
||||||
|
|
||||||
|
private static void onMessageCreateEvent(MessageCreateEvent message) {
|
||||||
|
boolean isAdmin = false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
isAdmin = message.getChannel().getIdAsString().equalsIgnoreCase(ADMIN_CHANNEL.getIdAsString());
|
||||||
|
} catch (Exception ignored) {}
|
||||||
|
|
||||||
|
String content = message.getMessageContent();
|
||||||
|
String prefix = MainConfig.getString(ConfigurationEntry.DISCORD_PREFIX);
|
||||||
|
MessageAuthor author = message.getMessage().getAuthor();
|
||||||
|
|
||||||
|
if (author.isBotUser() || !message.isServerMessage()) return;
|
||||||
|
Optional<Server> server = message.getServer();
|
||||||
|
Optional<User> user = author.asUser();
|
||||||
|
|
||||||
|
if(prefix == null) {
|
||||||
|
Log.severe("Bot prefix does not exist. Stopping bot...");
|
||||||
|
stop();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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.");
|
||||||
|
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.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (content.toLowerCase().startsWith(prefix)) {
|
||||||
|
COMMAND_MANAGER.parse(content, user.get(), server.get(), message.getChannel(), prefix);
|
||||||
|
} else {
|
||||||
|
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)));
|
||||||
|
TextComponent component = new TextComponent("");
|
||||||
|
|
||||||
|
for (BaseComponent baseComponent : components) {
|
||||||
|
component.addExtra(baseComponent);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(message.getMessageAttachments().size() > 0) {
|
||||||
|
int i = 0;
|
||||||
|
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");
|
||||||
|
warningComponent.setColor(net.md_5.bungee.api.ChatColor.RED);
|
||||||
|
warningComponent.setBold(true);
|
||||||
|
TextComponent urlComponent = new TextComponent(url);
|
||||||
|
urlComponent.setColor(net.md_5.bungee.api.ChatColor.DARK_AQUA);
|
||||||
|
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]");
|
||||||
|
mediaComponent.setColor(net.md_5.bungee.api.ChatColor.YELLOW);
|
||||||
|
mediaComponent.setClickEvent(clickEvent);
|
||||||
|
mediaComponent.setHoverEvent(hoverEvent);
|
||||||
|
component.addExtra(mediaComponent);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(isAdmin) {
|
||||||
|
String name = author.getDiscriminatedName();
|
||||||
|
Log.info("[ADMIN] " + name + ": " + message);
|
||||||
|
|
||||||
|
for (org.bukkit.entity.Player player : Bukkit.getOnlinePlayers())
|
||||||
|
{
|
||||||
|
if (AdminList.isSuperAdmin(player))
|
||||||
|
{
|
||||||
|
player.spigot().sendMessage(component);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Bukkit.spigot().broadcast(component);
|
||||||
|
}
|
||||||
|
Log.info(component.toPlainText());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void load() {
|
public static void load() {
|
||||||
|
|
||||||
if (Boolean.FALSE.equals(MainConfig.getBoolean(ConfigurationEntry.DISCORD_IS_ENABLED))) {
|
if (Boolean.FALSE.equals(MainConfig.getBoolean(ConfigurationEntry.DISCORD_IS_ENABLED))) {
|
||||||
|
@ -39,6 +129,7 @@ public class DiscordBridge {
|
||||||
.join();
|
.join();
|
||||||
|
|
||||||
Optional<TextChannel> channelFuture = DISCORD_API.getTextChannelById(MainConfig.getString(ConfigurationEntry.DISCORD_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()) {
|
if (!channelFuture.isPresent()) {
|
||||||
Log.warning("TFM 4.3 Reloaded could not find your channel, stopping!");
|
Log.warning("TFM 4.3 Reloaded could not find your channel, stopping!");
|
||||||
|
@ -47,74 +138,14 @@ public class DiscordBridge {
|
||||||
}
|
}
|
||||||
|
|
||||||
CHANNEL = channelFuture.get();
|
CHANNEL = channelFuture.get();
|
||||||
|
adminChannelFuture.ifPresent(textChannel -> ADMIN_CHANNEL = textChannel);
|
||||||
COMMAND_MANAGER = new DiscordCommandManager();
|
COMMAND_MANAGER = new DiscordCommandManager();
|
||||||
COMMAND_MANAGER.init();
|
COMMAND_MANAGER.init();
|
||||||
|
|
||||||
CHANNEL.addMessageCreateListener((message) -> {
|
CHANNEL.addMessageCreateListener(DiscordBridge::onMessageCreateEvent);
|
||||||
String content = message.getMessageContent();
|
if (adminChannelFuture.isPresent()) {
|
||||||
String prefix = MainConfig.getString(ConfigurationEntry.DISCORD_PREFIX);
|
ADMIN_CHANNEL.addMessageCreateListener(DiscordBridge::onMessageCreateEvent);
|
||||||
MessageAuthor author = message.getMessage().getAuthor();
|
}
|
||||||
|
|
||||||
if (author.isBotUser() || !message.isServerMessage()) return;
|
|
||||||
Optional<Server> server = message.getServer();
|
|
||||||
Optional<User> user = author.asUser();
|
|
||||||
|
|
||||||
if(prefix == null) {
|
|
||||||
Log.severe("Bot prefix does not exist. Stopping bot...");
|
|
||||||
stop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
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.");
|
|
||||||
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.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (content.toLowerCase().startsWith(prefix)) {
|
|
||||||
COMMAND_MANAGER.parse(content, user.get(), server.get(), message.getChannel(), prefix);
|
|
||||||
} else {
|
|
||||||
String format = MainConfig.getString(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)));
|
|
||||||
TextComponent component = new TextComponent("");
|
|
||||||
|
|
||||||
for (BaseComponent baseComponent : components) {
|
|
||||||
component.addExtra(baseComponent);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(message.getMessageAttachments().size() > 0) {
|
|
||||||
int i = 0;
|
|
||||||
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");
|
|
||||||
warningComponent.setColor(net.md_5.bungee.api.ChatColor.RED);
|
|
||||||
warningComponent.setBold(true);
|
|
||||||
TextComponent urlComponent = new TextComponent(url);
|
|
||||||
urlComponent.setColor(net.md_5.bungee.api.ChatColor.DARK_AQUA);
|
|
||||||
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]");
|
|
||||||
mediaComponent.setColor(net.md_5.bungee.api.ChatColor.YELLOW);
|
|
||||||
mediaComponent.setClickEvent(clickEvent);
|
|
||||||
mediaComponent.setHoverEvent(hoverEvent);
|
|
||||||
component.addExtra(mediaComponent);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Bukkit.spigot().broadcast(component);
|
|
||||||
Log.info(component.toPlainText());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (Exception e) {
|
} 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("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 you've already set that up however, you may to read the exception below.");
|
||||||
|
@ -156,6 +187,23 @@ public class DiscordBridge {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void transmitAdminMessage(String message) {
|
||||||
|
transmitAdminMessage(message, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void transmitAdminMessage(String message, boolean disconnectAfterwards) {
|
||||||
|
if (ADMIN_CHANNEL == null) return;
|
||||||
|
if (!disconnectAfterwards) {
|
||||||
|
ADMIN_CHANNEL.sendMessage(sanitizeMessage(message));
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
ADMIN_CHANNEL.sendMessage(sanitizeMessage(message)).get();
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
DISCORD_API.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void stop() {
|
public static void stop() {
|
||||||
|
|
||||||
if (Boolean.FALSE.equals(MainConfig.getBoolean(ConfigurationEntry.DISCORD_IS_ENABLED))) {
|
if (Boolean.FALSE.equals(MainConfig.getBoolean(ConfigurationEntry.DISCORD_IS_ENABLED))) {
|
||||||
|
|
|
@ -898,14 +898,17 @@ public class Utilities
|
||||||
{
|
{
|
||||||
String name = sender.getName() + " " + PlayerRank.fromSender(sender).getPrefix() + ChatColor.WHITE;
|
String name = sender.getName() + " " + PlayerRank.fromSender(sender).getPrefix() + ChatColor.WHITE;
|
||||||
Log.info("[ADMIN] " + name + ": " + message);
|
Log.info("[ADMIN] " + name + ": " + message);
|
||||||
|
String adminChatMessage = "[" + ChatColor.AQUA + "ADMIN" + ChatColor.WHITE + "] " + ChatColor.DARK_RED + name + ": " + ChatColor.AQUA + message;
|
||||||
|
|
||||||
for (org.bukkit.entity.Player player : Bukkit.getOnlinePlayers())
|
for (org.bukkit.entity.Player player : Bukkit.getOnlinePlayers())
|
||||||
{
|
{
|
||||||
if (AdminList.isSuperAdmin(player))
|
if (AdminList.isSuperAdmin(player))
|
||||||
{
|
{
|
||||||
player.sendMessage("[" + ChatColor.AQUA + "ADMIN" + ChatColor.WHITE + "] " + ChatColor.DARK_RED + name + ": " + ChatColor.AQUA + message);
|
player.sendMessage(adminChatMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DiscordBridge.transmitAdminMessage(adminChatMessage.replaceAll("([`_~*])", "\\\\$1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
//getField: Borrowed from WorldEdit
|
//getField: Borrowed from WorldEdit
|
||||||
|
|
|
@ -329,12 +329,14 @@ discord:
|
||||||
is_enabled: false
|
is_enabled: false
|
||||||
token: ''
|
token: ''
|
||||||
channel: ''
|
channel: ''
|
||||||
|
admin_channel: ''
|
||||||
prefix: 'tf!'
|
prefix: 'tf!'
|
||||||
# Format:
|
# Format:
|
||||||
# {TAG} == Their full discord tag: e.g. Allink#9308
|
# {TAG} == Their full discord tag: e.g. Allink#9308
|
||||||
# {USERNAME} == Their discord username: e.g. Allink
|
# {USERNAME} == Their discord username: e.g. Allink
|
||||||
# %1$s == User's message
|
# %1$s == User's message
|
||||||
format: '[Discord] <{TAG}> %1$s'
|
format: '[Discord] <{TAG}> %1$s'
|
||||||
|
admin_format: '&f[&bADMIN&f] &4{TAG}&f:&b %1$s'
|
||||||
|
|
||||||
# Configure chat format
|
# Configure chat format
|
||||||
chat:
|
chat:
|
||||||
|
|
Loading…
Reference in a new issue