Skip to content

Commit

Permalink
Allow configuring Discord webhook name (#5159)
Browse files Browse the repository at this point in the history
Co-authored-by: Josh Roy <10731363+JRoy@users.noreply.github.com>
  • Loading branch information
diademiemi and JRoy committed Apr 10, 2023
1 parent 77dc87b commit 384f63b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,36 @@ public MessageFormat getMcToDiscordFormat(Player player) {
"username", "displayname", "message", "world", "prefix", "suffix");
}

public String getLegacyNameFormat() {
// For compatibility with old configs
if (isShowDisplayName()) {
return "{displayname}";
} else if (isShowName()) {
return "{username}";
} else {
// Will default to "{botname}" in the format
return null;
}
}

public MessageFormat getMcToDiscordNameFormat(Player player) {
String format = getFormatString("mc-to-discord-name-format");
if (format == null) {
format = getLegacyNameFormat();
}
final String filled;

if (plugin.isPAPI() && format != null) {
filled = me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, format);
} else {
filled = format;
}

return generateMessageFormat(filled, "{botname}", false,
"username", "displayname", "world", "prefix", "suffix", "botname");

}

public MessageFormat getTempMuteFormat() {
return tempMuteFormat;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,16 @@ public void sendChatMessage(final Player player, final String chatMessage) {
MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripEssentialsFormat(getPlugin().getEss().getPermissionsHandler().getSuffix(player))));

final String avatarUrl = DiscordUtil.getAvatarUrl(this, player);
final String name = getSettings().isShowName() ? player.getName() : (getSettings().isShowDisplayName() ? player.getDisplayName() : null);

DiscordUtil.dispatchDiscordMessage(this, MessageType.DefaultTypes.CHAT, formattedMessage, user.isAuthorized("essentials.discord.ping"), avatarUrl, name, player.getUniqueId());
final String formattedName = MessageUtil.formatMessage(getSettings().getMcToDiscordNameFormat(player),
MessageUtil.sanitizeDiscordMarkdown(player.getName()),
MessageUtil.sanitizeDiscordMarkdown(player.getDisplayName()),
MessageUtil.sanitizeDiscordMarkdown(getPlugin().getEss().getSettings().getWorldAlias(player.getWorld().getName())),
MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripEssentialsFormat(getPlugin().getEss().getPermissionsHandler().getPrefix(player))),
MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripEssentialsFormat(getPlugin().getEss().getPermissionsHandler().getSuffix(player))),
guild.getMember(jda.getSelfUser()).getEffectiveName());

DiscordUtil.dispatchDiscordMessage(this, MessageType.DefaultTypes.CHAT, formattedMessage, user.isAuthorized("essentials.discord.ping"), avatarUrl, formattedName, player.getUniqueId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.earth2me.essentials.Console;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.FormatUtil;
import com.earth2me.essentials.utils.VersionUtil;
import net.ess3.api.IUser;
import net.ess3.api.events.AfkStatusChangeEvent;
Expand Down Expand Up @@ -271,11 +272,13 @@ private void sendDiscordMessage(final MessageType messageType, final String mess
avatarUrl = DiscordUtil.getAvatarUrl(jda, player);
}

if (jda.getSettings().isShowName()) {
name = player.getName();
} else if (jda.getSettings().isShowDisplayName()) {
name = player.getDisplayName();
}
name = MessageUtil.formatMessage(jda.getSettings().getMcToDiscordNameFormat(player),
MessageUtil.sanitizeDiscordMarkdown(player.getName()),
MessageUtil.sanitizeDiscordMarkdown(player.getDisplayName()),
MessageUtil.sanitizeDiscordMarkdown(jda.getPlugin().getEss().getSettings().getWorldAlias(player.getWorld().getName())),
MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripEssentialsFormat(jda.getPlugin().getEss().getPermissionsHandler().getPrefix(player))),
MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripEssentialsFormat(jda.getPlugin().getEss().getPermissionsHandler().getSuffix(player))),
jda.getGuild().getMember(jda.getJda().getSelfUser()).getEffectiveName());

uuid = player.getUniqueId();
}
Expand Down
15 changes: 10 additions & 5 deletions EssentialsDiscord/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,6 @@ show-avatar: false
# To include the UUID of the player in this URL, use "{uuid}".
# To include the name of the player in this URL, use "{name}".
avatar-url: "https://crafthead.net/helm/{uuid}"
# Whether or not player messages should show their name as the bot name in Discord.
show-name: false
# Whether or not player messages should show their display-name/nickname as the bot name in Discord.
# You must disable show-name for this option to work.
show-displayname: false

# Whether or not fake join and leave messages should be sent to Discord when a player toggles vanish in Minecraft.
# Fake join/leave messages will be sent the same as real join and leave messages (and to the same channel).
Expand Down Expand Up @@ -263,6 +258,16 @@ messages:
# - {suffix}: The suffix of the player sending the message
# ... PlaceholderAPI placeholders are also supported here too!
mc-to-discord: "{displayname}: {message}"
# This is the bot's name which appears in Discord when sending player-specific messages.
# The following placeholders can be used here:
# - {username}: The username of the player sending the message
# - {displayname}: The display name of the player sending the message (This would be their nickname)
# - {world}: The name of the world the player sending the message is in
# - {prefix}: The prefix of the player sending the message
# - {suffix}: The suffix of the player sending the message
# - {botname}: Name of the Discord bot
# ... PlaceholderAPI placeholders are also supported here too!
mc-to-discord-name-format: "{botname}"
# This is the message sent to Discord when a player is temporarily muted in minecraft.
# The following placeholders can be used here:
# - {username}: The username of the player being muted
Expand Down

0 comments on commit 384f63b

Please sign in to comment.