joytou commited on
Commit
cb390bc
·
1 Parent(s): 006c68c

Add exec function to generate needed function

Browse files
Files changed (2) hide show
  1. discord.json +2 -2
  2. discord_bot.py +24 -5
discord.json CHANGED
@@ -36,13 +36,13 @@
36
  ]
37
  },
38
  {
39
- "name": "get-kudos",
40
  "description": "The amount of Kudos this user has.",
41
  "function": "getKudos",
42
  "parameters": []
43
  },
44
  {
45
- "name": "generate-status",
46
  "description": "Retrieve the status of an Asynchronous generation request.",
47
  "function": "generateStatus",
48
  "parameters": [
 
36
  ]
37
  },
38
  {
39
+ "name": "get_kudos",
40
  "description": "The amount of Kudos this user has.",
41
  "function": "getKudos",
42
  "parameters": []
43
  },
44
  {
45
+ "name": "generate_status",
46
  "description": "Retrieve the status of an Asynchronous generation request.",
47
  "function": "generateStatus",
48
  "parameters": [
discord_bot.py CHANGED
@@ -36,6 +36,23 @@ tree = bot.tree
36
  with open("discord.json", "r") as f:
37
  json_data = json.load(f)
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  """
41
  @tree.command(name="hello", description="Sends a greeting!")
@@ -175,12 +192,14 @@ MyClient(bot)
175
  """
176
 
177
 
178
- async def test(interaction: discord.Interaction, x: str, y: int):
 
 
179
  print(interaction)
180
- print(x)
181
- print(y)
182
- await interaction.response.send_message(f"x: {x}, y: {y}")
183
- tree.add_command(app_commands.Command(name="test", description="test", callback=test, extras={}))
184
 
185
 
186
 
 
36
  with open("discord.json", "r") as f:
37
  json_data = json.load(f)
38
 
39
+ # 自动生成调用函数
40
+ commandNamePrefix="discord_bot_call_"
41
+ file_content="\n\n".join([f"async def {commandNamePrefix}{command['name']}(interaction: discord.Interaction"
42
+ +''.join([f", {param['name']}: {param['type']}" for param in command["parameters"]])
43
+ +f"):\n\
44
+ await interaction.response.defer()\n\
45
+ result = await {command['function']}("
46
+ +', '.join([f"{param['name']}={param['name']}" for param in command["parameters"]])
47
+ +f")\n\
48
+ if result is not None:\n\
49
+ await interaction.followup.send(result)\n\
50
+ tree.add_command(app_commands.Command(name=\"{command['name']}\", description=\"{command['description']}\", callback={commandNamePrefix}{command['name']}))"
51
+ for command in json_data["command"]])
52
+ exec(file_content)
53
+
54
+
55
+
56
 
57
  """
58
  @tree.command(name="hello", description="Sends a greeting!")
 
192
  """
193
 
194
 
195
+ """
196
+ @tree.command(name="test", description="test")
197
+ async def test(interaction: discord.Interaction, *args):
198
  print(interaction)
199
+ print(args)
200
+ await interaction.response.send_message(f"{args}")
201
+ #tree.add_command(app_commands.Command(name="test", description="test", callback=test))
202
+ """
203
 
204
 
205