Skip to content

Commit 384f63b

Browse files
x86-39JRoy
andauthored
Allow configuring Discord webhook name (#5159)
Co-authored-by: Josh Roy <10731363+JRoy@users.noreply.github.com>
1 parent 77dc87b commit 384f63b

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,36 @@ public MessageFormat getMcToDiscordFormat(Player player) {
226226
"username", "displayname", "message", "world", "prefix", "suffix");
227227
}
228228

229+
public String getLegacyNameFormat() {
230+
// For compatibility with old configs
231+
if (isShowDisplayName()) {
232+
return "{displayname}";
233+
} else if (isShowName()) {
234+
return "{username}";
235+
} else {
236+
// Will default to "{botname}" in the format
237+
return null;
238+
}
239+
}
240+
241+
public MessageFormat getMcToDiscordNameFormat(Player player) {
242+
String format = getFormatString("mc-to-discord-name-format");
243+
if (format == null) {
244+
format = getLegacyNameFormat();
245+
}
246+
final String filled;
247+
248+
if (plugin.isPAPI() && format != null) {
249+
filled = me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, format);
250+
} else {
251+
filled = format;
252+
}
253+
254+
return generateMessageFormat(filled, "{botname}", false,
255+
"username", "displayname", "world", "prefix", "suffix", "botname");
256+
257+
}
258+
229259
public MessageFormat getTempMuteFormat() {
230260
return tempMuteFormat;
231261
}

EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,16 @@ public void sendChatMessage(final Player player, final String chatMessage) {
278278
MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripEssentialsFormat(getPlugin().getEss().getPermissionsHandler().getSuffix(player))));
279279

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

283-
DiscordUtil.dispatchDiscordMessage(this, MessageType.DefaultTypes.CHAT, formattedMessage, user.isAuthorized("essentials.discord.ping"), avatarUrl, name, player.getUniqueId());
282+
final String formattedName = MessageUtil.formatMessage(getSettings().getMcToDiscordNameFormat(player),
283+
MessageUtil.sanitizeDiscordMarkdown(player.getName()),
284+
MessageUtil.sanitizeDiscordMarkdown(player.getDisplayName()),
285+
MessageUtil.sanitizeDiscordMarkdown(getPlugin().getEss().getSettings().getWorldAlias(player.getWorld().getName())),
286+
MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripEssentialsFormat(getPlugin().getEss().getPermissionsHandler().getPrefix(player))),
287+
MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripEssentialsFormat(getPlugin().getEss().getPermissionsHandler().getSuffix(player))),
288+
guild.getMember(jda.getSelfUser()).getEffectiveName());
289+
290+
DiscordUtil.dispatchDiscordMessage(this, MessageType.DefaultTypes.CHAT, formattedMessage, user.isAuthorized("essentials.discord.ping"), avatarUrl, formattedName, player.getUniqueId());
284291
}
285292

286293
@Override

EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/BukkitListener.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.earth2me.essentials.Console;
44
import com.earth2me.essentials.utils.DateUtil;
5+
import com.earth2me.essentials.utils.FormatUtil;
56
import com.earth2me.essentials.utils.VersionUtil;
67
import net.ess3.api.IUser;
78
import net.ess3.api.events.AfkStatusChangeEvent;
@@ -271,11 +272,13 @@ private void sendDiscordMessage(final MessageType messageType, final String mess
271272
avatarUrl = DiscordUtil.getAvatarUrl(jda, player);
272273
}
273274

274-
if (jda.getSettings().isShowName()) {
275-
name = player.getName();
276-
} else if (jda.getSettings().isShowDisplayName()) {
277-
name = player.getDisplayName();
278-
}
275+
name = MessageUtil.formatMessage(jda.getSettings().getMcToDiscordNameFormat(player),
276+
MessageUtil.sanitizeDiscordMarkdown(player.getName()),
277+
MessageUtil.sanitizeDiscordMarkdown(player.getDisplayName()),
278+
MessageUtil.sanitizeDiscordMarkdown(jda.getPlugin().getEss().getSettings().getWorldAlias(player.getWorld().getName())),
279+
MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripEssentialsFormat(jda.getPlugin().getEss().getPermissionsHandler().getPrefix(player))),
280+
MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripEssentialsFormat(jda.getPlugin().getEss().getPermissionsHandler().getSuffix(player))),
281+
jda.getGuild().getMember(jda.getJda().getSelfUser()).getEffectiveName());
279282

280283
uuid = player.getUniqueId();
281284
}

EssentialsDiscord/src/main/resources/config.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,6 @@ show-avatar: false
147147
# To include the UUID of the player in this URL, use "{uuid}".
148148
# To include the name of the player in this URL, use "{name}".
149149
avatar-url: "https://crafthead.net/helm/{uuid}"
150-
# Whether or not player messages should show their name as the bot name in Discord.
151-
show-name: false
152-
# Whether or not player messages should show their display-name/nickname as the bot name in Discord.
153-
# You must disable show-name for this option to work.
154-
show-displayname: false
155150

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

0 commit comments

Comments
 (0)