-
Notifications
You must be signed in to change notification settings - Fork 3
/
commands.py
87 lines (72 loc) · 2.07 KB
/
commands.py
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
import os
import requests
import time
import typer
from dotenv import load_dotenv
load_dotenv()
APPLICATION_ID = os.getenv("APPLICATION_ID")
TOKEN = os.getenv("TOKEN")
GUILD_ID = os.getenv("GUILD_ID")
app = typer.Typer()
URL_BASE = f"https://discord.com/api/v10/applications/{APPLICATION_ID}/commands"
HEADERS_BASE = {"Authorization": f"Bot {TOKEN}"}
@app.command()
def hello():
print(f"=== CLI: Manage Slash Commands for Discord Bot ===")
@app.command()
def list_commands():
resp = requests.get(URL_BASE, headers=HEADERS_BASE)
try:
data = resp.json()
print(f"List Commands: {data}")
return data
except Exception as e:
print(e)
@app.command()
def list_ids_commands():
data = list_commands()
if data is not None:
ids = [e["id"] for e in data]
print(f"List ID Commands: {ids}")
return ids
return []
@app.command()
def reset():
ids = list_ids_commands()
for id in ids:
resp = requests.delete(f"{URL_BASE}/{id}", headers=HEADERS_BASE)
time.sleep(2)
print(f"COMMAND ID {id}: {resp.status_code}")
@app.command()
def add_commands():
cmds = [
{
"name": "stat",
"description": "Display the lastest registered stats for a governor",
"type": 1,
"options": [
{
"name": "governor_id",
"description": "The governor ID",
"required": True,
"type": 4, # require a INTEGER INPUT (ID)
},
],
},
{
"name": "top",
"description": "Display the TOP by total score",
"type": 1,
},
]
for cmd in cmds:
# This create or update the slash commands
response = requests.post(URL_BASE, headers=HEADERS_BASE, json=cmd)
if response.status_code >= 400:
print(response.content)
raise Exception("Request Error!")
else:
print("command updated")
time.sleep(2)
if __name__ == "__main__":
app()