-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakecommand.py
224 lines (203 loc) · 6.05 KB
/
makecommand.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import requests
import base64
import json
import getpass
my_application_id = "918936936377372752"
API_ENDPOINT = 'https://discord.com/api/v8'
CLIENT_ID = input("Client ID: ")
CLIENT_SECRET = getpass.getpass("Client Secret: ")
def get_token():
data = {
'grant_type': 'client_credentials',
'scope': 'applications.commands.update guilds'
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
r = requests.post('%s/oauth2/token' % API_ENDPOINT, data=data, headers=headers, auth=(CLIENT_ID, CLIENT_SECRET))
r.raise_for_status()
return r.json()
g = get_token()
print(g)
my_credentials_token = g['access_token']
baseurl = "https://discord.com/api/v8"
url = f"{baseurl}/applications/{my_application_id}/commands"
print(url)
commands = dict(
issue = dict(
name = "issue",
type = 1,
description = "manage creating a GitHub issue",
options = [
dict(
name = "start",
description = "start creating a new GitHub issue",
type = 1,
options = [
dict(
name = "issue-name",
description = "The name of the new issue",
type = 3,
required = True)]),
dict(
name = "help",
description = "display help on using the bot",
type = 1),
dict(
name = "preview",
description = "mark an issue complete and ready for sending to GitHub",
type = 1),
dict(
name = "submit",
description = "submit the issue to GitHub",
type = 1),
dict(
name = "summary",
description = "set the issue summary",
options = [
dict(name="summary",
description = "The summary of the pending issue",
type=3,
required = True)],
type = 1),
dict(
name = "add",
description = "Add text to a pending issue",
type = 1,
options = [
dict(
name = "text",
description = "The tet to add to the pending issue",
type = 3,
required = True)]),
dict(
name = "status",
description = "Get the status of the pending issue",
type = 1,
),
dict(
name = "remove",
description = "Remove the Nth message or reference from the current issue (0 indexed)",
type = 2,
options = [
dict(
name = "message",
description = "remove the Nth message (0 indexed)",
type = 1,
options = [
dict(
name = "message",
description = "the index of the message to remove",
type = 4,
required = False)]),
dict(
name = "reference",
description = "remove the Nth reference (0 indexed)",
type = 1,
options = [
dict(
name = "text",
description = "the index of the reference to remove",
type = 4,
required = False)])]),
dict(
name = "repository",
description = "Get or set the GitHub repositry where issues are opened",
type = 2,
options = [
dict(
name = "set",
description = "set the repository",
type = 1,
options = [
dict(
name = "repository-name",
description = "the name of the repository",
type = 3,
required = True)]),
dict(
name = "get",
description = "get the repository",
type = 1)]),
dict(
name = "role",
description = "Get or set the role which has permission to create GitHub issues",
type = 2,
options = [
dict(
name = "set",
description = "set the role",
type = 1,
options = [
dict(
name = "role-name",
description = "the @role",
type = 3,
required = True)]),
dict(
name = "get",
description = "get the role",
type = 1)])]),
AddToIssue = dict(
name="AddToIssue",
type=3,
)
)
# or a client credentials token for your app with the applications.commands.update scope
headers = {
"Authorization": f"Bearer {my_credentials_token}"
}
import sys
import time
if len(sys.argv) == 1 or sys.argv[1] == 'setup':
#r = requests.put(url, headers=headers, json=json)
r = requests.get(url, headers=headers)
if r.ok:
current_commands = json.loads(r.text)
current_command_map = {}
print("Existing Commands")
print("-"*20)
for command in current_commands:
print(f"Name: {command['name']}")
if command['name'] not in commands:
print("Removing obsolete command")
r = requests.delete(f"{url}/{command['id']}", headers=headers)
if not r.ok:
print(r.text)
raise SystemExit(1)
time.sleep(1)
else:
current_command_map[command['name']] = command
for command, nc in commands.items():
if command in current_command_map:
cc = current_command_map[command]
print(f"Updating {command}")
id = cc['id']
r = requests.patch(f"{url}/{id}", headers=headers, json=nc)
if not r.ok:
print(r.text)
raise SystemExit(2)
time.sleep(5)
else:
print(f"Creating new command {command}")
r = requests.post(url, headers=headers, json=nc)
if not r.ok:
print(r.text)
raise SystemExit(3)
time.sleep(5)
else:
print(r)
print(r.text)
else:
method = sys.argv[1]
suburl = sys.argv[2]
if method in ['put', 'patch', 'post']:
body = json.loads(input())
r = getattr(requests, method)(f"{baseurl}{suburl}", headers=headers, json=body)
else:
r = getattr(requests, method)(f"{baseurl}{suburl}", headers=headers)
print(r.ok)
if r.ok:
print(json.dumps(indent=4, obj=json.loads(r.text)))
else:
print(r.text)