Repository navigation
Expand file tree
/
Copy pathBotNext.py
More file actions
89 lines (65 loc) · 2.24 KB
/
Copy pathBotNext.py
File metadata and controls
89 lines (65 loc) · 2.24 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
import json
import os
import requests
from nacl.signing import VerifyKey
from nacl.exceptions import BadSignatureError
PUBLIC_KEY = os.getenv("DISCORD_PUBLIC_KEY")
API_URL = os.getenv("API_URL")
GUILD_ID = "1219845424169353276"
ROLE_ID = "1235047378327703562"
BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
DISCORD_API = "https://discord.com/api/v10"
def lambda_handler(event, context):
headers = event.get("headers", {})
raw_body = event.get("body", "")
# 🔐 Validação obrigatória do Discord
try:
verify_discord_signature(headers, raw_body)
except Exception:
return {"statusCode": 401}
body = json.loads(raw_body)
# 🔁 Ping
if body["type"] == 1:
return json_response({"type": 1})
# Slash command
if body["type"] == 2 and body["data"]["name"] == "enviar_ra":
ra = body["data"]["options"][0]["value"]
user_id = body["member"]["user"]["id"]
r = requests.get(f"{API_URL}{ra}", timeout=2)
if r.status_code != 200:
return interaction("❌ Erro ao validar o RA.")
dados = r.json()
if dados.get("active") and dados.get("confirmed"):
add_role(user_id)
return interaction("✅ RA verificado com sucesso!")
else:
return interaction("❌ RA não está ativo ou confirmado.")
return {"statusCode": 400}
def verify_discord_signature(headers, body):
signature = headers.get("x-signature-ed25519")
timestamp = headers.get("x-signature-timestamp")
if not signature or not timestamp:
raise Exception("Missing signature")
verify_key = VerifyKey(bytes.fromhex(PUBLIC_KEY))
verify_key.verify(
f"{timestamp}{body}".encode(),
bytes.fromhex(signature)
)
def add_role(user_id):
url = f"{DISCORD_API}/guilds/{GUILD_ID}/members/{user_id}/roles/{ROLE_ID}"
headers = {"Authorization": f"Bot {BOT_TOKEN}"}
requests.put(url, headers=headers)
def json_response(body):
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps(body)
}
def interaction(message):
return json_response({
"type": 4,
"data": {
"content": message,
"flags": 64
}
})