-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-discord-webhook.py
More file actions
executable file
·127 lines (107 loc) · 4.25 KB
/
test-discord-webhook.py
File metadata and controls
executable file
·127 lines (107 loc) · 4.25 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
#!/usr/bin/env python3
"""
Discord Webhook Test Script
This script allows you to test Discord webhook notifications without running a Minecraft server
"""
import sys
import json
import urllib.request
import urllib.error
def send_discord_message(webhook_url, content):
"""Send a message to Discord via webhook"""
if not webhook_url:
raise ValueError("Discord webhook URL is not configured")
# Create JSON payload
payload = {
"content": content
}
data = json.dumps(payload).encode('utf-8')
print(f"Sending payload: {json.dumps(payload)}")
# Create request
req = urllib.request.Request(
webhook_url,
data=data,
headers={'Content-Type': 'application/json'}
)
# Send request
try:
with urllib.request.urlopen(req, timeout=10) as response:
status_code = response.getcode()
print(f"Response code: {status_code}")
if status_code < 200 or status_code >= 300:
raise Exception(f"Discord webhook returned error code: {status_code}")
return True
except urllib.error.HTTPError as e:
print(f"Response code: {e.code}")
raise Exception(f"Discord webhook returned error code: {e.code}")
def mask_webhook_url(url):
"""Mask the webhook token for security"""
if "/webhooks/" in url:
parts = url.split("/webhooks/")
if len(parts) > 1:
token_parts = parts[1].split("/")
if len(token_parts) > 1:
return f"{parts[0]}/webhooks/{token_parts[0]}/****"
return url
def main():
# Colors for terminal output
RED = '\033[0;31m'
GREEN = '\033[0;32m'
YELLOW = '\033[1;33m'
BLUE = '\033[0;34m'
NC = '\033[0m' # No Color
print(f"{BLUE}========================================{NC}")
print(f"{BLUE}Discord Webhook Test Script (Python){NC}")
print(f"{BLUE}========================================{NC}")
print()
# Check arguments
if len(sys.argv) < 2:
print(f"{RED}Error: Discord webhook URL is required{NC}")
print()
print("Usage: python3 test-discord-webhook.py <webhook-url> [player-name] [server-name]")
print()
print("Example:")
print(" python3 test-discord-webhook.py https://discord.com/api/webhooks/123456/abcdef")
print(" python3 test-discord-webhook.py https://discord.com/api/webhooks/123456/abcdef Steve")
print(' python3 test-discord-webhook.py https://discord.com/api/webhooks/123456/abcdef Steve "My Awesome Server"')
print()
print(f"{YELLOW}To get a Discord webhook URL:{NC}")
print(" 1. Go to your Discord server settings")
print(" 2. Navigate to Integrations → Webhooks")
print(" 3. Click 'New Webhook'")
print(" 4. Configure the webhook and copy the URL")
print()
sys.exit(1)
webhook_url = sys.argv[1]
player_name = sys.argv[2] if len(sys.argv) > 2 else "TestPlayer"
server_name = sys.argv[3] if len(sys.argv) > 3 else "Minecraft"
print("Testing Discord webhook...")
print(f"Webhook URL: {mask_webhook_url(webhook_url)}")
print(f"Player Name: {player_name}")
print(f"Server Name: {server_name}")
print()
# Format message like the plugin does
message = f"**{player_name}** joined the **{server_name}** server"
print(f"{YELLOW}Sending test message...{NC}")
print()
try:
send_discord_message(webhook_url, message)
print()
print(f"{GREEN}✓ SUCCESS: Message sent to Discord!{NC}")
print("Check your Discord channel for the notification.")
print()
print(f"{GREEN}========================================{NC}")
print(f"{GREEN}Test completed successfully!{NC}")
print(f"{GREEN}========================================{NC}")
sys.exit(0)
except Exception as e:
print()
print(f"{RED}✗ ERROR: Failed to send message to Discord{NC}")
print(f"Error: {str(e)}")
print()
print(f"{RED}========================================{NC}")
print(f"{RED}Test failed!{NC}")
print(f"{RED}========================================{NC}")
sys.exit(1)
if __name__ == "__main__":
main()