Add support for admin chat discord bridge

This commit is contained in:
business-goose 2022-03-21 19:11:17 +00:00
parent 7539c9e117
commit 68a93fc9c1
4 changed files with 121 additions and 66 deletions

View file

@ -92,8 +92,10 @@ public enum ConfigurationEntry
DISCORD_IS_ENABLED(Boolean.class, "discord.is_enabled"),
DISCORD_TOKEN(String.class, "discord.token"),
DISCORD_CHANNEL(String.class, "discord.channel"),
DISCORD_ADMIN_CHANNEL(String.class, "discord.admin_channel"),
DISCORD_PREFIX(String.class, "discord.prefix"),
DISCORD_FORMAT(String.class, "discord.format"),
DISCORD_ADMIN_FORMAT(String.class, "discord.admin_format"),
//
CHAT_FORMAT(String.class, "chat.format");

View file

@ -1,9 +1,11 @@
package me.StevenLawson.TotalFreedomMod.discord.bridge;
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;
@ -17,6 +19,8 @@ import org.javacord.api.entity.message.MessageAttachment;
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.regex.Pattern;
@ -24,33 +28,16 @@ import java.util.regex.Pattern;
public class DiscordBridge {
private static DiscordApi DISCORD_API;
private static TextChannel CHANNEL;
private static TextChannel ADMIN_CHANNEL;
public static DiscordCommandManager COMMAND_MANAGER;
public static void load() {
if (Boolean.FALSE.equals(MainConfig.getBoolean(ConfigurationEntry.DISCORD_IS_ENABLED))) {
return;
}
private static void onMessageCreateEvent(MessageCreateEvent message) {
boolean isAdmin = false;
try {
DISCORD_API = new DiscordApiBuilder()
.setToken(MainConfig.getString(ConfigurationEntry.DISCORD_TOKEN))
.login()
.join();
isAdmin = message.getChannel().getIdAsString().equalsIgnoreCase(ADMIN_CHANNEL.getIdAsString());
} catch (Exception ignored) {}
Optional<TextChannel> channelFuture = DISCORD_API.getTextChannelById(MainConfig.getString(ConfigurationEntry.DISCORD_CHANNEL));
if (!channelFuture.isPresent()) {
Log.warning("TFM 4.3 Reloaded could not find your channel, stopping!");
return;
}
CHANNEL = channelFuture.get();
COMMAND_MANAGER = new DiscordCommandManager();
COMMAND_MANAGER.init();
CHANNEL.addMessageCreateListener((message) -> {
String content = message.getMessageContent();
String prefix = MainConfig.getString(ConfigurationEntry.DISCORD_PREFIX);
MessageAuthor author = message.getMessage().getAuthor();
@ -78,7 +65,7 @@ public class DiscordBridge {
if (content.toLowerCase().startsWith(prefix)) {
COMMAND_MANAGER.parse(content, user.get(), server.get(), message.getChannel(), prefix);
} else {
String format = MainConfig.getString(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)));
@ -111,10 +98,54 @@ public class DiscordBridge {
}
}
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() {
if (Boolean.FALSE.equals(MainConfig.getBoolean(ConfigurationEntry.DISCORD_IS_ENABLED))) {
return;
}
try {
DISCORD_API = new DiscordApiBuilder()
.setToken(MainConfig.getString(ConfigurationEntry.DISCORD_TOKEN))
.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));
if (!channelFuture.isPresent()) {
Log.warning("TFM 4.3 Reloaded could not find your channel, stopping!");
return;
}
CHANNEL = channelFuture.get();
adminChannelFuture.ifPresent(textChannel -> ADMIN_CHANNEL = textChannel);
COMMAND_MANAGER = new DiscordCommandManager();
COMMAND_MANAGER.init();
CHANNEL.addMessageCreateListener(DiscordBridge::onMessageCreateEvent);
if (adminChannelFuture.isPresent()) {
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.");
@ -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() {
if (Boolean.FALSE.equals(MainConfig.getBoolean(ConfigurationEntry.DISCORD_IS_ENABLED))) {

View file

@ -898,14 +898,17 @@ public class Utilities
{
String name = sender.getName() + " " + PlayerRank.fromSender(sender).getPrefix() + ChatColor.WHITE;
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())
{
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

View file

@ -329,12 +329,14 @@ discord:
is_enabled: false
token: ''
channel: ''
admin_channel: ''
prefix: 'tf!'
# Format:
# {TAG} == Their full discord tag: e.g. Allink#9308
# {USERNAME} == Their discord username: e.g. Allink
# %1$s == User's message
format: '[Discord] <{TAG}> %1$s'
admin_format: '&f[&bADMIN&f] &4{TAG}&f:&b %1$s'
# Configure chat format
chat: