-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscordWebhookService.java
More file actions
147 lines (133 loc) · 4.82 KB
/
DiscordWebhookService.java
File metadata and controls
147 lines (133 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package dansplugins.activitytracker.services;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import dansplugins.activitytracker.utils.Logger;
/**
* Service for sending Discord webhook notifications when players join or leave the server.
* @author Daniel McCoy Stephenson
*/
public class DiscordWebhookService {
private final ConfigService configService;
private final Logger logger;
public DiscordWebhookService(ConfigService configService, Logger logger) {
this.configService = configService;
this.logger = logger;
}
/**
* Checks if the Discord webhook feature is enabled and configured.
* @return true if enabled and a webhook URL is set.
*/
public boolean isEnabled() {
if (!configService.getBoolean("discordWebhookEnabled")) {
return false;
}
String url = configService.getString("discordWebhookUrl");
return url != null && !url.isEmpty();
}
/**
* Checks if webhooks should only fire for staff members.
* @return true if staff-only mode is active.
*/
public boolean isStaffOnly() {
return configService.getBoolean("discordWebhookStaffOnly");
}
/**
* Sends a player join notification to the configured Discord webhook.
* @param playerName The name of the player who joined.
*/
public void sendJoinNotification(String playerName) {
String template = configService.getString("discordWebhookJoinMessage");
if (template == null || template.isEmpty()) {
return;
}
String message = template.replace("{player}", playerName);
sendWebhookMessage(message);
}
/**
* Sends a player quit notification to the configured Discord webhook.
* @param playerName The name of the player who quit.
*/
public void sendQuitNotification(String playerName) {
String template = configService.getString("discordWebhookQuitMessage");
if (template == null || template.isEmpty()) {
return;
}
String message = template.replace("{player}", playerName);
sendWebhookMessage(message);
}
/**
* Sends a message to the configured Discord webhook URL.
* @param content The message content to send.
*/
private void sendWebhookMessage(String content) {
String webhookUrl = configService.getString("discordWebhookUrl");
if (webhookUrl == null || webhookUrl.isEmpty()) {
return;
}
try {
URL url = new URL(webhookUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setConnectTimeout(5000);
connection.setReadTimeout(10000);
connection.setDoOutput(true);
String jsonPayload = "{\"content\": \"" + escapeJson(content) + "\"}";
byte[] input = jsonPayload.getBytes(StandardCharsets.UTF_8);
OutputStream os = connection.getOutputStream();
try {
os.write(input, 0, input.length);
} finally {
os.close();
}
int responseCode = connection.getResponseCode();
if (responseCode < 200 || responseCode >= 300) {
logger.log("Discord webhook returned error code: " + responseCode);
}
} catch (IOException e) {
logger.log("Failed to send Discord webhook message: " + e.getMessage());
}
}
/**
* Escapes special characters for JSON string values.
* @param text The text to escape.
* @return The escaped text safe for JSON inclusion.
*/
private String escapeJson(String text) {
if (text == null) {
return "";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
switch (c) {
case '"':
sb.append("\\\"");
break;
case '\\':
sb.append("\\\\");
break;
case '\n':
sb.append("\\n");
break;
case '\r':
sb.append("\\r");
break;
case '\t':
sb.append("\\t");
break;
default:
if (c < 0x20) {
sb.append(String.format("\\u%04x", c));
} else {
sb.append(c);
}
break;
}
}
return sb.toString();
}
}