Skip to content

Commit a81f101

Browse files
add config to exclude specific servers, properly shutdown jda, remove deprecated discriminator placeholder, improve config comments
1 parent 420da25 commit a81f101

File tree

6 files changed

+95
-13
lines changed

6 files changed

+95
-13
lines changed

README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Chat from all servers gets bridged with a discord channel
2626
7. Under `[discord]`, paste the channel id you want to use
2727
- To get a channel id, you have to enable developer mode in Discord
2828
- Open Discord settings, go to `Advanced`, then turn on `Developer Mode`
29-
- Now right click the channel you want to use and click `Copy ID`
29+
- Now right-click the channel you want to use and click `Copy ID`
3030
8. Set any additional config options you want
3131
9. Start the server and check if it works
3232

@@ -36,7 +36,11 @@ Default config generated on startup:
3636

3737
```toml
3838
# Don't change this
39-
config_version = "1.7"
39+
config_version = "1.8"
40+
41+
# Comma separated list of server names to exclude from the bridge (defined under [servers] inside your velocity.toml)
42+
# e.g., exclude_servers = ["lobby", "survival"]
43+
exclude_servers = []
4044

4145
[discord]
4246
# Bot token from https://discordapp.com/developers/applications/
@@ -52,6 +56,7 @@ show_attachments_ingame = true
5256
# Show a text as playing activity of the bot
5357
show_activity = true
5458
# Activity text of the bot to show in Discord
59+
# Placeholders {amount} is available
5560
activity_text = "with {amount} players online"
5661

5762
# Enable mentioning Discord users from Minecraft chat
@@ -79,25 +84,33 @@ webhook_username = "{username}"
7984
[discord.chat]
8085
# The format set in the following key "discord.chat.message" is ignored when a webhook is used
8186
# Placeholders {username}, {server}, and {message} are available
87+
# Can be disabled
8288
message = "{username}: {message}"
8389
# Placeholders {username} and {server} are available
90+
# Can be disabled
8491
join_message = "**{username} joined the game**"
8592
leave_message = "**{username} left the game**"
8693
# Possible different format for timeouts or other terminating connections
87-
# Only placeholder {username} is available
94+
# Placeholder {username} is available
95+
# Can be disabled
8896
disconnect_message = "**{username} disconnected**"
8997
# Placeholders {username}, {current}, and {previous} are available
98+
# Can be disabled
9099
server_switch_message = "**{username} moved to {current} from {previous}**"
91100
# Placeholders {username} and {death_message} are available
101+
# Can be disabled
92102
death_message = "**{username} {death_message}**"
93103
# Placeholders {username}, {advancement_title}, and {advancement_description} are available
104+
# Can be disabled
94105
advancement_message = "**{username} has made the advancement __{advancement_title}__**\n_{advancement_description}_"
95106

96107
[discord.commands.list]
97108
enabled = true
98109
# Ephemeral messages are only visible to the user who sent the command
99110
ephemeral = true
111+
# Placeholders {server_name}, {online_players} and {max_players} are available
100112
server_format = "[{server_name} {online_players}/{max_players}]"
113+
# Placeholder {username} is available
101114
player_format = "- {username}"
102115
# Can be disabled
103116
no_players = "No players online"
@@ -108,9 +121,13 @@ codeblock_lang = "asciidoc"
108121
# Discord > Minecraft message formats
109122
# Uses XML-like formatting with https://docs.adventure.kyori.net/minimessage#format
110123
[minecraft]
124+
# Placeholder {discord} is available
111125
discord_chunk = "<dark_gray>[<{discord_color}>Discord<dark_gray>]<reset>"
112-
username_chunk = "<{role_color}><hover:show_text:{username}#{discriminator}>{nickname}</hover><reset>"
126+
# Placeholders {role_color}, {username} and {nickname} are available
127+
username_chunk = "<{role_color}><hover:show_text:{username}>{nickname}</hover><reset>"
128+
# Placeholders {discord_chunk}, {username_chunk}, {attachments} and {message} are available
113129
message = "{discord_chunk} {username_chunk}<dark_gray>: <reset>{message} {attachments}"
130+
# Placeholders {url} and {attachment_color} are available
114131
attachments = "<dark_gray><click:open_url:{url}>[<{attachment_color}>Attachment<dark_gray>]</click><reset>"
115132
# Colors for the <{discord_color}> and <{attachment_color}> tags
116133
discord_color = "#7289da"

src/main/java/ooo/foooooooooooo/velocitydiscord/VelocityDiscord.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.inject.Inject;
44
import com.velocitypowered.api.event.Subscribe;
55
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
6+
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
67
import com.velocitypowered.api.plugin.Plugin;
78
import com.velocitypowered.api.plugin.annotation.DataDirectory;
89
import com.velocitypowered.api.proxy.ProxyServer;
@@ -26,7 +27,7 @@
2627
public class VelocityDiscord {
2728
public static final String PluginName = "Velocity Discord Bridge";
2829
public static final String PluginDescription = "Velocity Discord Chat Bridge";
29-
public static final String PluginVersion = "1.7.0";
30+
public static final String PluginVersion = "1.8.0";
3031
public static final String PluginUrl = "https://github.com/fooooooooooooooo/VelocityDiscord";
3132

3233
public static final MinecraftChannelIdentifier YepIdentifier = MinecraftChannelIdentifier.create("velocity", "yep");
@@ -72,13 +73,21 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
7273
if (discord != null) {
7374
register(discord);
7475
}
76+
7577
if (yep != null) {
7678
register(yep);
7779
}
7880

7981
this.server.getChannelRegistrar().register(YepIdentifier);
8082
}
8183

84+
@Subscribe
85+
public void onProxyShutdown(ProxyShutdownEvent event) {
86+
if (discord != null) {
87+
discord.shutdown();
88+
}
89+
}
90+
8291
private void register(Object x) {
8392
this.server.getEventManager().register(this, x);
8493
}

src/main/java/ooo/foooooooooooo/velocitydiscord/config/Config.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
import java.io.IOException;
99
import java.nio.file.Files;
1010
import java.nio.file.Path;
11+
import java.util.ArrayList;
12+
import java.util.List;
1113
import java.util.Objects;
1214

1315
import static ooo.foooooooooooo.velocitydiscord.VelocityDiscord.PluginVersion;
14-
import static ooo.foooooooooooo.velocitydiscord.config.BaseConfig.get;
1516

16-
public class Config {
17+
public class Config extends BaseConfig {
1718
private static final String[] splitVersion = PluginVersion.split("\\.");
1819
private static final String configVersion = splitVersion[0] + '.' + splitVersion[1];
1920
private static final String configMajorVersion = splitVersion[0];
@@ -28,12 +29,16 @@ public class Config {
2829

2930
public ListCommandConfig listCommand;
3031

32+
public List<String> EXCLUDED_SERVERS = new ArrayList<>();
33+
3134
@Inject
3235
public Config(@DataDirectory Path dataDir) {
3336
this.dataDir = dataDir;
3437

3538
var config = loadFile();
3639

40+
loadConfig(config);
41+
3742
bot = new BotConfig(config);
3843
discord = new DiscordMessageConfig(config);
3944
minecraft = new MinecraftMessageConfig(config);
@@ -85,4 +90,13 @@ private boolean versionCompatible(String newVersion) {
8590
public boolean isFirstRun() {
8691
return bot.isDefaultValues() || configCreatedThisRun;
8792
}
93+
94+
@Override
95+
protected void loadConfig(com.electronwill.nightconfig.core.Config config) {
96+
EXCLUDED_SERVERS = get(config, "exclude_servers", EXCLUDED_SERVERS);
97+
}
98+
99+
public boolean serverDisabled(String name) {
100+
return EXCLUDED_SERVERS.contains(name);
101+
}
88102
}

src/main/java/ooo/foooooooooooo/velocitydiscord/discord/Discord.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ public void onReady(@Nonnull ReadyEvent event) {
100100
updateActivityPlayerAmount();
101101
}
102102

103+
public void shutdown() {
104+
jda.shutdown();
105+
}
106+
103107
@Subscribe(order = PostOrder.FIRST)
104108
public void onPlayerChat(PlayerChatEvent event) {
105109
var currentServer = event.getPlayer().getCurrentServer();
@@ -108,8 +112,13 @@ public void onPlayerChat(PlayerChatEvent event) {
108112
return;
109113
}
110114

111-
var username = event.getPlayer().getUsername();
112115
var server = currentServer.get().getServerInfo().getName();
116+
117+
if (config.serverDisabled(server)) {
118+
return;
119+
}
120+
121+
var username = event.getPlayer().getUsername();
113122
var content = event.getMessage();
114123

115124
if (config.bot.ENABLE_MENTIONS) {
@@ -151,15 +160,26 @@ public void onConnect(ServerConnectedEvent event) {
151160
var username = event.getPlayer().getUsername();
152161
var server = event.getServer().getServerInfo().getName();
153162

163+
if (config.serverDisabled(server)) {
164+
updateActivityPlayerAmount();
165+
166+
return;
167+
}
168+
154169
var previousServer = event.getPreviousServer();
155170

156171
StringTemplate message = null;
157172

158173
if (previousServer.isPresent()) {
159174
if (config.discord.SERVER_SWITCH_MESSAGE_FORMAT.isPresent()) {
160-
161175
var previous = previousServer.get().getServerInfo().getName();
162176

177+
if (config.serverDisabled(previous)) {
178+
updateActivityPlayerAmount();
179+
180+
return;
181+
}
182+
163183
message = new StringTemplate(config.discord.SERVER_SWITCH_MESSAGE_FORMAT.get())
164184
.add("username", username)
165185
.add("current", server)
@@ -185,6 +205,12 @@ public void onDisconnect(DisconnectEvent event) {
185205
var username = event.getPlayer().getUsername();
186206
var server = currentServer.map(serverConnection -> serverConnection.getServerInfo().getName()).orElse("null");
187207

208+
if (config.serverDisabled(server)) {
209+
updateActivityPlayerAmount();
210+
211+
return;
212+
}
213+
188214
String template = currentServer.isPresent() ? config.discord.LEAVE_MESSAGE_FORMAT.orElse(null) : config.discord.DISCONNECT_MESSAGE_FORMAT.orElse(null);
189215

190216
if (template != null) {

src/main/java/ooo/foooooooooooo/velocitydiscord/discord/MessageListener.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
8787
var username_chunk = new StringTemplate(config.minecraft.USERNAME_CHUNK_FORMAT)
8888
.add("role_color", hex)
8989
.add("username", author.getName())
90-
.add("discriminator", author.getDiscriminator())
9190
.add("nickname", member.getEffectiveName()).toString();
9291

9392
var attachment_chunk = config.minecraft.ATTACHMENT_FORMAT;

src/main/resources/config.toml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Don't change this
2-
config_version = "1.7"
2+
config_version = "1.8"
3+
4+
# Comma separated list of server names to exclude from the bridge (defined under [servers] inside your velocity.toml)
5+
# e.g., exclude_servers = ["lobby", "survival"]
6+
exclude_servers = []
37

48
[discord]
59
# Bot token from https://discordapp.com/developers/applications/
@@ -15,6 +19,7 @@ show_attachments_ingame = true
1519
# Show a text as playing activity of the bot
1620
show_activity = true
1721
# Activity text of the bot to show in Discord
22+
# Placeholders {amount} is available
1823
activity_text = "with {amount} players online"
1924

2025
# Enable mentioning Discord users from Minecraft chat
@@ -42,25 +47,33 @@ webhook_username = "{username}"
4247
[discord.chat]
4348
# The format set in the following key "discord.chat.message" is ignored when a webhook is used
4449
# Placeholders {username}, {server}, and {message} are available
50+
# Can be disabled
4551
message = "{username}: {message}"
4652
# Placeholders {username} and {server} are available
53+
# Can be disabled
4754
join_message = "**{username} joined the game**"
4855
leave_message = "**{username} left the game**"
4956
# Possible different format for timeouts or other terminating connections
50-
# Only placeholder {username} is available
57+
# Placeholder {username} is available
58+
# Can be disabled
5159
disconnect_message = "**{username} disconnected**"
5260
# Placeholders {username}, {current}, and {previous} are available
61+
# Can be disabled
5362
server_switch_message = "**{username} moved to {current} from {previous}**"
5463
# Placeholders {username} and {death_message} are available
64+
# Can be disabled
5565
death_message = "**{username} {death_message}**"
5666
# Placeholders {username}, {advancement_title}, and {advancement_description} are available
67+
# Can be disabled
5768
advancement_message = "**{username} has made the advancement __{advancement_title}__**\n_{advancement_description}_"
5869

5970
[discord.commands.list]
6071
enabled = true
6172
# Ephemeral messages are only visible to the user who sent the command
6273
ephemeral = true
74+
# Placeholders {server_name}, {online_players} and {max_players} are available
6375
server_format = "[{server_name} {online_players}/{max_players}]"
76+
# Placeholder {username} is available
6477
player_format = "- {username}"
6578
# Can be disabled
6679
no_players = "No players online"
@@ -71,9 +84,13 @@ codeblock_lang = "asciidoc"
7184
# Discord > Minecraft message formats
7285
# Uses XML-like formatting with https://docs.adventure.kyori.net/minimessage#format
7386
[minecraft]
87+
# Placeholder {discord} is available
7488
discord_chunk = "<dark_gray>[<{discord_color}>Discord<dark_gray>]<reset>"
75-
username_chunk = "<{role_color}><hover:show_text:{username}#{discriminator}>{nickname}</hover><reset>"
89+
# Placeholders {role_color}, {username} and {nickname} are available
90+
username_chunk = "<{role_color}><hover:show_text:{username}>{nickname}</hover><reset>"
91+
# Placeholders {discord_chunk}, {username_chunk}, {attachments} and {message} are available
7692
message = "{discord_chunk} {username_chunk}<dark_gray>: <reset>{message} {attachments}"
93+
# Placeholders {url} and {attachment_color} are available
7794
attachments = "<dark_gray><click:open_url:{url}>[<{attachment_color}>Attachment<dark_gray>]</click><reset>"
7895
# Colors for the <{discord_color}> and <{attachment_color}> tags
7996
discord_color = "#7289da"

0 commit comments

Comments
 (0)