joytou commited on
Commit
f1315c3
·
1 Parent(s): 79a7353

Add get_help, support for default valve with the discord function

Browse files
Files changed (2) hide show
  1. discord.json +18 -2
  2. discord_bot.py +87 -2
discord.json CHANGED
@@ -27,7 +27,9 @@
27
  "parameters": [
28
  {
29
  "name": "name",
30
- "type": "str"
 
 
31
  }
32
  ]
33
  }
@@ -41,13 +43,14 @@
41
  {
42
  "name": "name",
43
  "type": "str",
 
44
  "description": "The user to greet"
45
  }
46
  ]
47
  },
48
  {
49
  "name": "get_kudos",
50
- "description": "The amount of Kudos this user has.",
51
  "function": "get_kudos",
52
  "parameters": []
53
  },
@@ -62,6 +65,19 @@
62
  "description": "The ID of asyncronous generation request."
63
  }
64
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  }
66
  ]
67
  }
 
27
  "parameters": [
28
  {
29
  "name": "name",
30
+ "type": "str",
31
+ "default": "this user",
32
+ "description": "The user to greet"
33
  }
34
  ]
35
  }
 
43
  {
44
  "name": "name",
45
  "type": "str",
46
+ "default": "this user",
47
  "description": "The user to greet"
48
  }
49
  ]
50
  },
51
  {
52
  "name": "get_kudos",
53
+ "description": "Get the amount of Kudos this user has.",
54
  "function": "get_kudos",
55
  "parameters": []
56
  },
 
65
  "description": "The ID of asyncronous generation request."
66
  }
67
  ]
68
+ },
69
+ {
70
+ "name": "help",
71
+ "description": "To query supported command(s) in this app.",
72
+ "function": "get_help",
73
+ "parameters": [
74
+ {
75
+ "name": "command_name",
76
+ "type": "str",
77
+ "default": "",
78
+ "description": "The command name to query. Keep it empty to query all commands."
79
+ }
80
+ ]
81
  }
82
  ]
83
  }
discord_bot.py CHANGED
@@ -61,8 +61,8 @@ def generate_discord_command_callback_param_str(pre: str = '', parameters: list
61
  string: 生成的调用参数的代码
62
  """
63
  return f'''
64
- {pre}{''.join([f", {param['name']}: {param['type']}"
65
- for param in parameters])}
66
  '''
67
 
68
 
@@ -165,6 +165,91 @@ if "command" in json_data:
165
  exec(generate_bot_command_def(command))
166
 
167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  async def greet(name: str):
169
  return f"Hello, {name}!"
170
 
 
61
  string: 生成的调用参数的代码
62
  """
63
  return f'''
64
+ {pre}{''.join([f", {param['name']}: {param['type']}" + (f' = "{param["default"]}"' if 'default' in param and param['type'] == 'str' else f" = {param['default']}" if 'default' in param else '')
65
+ for param in parameters])}
66
  '''
67
 
68
 
 
165
  exec(generate_bot_command_def(command))
166
 
167
 
168
+ def get_help(command_name: str = ''):
169
+ def do_with_message(message: dict):
170
+ result = ''
171
+ result += f'If message is '
172
+ match message['type']:
173
+ case "equals":
174
+ result += ''
175
+ case "contains":
176
+ result += 'contains '
177
+ case "startswith":
178
+ result += 'starts with '
179
+ case "endswith":
180
+ result += 'ends with '
181
+ result += f'"{message["content"]}", then this app will '
182
+ result += generate_command_help_info(message)
183
+ return result
184
+ def do_with_command(command: dict):
185
+ result = ''
186
+ result += f'If message is starts with "{command["name"]}", then this app will '
187
+ result += generate_command_help_info(command)
188
+ return result
189
+ def do_with_app_command(command: dict):
190
+ result = ''
191
+ result += f'If message is starts with "/{command["name"]}", then this app will '
192
+ result += generate_command_help_info(command)
193
+ return result
194
+ def generate_command_help_info(message: dict):
195
+ result = ''
196
+ if 'response' in message:
197
+ result += f'response "{message["response"]}"'
198
+ elif 'function' in message:
199
+ if 'description' in message:
200
+ result += f'{message["description"][0].lower()}{message["description"][1:]}'
201
+ else:
202
+ result += f'execulate function which named "{message["function"]}"'
203
+ if 'parameters' in message and len(message['parameters']) > 0:
204
+ result += ', with parameter(s): '
205
+ for param in message['parameters']:
206
+ result += '\n\t'
207
+ result += f'- {param["name"]}: {param["type"]}'
208
+ if 'default' in param:
209
+ result += ' = '
210
+ match param['type']:
211
+ case 'str':
212
+ result += f'"{param["default"]}"'
213
+ case _:
214
+ result += f'"{param["default"]}"'
215
+ if 'description' in param:
216
+ result += f'\n\t {param["description"]}'
217
+ else:
218
+ result += 'do nothing'
219
+ result += '\n'
220
+ return result
221
+ result = ''
222
+ if command_name is None or command_name == '':
223
+ if 'message' in json_data:
224
+ for message in json_data['message']:
225
+ result += do_with_message(message)
226
+ if 'command' in json_data:
227
+ for command in json_data['command']:
228
+ result += do_with_command(command)
229
+ if 'app_command' in json_data:
230
+ for command in json_data['app_command']:
231
+ result += do_with_app_command(command)
232
+ else:
233
+ if 'message' in json_data:
234
+ for message in json_data['message']:
235
+ if message['content'] != command_name:
236
+ continue
237
+ result += do_with_message(message)
238
+ if 'command' in json_data:
239
+ for command in json_data['command']:
240
+ if command['name'] != command_name:
241
+ continue
242
+ result += do_with_command(command)
243
+ if 'app_command' in json_data:
244
+ for command in json_data['app_command']:
245
+ if command['name'] != command_name:
246
+ continue
247
+ result += do_with_app_command(command)
248
+ if result is None or result == '':
249
+ result = f'Can\'t not find "{command_name}", you can type "/help" to query all commands, or "/help [command name]" to query the command.'
250
+ return result
251
+
252
+
253
  async def greet(name: str):
254
  return f"Hello, {name}!"
255