2022-03-21 14:24:30 +00:00
package me.StevenLawson.TotalFreedomMod.discord.bridge ;
2022-03-20 12:35:43 +00:00
import me.StevenLawson.TotalFreedomMod.Log ;
2022-03-21 19:11:17 +00:00
import me.StevenLawson.TotalFreedomMod.admin.AdminList ;
2022-03-20 12:35:43 +00:00
import me.StevenLawson.TotalFreedomMod.config.ConfigurationEntry ;
import me.StevenLawson.TotalFreedomMod.config.MainConfig ;
2022-03-21 14:24:30 +00:00
import me.StevenLawson.TotalFreedomMod.discord.command.DiscordCommandManager ;
2022-03-21 19:11:17 +00:00
import me.StevenLawson.TotalFreedomMod.player.PlayerRank ;
2022-03-21 10:12:25 +00:00
import net.md_5.bungee.api.chat.BaseComponent ;
import net.md_5.bungee.api.chat.ClickEvent ;
import net.md_5.bungee.api.chat.HoverEvent ;
import net.md_5.bungee.api.chat.TextComponent ;
2022-03-20 12:35:43 +00:00
import org.bukkit.Bukkit ;
2022-03-21 09:26:24 +00:00
import org.bukkit.ChatColor ;
2022-03-20 12:35:43 +00:00
import org.javacord.api.DiscordApi ;
import org.javacord.api.DiscordApiBuilder ;
import org.javacord.api.entity.channel.TextChannel ;
2022-03-29 22:39:31 +00:00
import org.javacord.api.entity.message.Message ;
2022-03-21 10:12:25 +00:00
import org.javacord.api.entity.message.MessageAttachment ;
2022-03-20 12:35:43 +00:00
import org.javacord.api.entity.message.MessageAuthor ;
2022-03-21 14:24:30 +00:00
import org.javacord.api.entity.server.Server ;
import org.javacord.api.entity.user.User ;
2022-03-21 19:11:17 +00:00
import org.javacord.api.event.message.MessageCreateEvent ;
import org.javacord.api.event.message.MessageEvent ;
2022-03-20 12:35:43 +00:00
2022-03-21 14:24:30 +00:00
import java.util.Optional ;
2022-03-20 12:35:43 +00:00
import java.util.regex.Pattern ;
public class DiscordBridge {
private static DiscordApi DISCORD_API ;
private static TextChannel CHANNEL ;
2022-03-21 19:11:17 +00:00
private static TextChannel ADMIN_CHANNEL ;
2022-03-21 14:24:30 +00:00
public static DiscordCommandManager COMMAND_MANAGER ;
2022-03-20 12:35:43 +00:00
2022-03-21 19:11:17 +00:00
private static void onMessageCreateEvent ( MessageCreateEvent message ) {
boolean isAdmin = false ;
try {
isAdmin = message . getChannel ( ) . getIdAsString ( ) . equalsIgnoreCase ( ADMIN_CHANNEL . getIdAsString ( ) ) ;
} catch ( Exception ignored ) { }
2022-03-29 22:25:33 +00:00
String content = message . getMessage ( ) . getReadableContent ( ) ;
2022-03-21 19:11:17 +00:00
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 {
2022-03-29 22:39:31 +00:00
Optional < Message > referenced = message . getMessage ( ) . getReferencedMessage ( ) ;
String reply = " " ;
if ( referenced . isPresent ( ) ) {
Message replyingTo = referenced . get ( ) ;
reply = String . format ( " ┌ %s \ t%s \ n " , replyingTo . getAuthor ( ) . getDiscriminatedName ( ) , replyingTo . getReadableContent ( ) ) ;
}
2022-03-21 19:11:17 +00:00
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 ) ) ) ;
2022-03-29 22:39:31 +00:00
TextComponent component = new TextComponent ( reply ) ;
2022-03-21 19:11:17 +00:00
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 ) {
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 ( ) ) ;
}
}
2022-03-20 12:35:43 +00:00
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 ) ) ;
2022-03-21 19:11:17 +00:00
Optional < TextChannel > adminChannelFuture = DISCORD_API . getTextChannelById ( MainConfig . getString ( ConfigurationEntry . DISCORD_ADMIN_CHANNEL ) ) ;
2022-03-20 12:35:43 +00:00
if ( ! channelFuture . isPresent ( ) ) {
Log . warning ( " TFM 4.3 Reloaded could not find your channel, stopping! " ) ;
return ;
}
CHANNEL = channelFuture . get ( ) ;
2022-03-21 19:11:17 +00:00
adminChannelFuture . ifPresent ( textChannel - > ADMIN_CHANNEL = textChannel ) ;
2022-03-21 14:24:30 +00:00
COMMAND_MANAGER = new DiscordCommandManager ( ) ;
COMMAND_MANAGER . init ( ) ;
2022-03-20 12:35:43 +00:00
2022-03-21 19:11:17 +00:00
CHANNEL . addMessageCreateListener ( DiscordBridge : : onMessageCreateEvent ) ;
if ( adminChannelFuture . isPresent ( ) ) {
ADMIN_CHANNEL . addMessageCreateListener ( DiscordBridge : : onMessageCreateEvent ) ;
}
2022-03-20 12:35:43 +00:00
} 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 " ) ;
e . printStackTrace ( ) ;
return ;
}
Log . info ( " TFM 4.3 Reloaded Discord started. " ) ;
transmitMessage ( " **Server has started** " ) ;
}
public static String sanitizeMessage ( String message ) {
Pattern colors = Pattern . compile ( " §. " , Pattern . MULTILINE | Pattern . CASE_INSENSITIVE ) ;
Pattern pings = Pattern . compile ( " @ " , Pattern . MULTILINE | Pattern . CASE_INSENSITIVE ) ;
if ( message . length ( ) > 2000 ) {
message = message . substring ( 0 , 2000 ) ;
}
2022-03-21 10:12:25 +00:00
return message . replaceAll ( colors . pattern ( ) , " " ) . replaceAll ( pings . pattern ( ) , " @ \ u200B " ) ;
2022-03-20 12:35:43 +00:00
}
public static void transmitMessage ( String message ) {
transmitMessage ( message , false ) ;
}
public static void transmitMessage ( String message , boolean disconnectAfterwards ) {
if ( CHANNEL = = null ) return ;
if ( ! disconnectAfterwards ) {
CHANNEL . sendMessage ( sanitizeMessage ( message ) ) ;
} else {
try {
CHANNEL . sendMessage ( sanitizeMessage ( message ) ) . get ( ) ;
} catch ( Exception ignored ) {
}
DISCORD_API . disconnect ( ) ;
}
}
2022-03-21 19:11:17 +00:00
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 ( ) ;
}
}
2022-03-20 12:35:43 +00:00
public static void stop ( ) {
if ( Boolean . FALSE . equals ( MainConfig . getBoolean ( ConfigurationEntry . DISCORD_IS_ENABLED ) ) ) {
return ;
}
transmitMessage ( " **Server has stopped** " , true ) ;
Log . info ( " TFM 4.3 Reloaded Discord stopped. " ) ;
}
2022-03-29 21:35:52 +00:00
public static String fullySanitizeDiscordMessage ( String message )
{
String partiallySanitized = sanitizeMessage ( message ) ;
2022-03-29 21:37:31 +00:00
return partiallySanitized . replaceAll ( " ([`_~*| \\ \\ ]) " , " \\ \\ $1 " ) ;
2022-03-29 21:35:52 +00:00
}
2022-03-20 12:35:43 +00:00
}