Alibrown commited on
Commit
cd416a9
·
verified ·
1 Parent(s): 708db7e

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +78 -0
main.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # test_connection.py
2
+
3
+ import os
4
+ import requests
5
+ import json
6
+
7
+ # --- Konfiguration ---
8
+ # HINWEIS: Stelle sicher, dass diese Variablen als Secrets in deinen Hugging Face Space-Einstellungen hinterlegt sind.
9
+ # Sie werden automatisch von der Umgebung geladen.
10
+ try:
11
+ # Saubere, konsistente Namen für die Umgebungsvariablen
12
+ APPLICATION_ID = os.environ["APPLICATION_ID"]
13
+ BOT_TOKEN = os.environ["BOT_TOKEN"]
14
+ GUILD_ID = os.environ["GUILD_ID"]
15
+ PUBLIC_KEY = os.environ["PUBLIC_KEY"] # Auch hier, für die Konsistenz!
16
+ except KeyError as e:
17
+ print(f"Fehler: Umgebungsvariable {e} nicht gefunden. Bitte als Secret hinzufügen.")
18
+ exit()
19
+
20
+ # --- API-Endpunkt ---
21
+ # Der Endpunkt für die Registrierung von Guild-Befehlen
22
+ url = f"https://discord.com/api/v10/applications/{APPLICATION_ID}/guilds/{GUILD_ID}/commands"
23
+
24
+ # --- Befehls-Payload ---
25
+ # Ein einfacher Slash-Befehl, um die Verbindung zu testen.
26
+ # Diesen Befehl kannst du später entfernen oder in deine Hauptanwendung integrieren.
27
+ commands = [
28
+ {
29
+ "name": "testping",
30
+ "description": "Prüft die Verbindung und antwortet mit Pong!",
31
+ "type": 1
32
+ }
33
+ ]
34
+
35
+ # --- Anfrage-Header ---
36
+ # Der Bot-Token ist für die Authentifizierung erforderlich.
37
+ headers = {
38
+ "Authorization": f"Bot {BOT_TOKEN}",
39
+ "Content-Type": "application/json"
40
+ }
41
+
42
+ # --- Ausführung der API-Anfrage ---
43
+ print("Versuche, den 'testping' Befehl zu registrieren...")
44
+ print(f"Ziel-URL: {url}")
45
+
46
+ try:
47
+ response = requests.put(url, headers=headers, json=commands, timeout=10)
48
+ response.raise_for_status() # Löst einen Fehler für schlechte HTTP-Statuscodes aus (4xx, 5xx)
49
+
50
+ print("\n-----------------------------------")
51
+ print("✅ ERFOLG! Verbindung hergestellt und Befehl registriert.")
52
+ print("Discord-API-Antwort (Statuscode):", response.status_code)
53
+ print("Antwort-Body:", json.dumps(response.json(), indent=2))
54
+ print("-----------------------------------")
55
+
56
+ except requests.exceptions.ConnectionError as e:
57
+ print("\n-----------------------------------")
58
+ print("❌ FEHLER: Verbindung fehlgeschlagen.")
59
+ print("Die Anwendung konnte keine Verbindung zu Discord.com herstellen.")
60
+ print("Das ist derselbe Fehler wie zuvor, was auf ein Netzwerkproblem im Space hindeutet.")
61
+ print("Fehlerdetails:", e)
62
+ print("-----------------------------------")
63
+
64
+ except requests.exceptions.HTTPError as e:
65
+ print("\n-----------------------------------")
66
+ print("⚠️ FEHLER: Discord API-Antwortfehler.")
67
+ print("Der Befehl wurde gesendet, aber Discord hat mit einem Fehler geantwortet.")
68
+ print("Dies könnte an falschen Token, Berechtigungen oder IDs liegen.")
69
+ print(f"Statuscode: {response.status_code}")
70
+ print("Fehlerdetails:", e)
71
+ print("Antwort-Body:", response.text)
72
+ print("-----------------------------------")
73
+
74
+ except Exception as e:
75
+ print("\n-----------------------------------")
76
+ print("❌ EIN UNBEKANNTER FEHLER IST AUFGETRETEN.")
77
+ print("Fehlerdetails:", e)
78
+ print("-----------------------------------")