Skip to content

Commit aba163f

Browse files
authored
Merge pull request #21 from astr0n8t/v0.2.0
V0.2.0
2 parents a88ea51 + 96b51ca commit aba163f

File tree

1 file changed

+55
-11
lines changed

1 file changed

+55
-11
lines changed

bot.py

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,82 @@ class Command:
1616
desc: str
1717
resp: str
1818
url: str
19+
headers: str
20+
data: str
21+
group: str
1922

20-
def cmd_builder(name, desc, resp, url):
23+
@dataclass
24+
class Group:
25+
name: str
26+
desc: str
27+
parent: str
2128

22-
return"@bot.command(description='" + desc + """')
29+
def cmd_builder(name, desc, resp, url, headers, data, group):
30+
31+
return"@" + group + ".command(description='" + desc + """')
2332
async def """ + name + """(ctx):
24-
req = requests.post('""" + url + """')
33+
req = requests.post('""" + url + """', headers=""" + headers + """, data=""" + data + """)
2534
await ctx.respond(f\"""" + resp + "\")"
2635

36+
def group_builder(name, desc, parent):
37+
return name + " = " + parent + ".create_group('" + name + "', '" + desc + "')"
38+
2739
def get_commands():
28-
command_list = []
40+
commands = []
2941
dishook_prefix = "DISHOOK_COMMAND_"
3042
i = 0
3143
while os.getenv(str(dishook_prefix + str(i))):
44+
current_prefix = str(dishook_prefix + str(i))
3245
cmd = Command(
33-
name=str(os.getenv(str(dishook_prefix + str(i)))),
34-
desc=str(os.getenv(str(dishook_prefix + str(i) + "_DESCRIPTION"))),
35-
resp=str(os.getenv(str(dishook_prefix + str(i) + "_RESPONSE"))),
36-
url=str(os.getenv(str(dishook_prefix + str(i) + "_URL")))
46+
name=str(os.getenv(current_prefix)),
47+
desc=str(os.getenv(str(current_prefix + "_DESCRIPTION"))),
48+
resp=str(os.getenv(str(current_prefix + "_RESPONSE"))),
49+
url=str(os.getenv(str(current_prefix + "_URL"))),
50+
headers="{}",
51+
data="{}",
52+
group="bot"
3753
)
38-
command_list.append(cmd)
54+
if os.getenv(str(current_prefix + "_HEADERS")):
55+
cmd.headers = str(os.getenv(str(current_prefix + "_HEADERS")))
56+
if os.getenv(str(current_prefix + "_DATA")):
57+
cmd.headers = str(os.getenv(str(current_prefix + "_DATA")))
58+
if os.getenv(str(current_prefix + "_GROUP")):
59+
cmd.group = str(os.getenv(str(current_prefix + "_GROUP")))
60+
commands.append(cmd)
3961
i += 1
40-
return command_list
62+
return commands
63+
64+
def get_groups():
65+
groups = []
66+
dishook_prefix = "DISHOOK_GROUP_"
67+
i = 0
68+
while os.getenv(str(dishook_prefix + str(i))):
69+
current_prefix = str(dishook_prefix + str(i))
70+
group = Group(
71+
name=str(os.getenv(current_prefix)),
72+
desc=str(os.getenv(str(current_prefix + "_DESCRIPTION"))),
73+
parent="bot"
74+
)
75+
if os.getenv(str(current_prefix + "_PARENT")):
76+
group.parent = str(os.getenv(str(current_prefix + "_PARENT")))
77+
groups.append(group)
78+
i += 1
79+
return groups
4180

4281
if __name__ == '__main__':
4382

4483
print("Beginning to look for commands designated by DISHOOK_COMMAND_#")
4584

4685
commands = get_commands()
86+
groups = get_groups()
87+
88+
for group in groups:
89+
print("Loading groups: '" + group.name + "' with desc: '" + group.desc + "'")
90+
exec(group_builder(group.name, group.desc, group.parent))
4791

4892
for cmd in commands:
4993
print("Loading command: '" + cmd.name + "' with desc: '" + cmd.desc + "'")
50-
exec(cmd_builder(cmd.name, cmd.desc, cmd.resp, cmd.url))
94+
exec(cmd_builder(cmd.name, cmd.desc, cmd.resp, cmd.url, cmd.headers, cmd.data))
5195

5296
print("Success. Starting dishook...")
5397
bot.run(token)

0 commit comments

Comments
 (0)