Skip to content

Commit d7f7b4f

Browse files
Add Discord webhook testing scripts (bash and python)
Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com>
1 parent ca99f22 commit d7f7b4f

File tree

3 files changed

+450
-0
lines changed

3 files changed

+450
-0
lines changed

DISCORD_TESTING.md

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Discord Webhook Testing Scripts
2+
3+
This directory contains scripts to test Discord webhook notifications without running a Minecraft server.
4+
5+
## Prerequisites
6+
7+
### Bash Script (`test-discord-webhook.sh`)
8+
- Bash shell
9+
- Java installed (any version with `javac` and `java` commands)
10+
11+
### Python Script (`test-discord-webhook.py`)
12+
- Python 3.x installed
13+
14+
## Getting a Discord Webhook URL
15+
16+
1. Go to your Discord server settings
17+
2. Navigate to **Integrations****Webhooks**
18+
3. Click **New Webhook**
19+
4. Configure the webhook:
20+
- Set a name (e.g., "Herald Bot")
21+
- Choose the channel where notifications should be sent
22+
- Copy the webhook URL
23+
5. Keep this URL secure - don't share it publicly!
24+
25+
## Usage
26+
27+
### Bash Script
28+
29+
```bash
30+
# Basic usage (uses default player and server names)
31+
./test-discord-webhook.sh https://discord.com/api/webhooks/123456/abcdef
32+
33+
# With custom player name
34+
./test-discord-webhook.sh https://discord.com/api/webhooks/123456/abcdef Steve
35+
36+
# With custom player and server names
37+
./test-discord-webhook.sh https://discord.com/api/webhooks/123456/abcdef Steve "My Awesome Server"
38+
```
39+
40+
### Python Script
41+
42+
```bash
43+
# Basic usage (uses default player and server names)
44+
python3 test-discord-webhook.py https://discord.com/api/webhooks/123456/abcdef
45+
46+
# With custom player name
47+
python3 test-discord-webhook.py https://discord.com/api/webhooks/123456/abcdef Steve
48+
49+
# With custom player and server names
50+
python3 test-discord-webhook.py https://discord.com/api/webhooks/123456/abcdef Steve "My Awesome Server"
51+
```
52+
53+
## What the Scripts Do
54+
55+
These scripts simulate the Herald plugin's Discord notification by:
56+
57+
1. Taking a Discord webhook URL as input
58+
2. Formatting a message in the same way the plugin does: `**PlayerName** joined the **ServerName** server`
59+
3. Sending the message to Discord via HTTP POST request
60+
4. Reporting success or failure
61+
62+
The message will appear in your Discord channel exactly as it would when a player joins your Minecraft server.
63+
64+
## Example Output
65+
66+
### Successful Test
67+
```
68+
========================================
69+
Discord Webhook Test Script
70+
========================================
71+
72+
Testing Discord webhook...
73+
Webhook URL: https://discord.com/api/webhooks/123456/****
74+
Player Name: Steve
75+
Server Name: My Awesome Server
76+
77+
Sending test message...
78+
79+
Sending payload: {"content": "**Steve** joined the **My Awesome Server** server"}
80+
Response code: 204
81+
✓ SUCCESS: Message sent to Discord!
82+
Check your Discord channel for the notification.
83+
84+
========================================
85+
Test completed successfully!
86+
========================================
87+
```
88+
89+
### Failed Test
90+
```
91+
========================================
92+
Discord Webhook Test Script
93+
========================================
94+
95+
Testing Discord webhook...
96+
Webhook URL: https://discord.com/api/webhooks/invalid/****
97+
Player Name: Steve
98+
Server Name: Minecraft
99+
100+
Sending test message...
101+
102+
✗ ERROR: Failed to send message to Discord
103+
Error: Discord webhook returned error code: 404
104+
105+
========================================
106+
Test failed!
107+
========================================
108+
```
109+
110+
## Troubleshooting
111+
112+
### "Command not found" Error
113+
- **Bash script**: Make sure the script is executable: `chmod +x test-discord-webhook.sh`
114+
- **Python script**: Make sure the script is executable: `chmod +x test-discord-webhook.py`
115+
- **Java not found**: Install Java Development Kit (JDK)
116+
- **Python not found**: Install Python 3
117+
118+
### "Error code: 404" or "Error code: 401"
119+
- Your webhook URL is invalid or has been deleted
120+
- Create a new webhook and try again
121+
122+
### "Error code: 429"
123+
- You're being rate-limited by Discord
124+
- Wait a few seconds and try again
125+
126+
### No Message Appears in Discord
127+
- Check that you're looking at the correct channel
128+
- Verify the webhook is configured for the right channel
129+
- Make sure the webhook hasn't been deleted
130+
131+
## Security Note
132+
133+
**Never commit your actual webhook URL to version control!** These scripts mask the webhook token in output for security, but you should still keep your webhook URLs private.
134+
135+
## Integration with Herald Plugin
136+
137+
Once you've verified your webhook URL works with these scripts, you can configure it in your Herald plugin's `config.yml`:
138+
139+
```yaml
140+
discord:
141+
enabled: true
142+
webhook-url: "https://discord.com/api/webhooks/123456/abcdef"
143+
```
144+
145+
The plugin will send notifications in exactly the same format as these test scripts.

test-discord-webhook.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Discord Webhook Test Script
5+
This script allows you to test Discord webhook notifications without running a Minecraft server
6+
"""
7+
8+
import sys
9+
import json
10+
import urllib.request
11+
import urllib.error
12+
13+
def send_discord_message(webhook_url, content):
14+
"""Send a message to Discord via webhook"""
15+
if not webhook_url:
16+
raise ValueError("Discord webhook URL is not configured")
17+
18+
# Create JSON payload
19+
payload = {
20+
"content": content
21+
}
22+
23+
data = json.dumps(payload).encode('utf-8')
24+
25+
print(f"Sending payload: {json.dumps(payload)}")
26+
27+
# Create request
28+
req = urllib.request.Request(
29+
webhook_url,
30+
data=data,
31+
headers={'Content-Type': 'application/json'}
32+
)
33+
34+
# Send request
35+
try:
36+
with urllib.request.urlopen(req) as response:
37+
status_code = response.getcode()
38+
print(f"Response code: {status_code}")
39+
40+
if status_code < 200 or status_code >= 300:
41+
raise Exception(f"Discord webhook returned error code: {status_code}")
42+
43+
return True
44+
except urllib.error.HTTPError as e:
45+
print(f"Response code: {e.code}")
46+
raise Exception(f"Discord webhook returned error code: {e.code}")
47+
48+
def mask_webhook_url(url):
49+
"""Mask the webhook token for security"""
50+
if "/webhooks/" in url:
51+
parts = url.split("/webhooks/")
52+
if len(parts) > 1:
53+
token_parts = parts[1].split("/")
54+
if len(token_parts) > 1:
55+
return f"{parts[0]}/webhooks/{token_parts[0]}/****"
56+
return url
57+
58+
def main():
59+
# Colors for terminal output
60+
RED = '\033[0;31m'
61+
GREEN = '\033[0;32m'
62+
YELLOW = '\033[1;33m'
63+
BLUE = '\033[0;34m'
64+
NC = '\033[0m' # No Color
65+
66+
print(f"{BLUE}========================================{NC}")
67+
print(f"{BLUE}Discord Webhook Test Script (Python){NC}")
68+
print(f"{BLUE}========================================{NC}")
69+
print()
70+
71+
# Check arguments
72+
if len(sys.argv) < 2:
73+
print(f"{RED}Error: Discord webhook URL is required{NC}")
74+
print()
75+
print("Usage: python3 test-discord-webhook.py <webhook-url> [player-name] [server-name]")
76+
print()
77+
print("Example:")
78+
print(" python3 test-discord-webhook.py https://discord.com/api/webhooks/123456/abcdef")
79+
print(" python3 test-discord-webhook.py https://discord.com/api/webhooks/123456/abcdef Steve")
80+
print(' python3 test-discord-webhook.py https://discord.com/api/webhooks/123456/abcdef Steve "My Awesome Server"')
81+
print()
82+
print(f"{YELLOW}To get a Discord webhook URL:{NC}")
83+
print(" 1. Go to your Discord server settings")
84+
print(" 2. Navigate to Integrations → Webhooks")
85+
print(" 3. Click 'New Webhook'")
86+
print(" 4. Configure the webhook and copy the URL")
87+
print()
88+
sys.exit(1)
89+
90+
webhook_url = sys.argv[1]
91+
player_name = sys.argv[2] if len(sys.argv) > 2 else "TestPlayer"
92+
server_name = sys.argv[3] if len(sys.argv) > 3 else "Minecraft"
93+
94+
print("Testing Discord webhook...")
95+
print(f"Webhook URL: {mask_webhook_url(webhook_url)}")
96+
print(f"Player Name: {player_name}")
97+
print(f"Server Name: {server_name}")
98+
print()
99+
100+
# Format message like the plugin does
101+
message = f"**{player_name}** joined the **{server_name}** server"
102+
103+
print(f"{YELLOW}Sending test message...{NC}")
104+
print()
105+
106+
try:
107+
send_discord_message(webhook_url, message)
108+
print()
109+
print(f"{GREEN}✓ SUCCESS: Message sent to Discord!{NC}")
110+
print("Check your Discord channel for the notification.")
111+
print()
112+
print(f"{GREEN}========================================{NC}")
113+
print(f"{GREEN}Test completed successfully!{NC}")
114+
print(f"{GREEN}========================================{NC}")
115+
sys.exit(0)
116+
except Exception as e:
117+
print()
118+
print(f"{RED}✗ ERROR: Failed to send message to Discord{NC}")
119+
print(f"Error: {str(e)}")
120+
print()
121+
print(f"{RED}========================================{NC}")
122+
print(f"{RED}Test failed!{NC}")
123+
print(f"{RED}========================================{NC}")
124+
sys.exit(1)
125+
126+
if __name__ == "__main__":
127+
main()

0 commit comments

Comments
 (0)