Spaces:
Running
Running
Improve tree.command, add async eval
Browse files- discord_bot.py +47 -21
- requirements.txt +1 -0
discord_bot.py
CHANGED
@@ -9,6 +9,7 @@ from threading import Thread
|
|
9 |
import json
|
10 |
from horde import HordeAPI
|
11 |
import inspect
|
|
|
12 |
|
13 |
|
14 |
|
@@ -33,26 +34,52 @@ with open("discord.json", "r") as f:
|
|
33 |
json_data = json.load(f)
|
34 |
|
35 |
|
36 |
-
# 自动生成调用函数
|
37 |
COMMAND_NAME_PREFIX="discord_bot_call_"
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
|
58 |
async def greet(name: str):
|
@@ -114,8 +141,7 @@ async def on_message(message):
|
|
114 |
# 如果规则指定了函数,则调用对应的函数
|
115 |
if "function" in rule:
|
116 |
function_name = rule["function"]
|
117 |
-
|
118 |
-
result = await function(message)
|
119 |
await message.channel.send(result)
|
120 |
# 否则发送预定义的响应消息
|
121 |
elif "response" in rule:
|
|
|
9 |
import json
|
10 |
from horde import HordeAPI
|
11 |
import inspect
|
12 |
+
from async_eval import eval
|
13 |
|
14 |
|
15 |
|
|
|
34 |
json_data = json.load(f)
|
35 |
|
36 |
|
|
|
37 |
COMMAND_NAME_PREFIX="discord_bot_call_"
|
38 |
+
|
39 |
+
|
40 |
+
# 生成discord command的callback的调用参数
|
41 |
+
def generate_discord_command_callback_param_str(parameters: list = []) -> str:
|
42 |
+
result=f'''
|
43 |
+
interaction: discord.Interaction{''.join([f", {param['name']}: {param['type']}"
|
44 |
+
for param in parameters])}
|
45 |
+
'''
|
46 |
+
return result
|
47 |
+
|
48 |
+
|
49 |
+
# 生成调用用户自定义的函数的参数
|
50 |
+
def generate_user_function_param_str(parameters: list = []) -> str:
|
51 |
+
result=f'''{', '.join([f"{param['name']}={param['name']}"
|
52 |
+
for param in parameters])}'''
|
53 |
+
return result
|
54 |
+
|
55 |
+
|
56 |
+
# 生成tree add_command
|
57 |
+
def generate_tree_add_command(command: dict = {}) -> str:
|
58 |
+
result=f'''
|
59 |
+
tree.add_command(app_commands.Command(
|
60 |
+
name="{command['name']}",
|
61 |
+
description="{command['description']}",
|
62 |
+
callback={COMMAND_NAME_PREFIX}{command['name']}
|
63 |
+
))
|
64 |
+
'''
|
65 |
+
return result
|
66 |
+
|
67 |
+
|
68 |
+
# 生成discord command的callback的调用函数
|
69 |
+
def generate_discord_command_callback_function_str(command: dict = {}) -> str:
|
70 |
+
result=f'''
|
71 |
+
async def {COMMAND_NAME_PREFIX}{command['name']}({generate_discord_command_callback_param_str(command['parameters'])}):
|
72 |
+
await interaction.response.defer()
|
73 |
+
result = await {command['function']}({generate_user_function_param_str(command['parameters'])})
|
74 |
+
if result is not None:
|
75 |
+
await interaction.followup.send(result)
|
76 |
+
'''
|
77 |
+
return result
|
78 |
+
|
79 |
+
|
80 |
+
for command in json_data["command"]:
|
81 |
+
exec(generate_discord_command_callback_function_str(command))
|
82 |
+
exec(generate_tree_add_command(command))
|
83 |
|
84 |
|
85 |
async def greet(name: str):
|
|
|
141 |
# 如果规则指定了函数,则调用对应的函数
|
142 |
if "function" in rule:
|
143 |
function_name = rule["function"]
|
144 |
+
result = eval(f"await {function_name()}")
|
|
|
145 |
await message.channel.send(result)
|
146 |
# 否则发送预定义的响应消息
|
147 |
elif "response" in rule:
|
requirements.txt
CHANGED
@@ -10,3 +10,4 @@ py-cord
|
|
10 |
discord-py-interactions
|
11 |
nextcord
|
12 |
httpx
|
|
|
|
10 |
discord-py-interactions
|
11 |
nextcord
|
12 |
httpx
|
13 |
+
async-eval
|