This repository has been archived by the owner on Oct 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slashrequest.py
210 lines (197 loc) · 5.5 KB
/
slashrequest.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
import requests
import json
import discord
from discord.ext import commands
from discord_slash import SlashCommand, utils
def store(file, key=None, read=False, val=None):
with open(file, 'r') as v:
x = json.load(v)
if read is not False:
if key is None:
return x
else:
return x[key]
else:
if val is None:
with open(file, 'w') as v:
json.dump(key, v, indent=4)
return
x[key] = val
with open(file, 'w') as v:
json.dump(x, v, indent=4)
head = store('config.json', 'slashConfig', True)['auth']
# internal
def checkURL():
x = store('config.json', 'slashConfig', True)
returns = []
if x['appID'] is not None:
returns.append(x['appID'])
if x['guildID'] is not None and x['tempID'] is None:
returns.append(x['guildID'])
else:
returns.append(x['tempID'])
return returns
class sc:
def setGID(ngid):
x = store('config.json', None, True)
x['slashConfig']['tempID'] = ngid
store('config.json', x)
print(f"Set new guild id to {ngid}")
# Basic functions
def get(comName=None, all=False):
k = checkURL()
url = f"https://discord.com/api/v8/applications/{k[0]}/guilds/{k[1]}/commands"
f = requests.get(url, headers=head)
if all is False:
if comName is not None:
d = None
for com in f.json():
if com['name'] == comName:
d = com
break
return d
g = []
for com in f.json():
name = com['name']
id = com['id']
g.append({"name": name, "id": id})
return g
return f.json()
def post(jsonData):
k = checkURL()
url = f"https://discord.com/api/v8/applications/{k[0]}/guilds/{k[1]}/commands"
e = requests.post(url, headers=head, json=jsonData)
return e
def rem(slashName):
k = checkURL()
url = f"https://discord.com/api/v8/applications/{k[0]}/guilds/{k[1]}/commands"
f = requests.get(url, headers=head)
d = None
if slashName is not None:
d = None
for com in f.json():
if com['name'] == slashName:
d = com
break
if d is None: return
id = 'id'
r = requests.delete(url + f"/{com[id]}", headers=head)
return r
# Permissions
def perm(commandID, roleIDTuple=None, permTuple=None, *, includeSelf=True, user=None, userPerm=True, setAll=None):
k = checkURL()
url = f"https://discord.com/api/v8/applications/{k[0]}/guilds/{k[1]}/commands/{commandID}/permissions"
eurl = f"https://discord.com/api/v8/applications/{k[0]}/guilds/{k[1]}/commands"
if setAll is False:
g = None
f = requests.get(eurl, headers=head)
for com in f.json():
if com['id'] == f"{commandID}":
g = com
break
if g['default_permission'] == True:
g.pop('id')
g.pop('version')
g.pop('application_id')
g.pop('guild_id')
g['default_permission'] = False
d = requests.post(eurl, headers=head, json=g)
return d
jData = {
"permissions": []
}
r = requests.put(url, headers=head, json=jData)
return r
elif setAll is True:
g = None
f = requests.get(eurl, headers=head)
for com in f.json():
if com['id'] == f"{commandID}":
g = com
break
g.pop('id')
g.pop('version')
g.pop('application_id')
g.pop('guild_id')
g['default_permission'] = True
d = requests.post(eurl, headers=head, json=g)
return d
perms = []
if includeSelf is True or (roleIDTuple is None and staff is False) and user is None:
selfID = store('config.json', 'slashConfig', True)['selfID']
selfData = {
"id": selfID,
"type": 2,
"permission": True
}
perms.append(selfData)
if roleIDTuple is not None:
try:
count = 0
for roleID in roleIDTuple:
permAllow = permTuple[count]
count += 1
roleData = {
"id": roleID,
"type": 1,
"permission": permAllow
}
perms.append(roleData)
except:
return
if user is not None:
try:
uid = str(user)
except:
return
userData = {
"id": uid,
"type": 2,
"permission": userPerm
}
perms.append(userData)
jData = {
"permissions": perms
}
r = requests.put(url, headers=head, json=jData)
return r
class gl:
def get(comName=None, all=False):
k = checkURL()
url = f"https://discord.com/api/v8/applications/{k[0]}/commands"
f = requests.get(url, headers=head)
if all is False:
if comName is not None:
d = None
for com in f.json():
if com['name'] == comName:
d = com
break
return d
g = []
for com in f.json():
name = com['name']
id = com['id']
g.append({"name": name, "id": id})
return g
return f.json()
def post(jsonData):
k = checkURL()
url = f"https://discord.com/api/v8/applications/{k[0]}/commands"
e = requests.post(url, headers=head, json=jsonData)
return e
def rem(slashName):
k = checkURL()
url = f"https://discord.com/api/v8/applications/{k[0]}/commands"
f = requests.get(url, headers=head)
d = None
if slashName is not None:
d = None
for com in f.json():
if com['name'] == slashName:
d = com
break
if d is None: return
id = 'id'
r = requests.delete(url + f"/{com[id]}", headers=head)
return r