Skip to content

Commit 8a55cb8

Browse files
committed
Matching commands precisely.
Only matching the command not whole message; Commands now sending to Slack with quote '```'; Actions (e.g. login, logout) now sending to Slack with italic; Normal messages now sending to Slack without mrkdwn parsing.
1 parent d47afdb commit 8a55cb8

File tree

6 files changed

+63
-15
lines changed

6 files changed

+63
-15
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>us.circuitsoft.slack</groupId>
55
<artifactId>Slack</artifactId>
6-
<version>1.4.4</version>
6+
<version>1.5.0</version>
77
<packaging>jar</packaging>
88
<properties>
99
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

src/main/java/org/circuitsoft/slack/api/BukkitPoster.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,45 @@ public class BukkitPoster extends BukkitRunnable {
1919
private final String message;
2020
private final String webhookUrl = getWebhookUrl();
2121
private final String iconUrl;
22+
private final Boolean isAction;
2223

2324
/**
2425
* Prepares the message to send to Slack.
2526
*
2627
* @param message The message to send to Slack.
2728
* @param name The username of the message to send to Slack.
2829
* @param iconUrl The image URL of the user that sends the message to Slack. Make this null if the username is a Minecraft player name.
30+
* @param isAction The message is an action or not.
2931
*/
30-
public BukkitPoster(String message, String name, String iconUrl) {
32+
public BukkitPoster(String message, String name, String iconUrl, Boolean isAction) {
3133
this.name = name;
3234
this.message = message;
3335
this.iconUrl = iconUrl;
36+
this.isAction = isAction;
37+
}
38+
39+
/**
40+
* Prepares the message to send to Slack.
41+
*
42+
* @param message The message to send to Slack.
43+
* @param name The username of the message to send to Slack.
44+
* @param iconUrl The image URL of the user that sends the message to Slack. Make this null if the username is a Minecraft player name.
45+
*/
46+
public BukkitPoster(String message, String name, String iconUrl) {
47+
this(message, name, iconUrl, false);
3448
}
3549

3650
@Override
3751
public void run() {
3852
JSONObject json = new JSONObject();
39-
json.put("text", name + ": " + message);
53+
if (isAction) {
54+
json.put("text", "_" + message + "_");
55+
} else if (message.startsWith("/")) {
56+
json.put("text", "```" + message + "```");
57+
} else {
58+
json.put("text", message);
59+
json.put("mrkdwn", false);
60+
}
4061
json.put("username", name);
4162
if (iconUrl == null) {
4263
json.put("icon_url", "https://cravatar.eu/helmhead/" + name + "/100.png");

src/main/java/org/circuitsoft/slack/bukkit/SlackBukkit.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,49 @@ public void onDisable() {
4343
@EventHandler(priority = EventPriority.MONITOR)
4444
public void onChat(AsyncPlayerChatEvent event) {
4545
if (isVisible("slack.hide.chat", event.getPlayer())) {
46-
send('"' + event.getMessage() + '"', event.getPlayer().getName());
46+
send(event.getMessage(), event.getPlayer().getName());
4747
}
4848
}
4949

5050
@EventHandler(priority = EventPriority.MONITOR)
5151
public void onLogin(PlayerJoinEvent event) {
5252
if (isVisible("slack.hide.login", event.getPlayer().getUniqueId())) {
53-
send("logged in", event.getPlayer().getName());
53+
send("logged in", event.getPlayer().getName(), true);
5454
}
5555
}
5656

5757
@EventHandler(priority = EventPriority.MONITOR)
5858
public void onQuit(PlayerQuitEvent event) {
5959
if (isVisible("slack.hide.logout", event.getPlayer())) {
60-
send("logged out", event.getPlayer().getName());
60+
send("logged out", event.getPlayer().getName(), true);
6161
}
6262
}
6363

6464
@EventHandler(priority = EventPriority.MONITOR)
6565
public void onCommand(PlayerCommandPreprocessEvent event) {
66-
if (isAllowed(event.getMessage()) && isVisible("slack.hide.command", event.getPlayer()) && !event.getMessage().contains("/slack send")) {
66+
if (!getConfig().getBoolean("send-commands")) {
67+
return;
68+
}
69+
String command = event.getMessage().split(" ")[0];
70+
if (isAllowed(command) && isVisible("slack.hide.command", event.getPlayer()) && !event.getMessage().contains("/slack send")) {
6771
send(event.getMessage(), event.getPlayer().getName());
6872
}
6973
}
7074

7175
public void send(String message, String name) {
72-
new SlackBukkitPoster(this, message, name, null).runTaskAsynchronously(this);
76+
send(message, name, null, false);
7377
}
7478

7579
public void send(String message, String name, String iconUrl) {
76-
new SlackBukkitPoster(this, message, name, iconUrl).runTaskAsynchronously(this);
80+
send(message, name, iconUrl, false);
81+
}
82+
83+
public void send(String message, String name, Boolean isAction) {
84+
send(message, name, null, isAction);
85+
}
86+
87+
public void send(String message, String name, String iconUrl, Boolean isAction) {
88+
new SlackBukkitPoster(this, message, name, iconUrl, isAction).runTaskAsynchronously(this);
7789
}
7890

7991
private boolean isAllowed(String command) {
@@ -156,7 +168,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
156168
sender.sendMessage(ChatColor.DARK_RED + "You are not allowed to execute this command!");
157169
}
158170
} else {
159-
sender.sendMessage(ChatColor.GOLD + "/slack send <username> <image URL or null for username's skin> <message> - send a custom message to slack \n/slack reload - reload Slack's config");
171+
sender.sendMessage(ChatColor.GOLD + "/slack send <username> <image URL or null for username's skin> <message> - send a custom message to slack\n/slack reload - reload Slack's config");
160172
}
161173
return true;
162174
}

src/main/java/org/circuitsoft/slack/bukkit/SlackBukkitPoster.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,31 @@ public class SlackBukkitPoster extends BukkitRunnable {
2121
private final String message;
2222
private final String webhookUrl = getWebhookUrl();
2323
private final String iconUrl;
24+
private final Boolean isAction;
2425

2526
public SlackBukkitPoster(JavaPlugin plugin, String message, String name, String iconUrl) {
27+
this(plugin, message, name, iconUrl, false);
28+
}
29+
30+
public SlackBukkitPoster(JavaPlugin plugin, String message, String name, String iconUrl, Boolean isAction) {
2631
this.plugin = plugin;
2732
this.name = name;
2833
this.message = message;
2934
this.iconUrl = iconUrl;
35+
this.isAction = isAction;
3036
}
3137

3238
@Override
3339
public void run() {
3440
JSONObject json = new JSONObject();
35-
json.put("text", name + ": " + message);
41+
if (isAction) {
42+
json.put("text", "_" + message + "_");
43+
} else if (message.startsWith("/")) {
44+
json.put("text", "```" + message + "```");
45+
} else {
46+
json.put("text", message);
47+
json.put("mrkdwn", false);
48+
}
3649
json.put("username", name);
3750
if (iconUrl == null) {
3851
json.put("icon_url", "https://cravatar.eu/helmhead/" + name + "/100.png");

src/main/resources/config.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#The version. Do not touch this.
2-
version: 1.4.4
2+
version: 1.5.0
33
#The incoming webhook URL you got from Slack.
44
webhook: https://hooks.slack.com/services/
55
#Show HTTP response codes in console
66
debug: false
7-
#Causes a bit of lag but you can hvae certain players not post to Slack
7+
#Send player commands to Slack
8+
send-commands: false
9+
#Causes a bit of lag but you can have certain players not post to Slack
810
use-perms: false
911
#Causes lag but you can block commands from being shown to Slack
1012
use-blacklist: false

src/main/resources/plugin.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name: Slack
2-
version: 1.4.4
2+
version: 1.5.0
33
description: Link your server to Slack!
44
author: CircuitSoft
5-
authors: [Janmm14, LaxWasHere, it3d]
5+
authors: [Janmm14, LaxWasHere, it3d, Davy]
66
website: https://circuitsoft.us
77

88
main: org.circuitsoft.slack.bukkit.SlackBukkit

0 commit comments

Comments
 (0)