2021-07-01 15:43:35 +02:00
package net.essentialsx.discord ;
import com.earth2me.essentials.IConf ;
import com.earth2me.essentials.config.ConfigurateUtil ;
import com.earth2me.essentials.config.EssentialsConfiguration ;
import com.earth2me.essentials.utils.FormatUtil ;
import net.dv8tion.jda.api.OnlineStatus ;
import net.dv8tion.jda.api.entities.Activity ;
import org.apache.logging.log4j.Level ;
import org.bukkit.entity.Player ;
import java.io.File ;
import java.text.MessageFormat ;
import java.util.ArrayList ;
2021-10-24 18:12:42 +02:00
import java.util.Collections ;
2021-07-01 15:43:35 +02:00
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
import java.util.regex.Pattern ;
import java.util.regex.PatternSyntaxException ;
2021-07-03 20:17:32 +02:00
import static com.earth2me.essentials.I18n.tl ;
2021-07-01 15:43:35 +02:00
public class DiscordSettings implements IConf {
private final EssentialsConfiguration config ;
private final EssentialsDiscord plugin ;
private final Map < String , Long > nameToChannelIdMap = new HashMap < > ( ) ;
private final Map < Long , List < String > > channelIdToNamesMap = new HashMap < > ( ) ;
private OnlineStatus status ;
private Activity statusActivity ;
2021-10-24 18:12:42 +02:00
private List < Pattern > discordFilter ;
2021-07-01 15:43:35 +02:00
private MessageFormat consoleFormat ;
private Level consoleLogLevel ;
2021-10-24 18:12:42 +02:00
private List < Pattern > consoleFilter ;
2021-07-01 15:43:35 +02:00
private MessageFormat discordToMcFormat ;
private MessageFormat tempMuteFormat ;
private MessageFormat tempMuteReasonFormat ;
private MessageFormat permMuteFormat ;
private MessageFormat permMuteReasonFormat ;
private MessageFormat unmuteFormat ;
private MessageFormat kickFormat ;
public DiscordSettings ( EssentialsDiscord plugin ) {
this . plugin = plugin ;
this . config = new EssentialsConfiguration ( new File ( plugin . getDataFolder ( ) , " config.yml " ) , " /config.yml " , EssentialsDiscord . class ) ;
reloadConfig ( ) ;
}
public String getBotToken ( ) {
return config . getString ( " token " , " " ) ;
}
public long getGuildId ( ) {
return config . getLong ( " guild " , 0 ) ;
}
2021-11-07 00:18:37 +01:00
public String getDiscordUrl ( ) {
return config . getString ( " discord-url " , " https://discord.gg/invite-code " ) ;
}
2021-07-01 15:43:35 +02:00
public long getPrimaryChannelId ( ) {
return config . getLong ( " channels.primary " , 0 ) ;
}
public long getChannelId ( String key ) {
try {
return Long . parseLong ( key ) ;
} catch ( NumberFormatException ignored ) {
return nameToChannelIdMap . getOrDefault ( key , 0L ) ;
}
}
2021-10-11 21:54:55 +02:00
public List < String > getChannelNames ( ) {
return new ArrayList < > ( nameToChannelIdMap . keySet ( ) ) ;
}
2021-07-01 15:43:35 +02:00
public List < String > getKeysFromChannelId ( long channelId ) {
return channelIdToNamesMap . get ( channelId ) ;
}
public String getMessageChannel ( String key ) {
return config . getString ( " message-types. " + key , " none " ) ;
}
public boolean isShowDiscordAttachments ( ) {
return config . getBoolean ( " show-discord-attachments " , true ) ;
}
public List < String > getPermittedFormattingRoles ( ) {
return config . getList ( " permit-formatting-roles " , String . class ) ;
}
public OnlineStatus getStatus ( ) {
return status ;
}
public Activity getStatusActivity ( ) {
return statusActivity ;
}
public boolean isAlwaysReceivePrimary ( ) {
return config . getBoolean ( " always-receive-primary " , false ) ;
}
public int getChatDiscordMaxLength ( ) {
return config . getInt ( " chat.discord-max-length " , 2000 ) ;
}
public boolean isChatFilterNewlines ( ) {
return config . getBoolean ( " chat.filter-newlines " , true ) ;
}
2021-10-24 18:12:42 +02:00
public List < Pattern > getDiscordFilters ( ) {
2021-07-01 15:43:35 +02:00
return discordFilter ;
}
public boolean isShowWebhookMessages ( ) {
return config . getBoolean ( " chat.show-webhook-messages " , false ) ;
}
public boolean isShowBotMessages ( ) {
return config . getBoolean ( " chat.show-bot-messages " , false ) ;
}
public boolean isShowAllChat ( ) {
return config . getBoolean ( " chat.show-all-chat " , false ) ;
}
2021-07-05 21:21:56 +02:00
public List < String > getRelayToConsoleList ( ) {
return config . getList ( " chat.relay-to-console " , String . class ) ;
}
2021-07-01 15:43:35 +02:00
public String getConsoleChannelDef ( ) {
return config . getString ( " console.channel " , " none " ) ;
}
public MessageFormat getConsoleFormat ( ) {
return consoleFormat ;
}
public String getConsoleWebhookName ( ) {
return config . getString ( " console.webhook-name " , " EssentialsX Console Relay " ) ;
}
public boolean isConsoleCommandRelay ( ) {
return config . getBoolean ( " console.command-relay " , false ) ;
}
2021-08-09 20:48:01 +02:00
public boolean isConsoleBotCommandRelay ( ) {
return config . getBoolean ( " console.bot-command-relay " , false ) ;
}
2021-07-01 15:43:35 +02:00
public Level getConsoleLogLevel ( ) {
return consoleLogLevel ;
}
2021-10-24 18:12:42 +02:00
public List < Pattern > getConsoleFilters ( ) {
return consoleFilter ;
}
2021-07-01 15:43:35 +02:00
public boolean isShowAvatar ( ) {
return config . getBoolean ( " show-avatar " , false ) ;
}
public boolean isShowName ( ) {
return config . getBoolean ( " show-name " , false ) ;
}
2021-07-08 23:15:45 +02:00
public boolean isShowDisplayName ( ) {
return config . getBoolean ( " show-displayname " , false ) ;
}
2021-07-07 21:55:43 +02:00
public String getAvatarURL ( ) {
return config . getString ( " avatar-url " , " https://crafthead.net/helm/{uuid} " ) ;
}
2021-07-03 19:18:59 +02:00
public boolean isVanishFakeJoinLeave ( ) {
return config . getBoolean ( " vanish-fake-join-leave " , true ) ;
}
public boolean isVanishHideMessages ( ) {
return config . getBoolean ( " vanish-hide-messages " , true ) ;
}
2021-07-01 15:43:35 +02:00
// General command settings
public boolean isCommandEnabled ( String command ) {
return config . getBoolean ( " commands. " + command + " .enabled " , true ) ;
}
public boolean isCommandEphemeral ( String command ) {
return config . getBoolean ( " commands. " + command + " .hide-command " , true ) ;
}
public List < String > getCommandSnowflakes ( String command ) {
return config . getList ( " commands. " + command + " .allowed-roles " , String . class ) ;
}
public List < String > getCommandAdminSnowflakes ( String command ) {
return config . getList ( " commands. " + command + " .admin-roles " , String . class ) ;
}
// Message formats
public MessageFormat getDiscordToMcFormat ( ) {
return discordToMcFormat ;
}
public MessageFormat getMcToDiscordFormat ( Player player ) {
final String format = getFormatString ( " mc-to-discord " ) ;
final String filled ;
if ( plugin . isPAPI ( ) & & format ! = null ) {
filled = me . clip . placeholderapi . PlaceholderAPI . setPlaceholders ( player , format ) ;
} else {
filled = format ;
}
return generateMessageFormat ( filled , " {displayname}: {message} " , false ,
" username " , " displayname " , " message " , " world " , " prefix " , " suffix " ) ;
}
public MessageFormat getTempMuteFormat ( ) {
return tempMuteFormat ;
}
public MessageFormat getTempMuteReasonFormat ( ) {
return tempMuteReasonFormat ;
}
public MessageFormat getPermMuteFormat ( ) {
return permMuteFormat ;
}
public MessageFormat getPermMuteReasonFormat ( ) {
return permMuteReasonFormat ;
}
public MessageFormat getUnmuteFormat ( ) {
return unmuteFormat ;
}
public MessageFormat getJoinFormat ( Player player ) {
final String format = getFormatString ( " join " ) ;
final String filled ;
if ( plugin . isPAPI ( ) & & format ! = null ) {
filled = me . clip . placeholderapi . PlaceholderAPI . setPlaceholders ( player , format ) ;
} else {
filled = format ;
}
return generateMessageFormat ( filled , " :arrow_right: {displayname} has joined! " , false ,
2021-08-09 22:23:49 +02:00
" username " , " displayname " , " joinmessage " , " online " , " unique " ) ;
2021-07-01 15:43:35 +02:00
}
public MessageFormat getQuitFormat ( Player player ) {
final String format = getFormatString ( " quit " ) ;
final String filled ;
if ( plugin . isPAPI ( ) & & format ! = null ) {
filled = me . clip . placeholderapi . PlaceholderAPI . setPlaceholders ( player , format ) ;
} else {
filled = format ;
}
return generateMessageFormat ( filled , " :arrow_left: {displayname} has left! " , false ,
2021-08-09 22:23:49 +02:00
" username " , " displayname " , " quitmessage " , " online " , " unique " ) ;
2021-07-01 15:43:35 +02:00
}
public MessageFormat getDeathFormat ( Player player ) {
final String format = getFormatString ( " death " ) ;
final String filled ;
if ( plugin . isPAPI ( ) & & format ! = null ) {
filled = me . clip . placeholderapi . PlaceholderAPI . setPlaceholders ( player , format ) ;
} else {
filled = format ;
}
return generateMessageFormat ( filled , " :skull: {deathmessage} " , false ,
" username " , " displayname " , " deathmessage " ) ;
}
public MessageFormat getAfkFormat ( Player player ) {
final String format = getFormatString ( " afk " ) ;
final String filled ;
if ( plugin . isPAPI ( ) & & format ! = null ) {
filled = me . clip . placeholderapi . PlaceholderAPI . setPlaceholders ( player , format ) ;
} else {
filled = format ;
}
return generateMessageFormat ( filled , " :person_walking: {displayname} is now AFK! " , false ,
" username " , " displayname " ) ;
}
public MessageFormat getUnAfkFormat ( Player player ) {
final String format = getFormatString ( " un-afk " ) ;
final String filled ;
if ( plugin . isPAPI ( ) & & format ! = null ) {
filled = me . clip . placeholderapi . PlaceholderAPI . setPlaceholders ( player , format ) ;
} else {
filled = format ;
}
return generateMessageFormat ( filled , " :keyboard: {displayname} is no longer AFK! " , false ,
" username " , " displayname " ) ;
}
2021-07-05 03:57:13 +02:00
public MessageFormat getAdvancementFormat ( Player player ) {
final String format = getFormatString ( " advancement " ) ;
final String filled ;
if ( plugin . isPAPI ( ) & & format ! = null ) {
filled = me . clip . placeholderapi . PlaceholderAPI . setPlaceholders ( player , format ) ;
} else {
filled = format ;
}
return generateMessageFormat ( filled , " :medal: {displayname} has completed the advancement **{advancement}**! " , false ,
" username " , " displayname " , " advancement " ) ;
}
2021-09-15 16:52:14 +02:00
public MessageFormat getActionFormat ( Player player ) {
final String format = getFormatString ( " action " ) ;
final String filled ;
if ( plugin . isPAPI ( ) & & format ! = null ) {
filled = me . clip . placeholderapi . PlaceholderAPI . setPlaceholders ( player , format ) ;
} else {
filled = format ;
}
return generateMessageFormat ( filled , " :person_biking: {displayname} *{action}* " , false ,
" username " , " displayname " , " action " ) ;
}
2021-07-01 20:50:34 +02:00
public String getStartMessage ( ) {
return config . getString ( " messages.server-start " , " :white_check_mark: The server has started! " ) ;
}
public String getStopMessage ( ) {
return config . getString ( " messages.server-stop " , " :octagonal_sign: The server has stopped! " ) ;
}
2021-07-01 15:43:35 +02:00
public MessageFormat getKickFormat ( ) {
return kickFormat ;
}
private String getFormatString ( String node ) {
final String pathPrefix = node . startsWith ( " . " ) ? " " : " messages. " ;
return config . getString ( pathPrefix + ( pathPrefix . isEmpty ( ) ? node . substring ( 1 ) : node ) , null ) ;
}
private MessageFormat generateMessageFormat ( String content , String defaultStr , boolean format , String . . . arguments ) {
content = content = = null ? defaultStr : content ;
content = format ? FormatUtil . replaceFormat ( content ) : FormatUtil . stripFormat ( content ) ;
2021-07-01 18:13:51 +02:00
content = content . replace ( " ' " , " '' " ) ;
2021-07-01 15:43:35 +02:00
for ( int i = 0 ; i < arguments . length ; i + + ) {
content = content . replace ( " { " + arguments [ i ] + " } " , " { " + i + " } " ) ;
2021-07-01 17:02:27 +02:00
content = content . replace ( " { " + arguments [ i ] . toUpperCase ( ) + " } " , " { " + i + " } " ) ;
2021-07-01 15:43:35 +02:00
}
2021-07-01 18:13:51 +02:00
content = content . replaceAll ( " \\ {([^0-9]+)} " , " '{$1}' " ) ;
2021-07-01 15:43:35 +02:00
return new MessageFormat ( content ) ;
}
@Override
public void reloadConfig ( ) {
2021-07-03 20:17:32 +02:00
if ( plugin . isInvalidStartup ( ) ) {
plugin . getLogger ( ) . warning ( tl ( " discordReloadInvalid " ) ) ;
return ;
}
2021-07-01 15:43:35 +02:00
config . load ( ) ;
// Build channel maps
nameToChannelIdMap . clear ( ) ;
channelIdToNamesMap . clear ( ) ;
final Map < String , Object > section = ConfigurateUtil . getRawMap ( config , " channels " ) ;
for ( Map . Entry < String , Object > entry : section . entrySet ( ) ) {
if ( entry . getValue ( ) instanceof Long ) {
final long value = ( long ) entry . getValue ( ) ;
nameToChannelIdMap . put ( entry . getKey ( ) , value ) ;
channelIdToNamesMap . computeIfAbsent ( value , o - > new ArrayList < > ( ) ) . add ( entry . getKey ( ) ) ;
}
}
// Presence stuff
status = OnlineStatus . fromKey ( config . getString ( " presence.status " , " online " ) ) ;
if ( status = = OnlineStatus . UNKNOWN ) {
// Default invalid status to online
status = OnlineStatus . ONLINE ;
}
final String activity = config . getString ( " presence.activity " , " default " ) . trim ( ) . toUpperCase ( ) . replace ( " CUSTOM_STATUS " , " DEFAULT " ) ;
statusActivity = null ;
Activity . ActivityType activityType = null ;
try {
if ( ! activity . equals ( " NONE " ) ) {
activityType = Activity . ActivityType . valueOf ( activity ) ;
}
} catch ( IllegalArgumentException e ) {
activityType = Activity . ActivityType . DEFAULT ;
}
if ( activityType ! = null ) {
statusActivity = Activity . of ( activityType , config . getString ( " presence.message " , " Minecraft " ) ) ;
}
2021-10-24 18:12:42 +02:00
if ( config . isList ( " chat.discord-filter " ) ) {
final List < Pattern > filters = new ArrayList < > ( ) ;
for ( final String chatFilterString : config . getList ( " chat.discord-filter " , String . class ) ) {
if ( chatFilterString ! = null & & ! chatFilterString . trim ( ) . isEmpty ( ) ) {
try {
filters . add ( Pattern . compile ( chatFilterString ) ) ;
} catch ( PatternSyntaxException e ) {
plugin . getLogger ( ) . log ( java . util . logging . Level . WARNING , " Invalid pattern for \" chat.discord-filter \" : " + e . getMessage ( ) ) ;
}
}
}
discordFilter = Collections . unmodifiableList ( filters ) ;
} else {
2021-07-01 15:43:35 +02:00
try {
2021-10-24 18:12:42 +02:00
final String chatFilter = config . getString ( " chat.discord-filter " , null ) ;
if ( chatFilter ! = null & & ! chatFilter . trim ( ) . isEmpty ( ) ) {
discordFilter = Collections . singletonList ( Pattern . compile ( chatFilter ) ) ;
} else {
discordFilter = Collections . emptyList ( ) ;
}
2021-07-01 15:43:35 +02:00
} catch ( PatternSyntaxException e ) {
plugin . getLogger ( ) . log ( java . util . logging . Level . WARNING , " Invalid pattern for \" chat.discord-filter \" : " + e . getMessage ( ) ) ;
2021-10-24 18:12:42 +02:00
discordFilter = Collections . emptyList ( ) ;
2021-07-01 15:43:35 +02:00
}
}
consoleLogLevel = Level . toLevel ( config . getString ( " console.log-level " , null ) , Level . INFO ) ;
2021-10-24 18:12:42 +02:00
if ( config . isList ( " console.console-filter " ) ) {
final List < Pattern > filters = new ArrayList < > ( ) ;
for ( final String filterString : config . getList ( " console.console-filter " , String . class ) ) {
if ( filterString ! = null & & ! filterString . trim ( ) . isEmpty ( ) ) {
try {
filters . add ( Pattern . compile ( filterString ) ) ;
} catch ( PatternSyntaxException e ) {
plugin . getLogger ( ) . log ( java . util . logging . Level . WARNING , " Invalid pattern for \" console.log-level \" : " + e . getMessage ( ) ) ;
}
}
}
consoleFilter = Collections . unmodifiableList ( filters ) ;
} else {
try {
final String cFilter = config . getString ( " console.console-filter " , null ) ;
if ( cFilter ! = null & & ! cFilter . trim ( ) . isEmpty ( ) ) {
consoleFilter = Collections . singletonList ( Pattern . compile ( cFilter ) ) ;
} else {
consoleFilter = Collections . emptyList ( ) ;
}
} catch ( PatternSyntaxException e ) {
plugin . getLogger ( ) . log ( java . util . logging . Level . WARNING , " Invalid pattern for \" console.log-level \" : " + e . getMessage ( ) ) ;
consoleFilter = Collections . emptyList ( ) ;
}
}
2021-07-01 15:43:35 +02:00
consoleFormat = generateMessageFormat ( getFormatString ( " .console.format " ) , " [{timestamp} {level}] {message} " , false ,
" timestamp " , " level " , " message " ) ;
discordToMcFormat = generateMessageFormat ( getFormatString ( " discord-to-mc " ) , " &6[#{channel}] &3{fullname}&7: &f{message} " , true ,
2021-07-07 17:55:25 +02:00
" channel " , " username " , " discriminator " , " fullname " , " nickname " , " color " , " message " , " role " ) ;
2021-07-01 15:43:35 +02:00
unmuteFormat = generateMessageFormat ( getFormatString ( " unmute " ) , " {displayname} unmuted. " , false , " username " , " displayname " ) ;
tempMuteFormat = generateMessageFormat ( getFormatString ( " temporary-mute " ) , " {controllerdisplayname} has muted player {displayname} for {time}. " , false ,
" username " , " displayname " , " controllername " , " controllerdisplayname " , " time " ) ;
permMuteFormat = generateMessageFormat ( getFormatString ( " permanent-mute " ) , " {controllerdisplayname} permanently muted {displayname}. " , false ,
" username " , " displayname " , " controllername " , " controllerdisplayname " ) ;
tempMuteReasonFormat = generateMessageFormat ( getFormatString ( " temporary-mute-reason " ) , " {controllerdisplayname} has muted player {displayname} for {time}. Reason: {reason}. " , false ,
" username " , " displayname " , " controllername " , " controllerdisplayname " , " time " , " reason " ) ;
permMuteReasonFormat = generateMessageFormat ( getFormatString ( " permanent-mute-reason " ) , " {controllerdisplayname} has muted player {displayname}. Reason: {reason}. " , false ,
" username " , " displayname " , " controllername " , " controllerdisplayname " , " reason " ) ;
kickFormat = generateMessageFormat ( getFormatString ( " kick " ) , " {displayname} was kicked with reason: {reason} " , false ,
" username " , " displayname " , " reason " ) ;
plugin . onReload ( ) ;
}
}