joytou commited on
Commit
4d83248
·
1 Parent(s): 5a1bef9
Files changed (1) hide show
  1. discord_bot.py +2 -72
discord_bot.py CHANGED
@@ -113,7 +113,7 @@ async def generateStatus(id: str):
113
  return f'Position in queue: {details["queue_position"]}, wait time: {details["wait_time"]}s'
114
 
115
 
116
- """
117
  # 根据 json 数据动态创建命令
118
  for command in json_data["command"]:
119
  async def dynamic_command(interaction: discord.Interaction, **kwargs):
@@ -133,82 +133,12 @@ for command in json_data["command"]:
133
  name=command["name"],
134
  description=command["description"],
135
  callback=dynamic_command,
136
- params=params
137
  )
138
 
139
  # 将命令添加到 bot 的 command tree
140
  bot.tree.add_command(tree_command)
141
- """
142
-
143
 
144
- # 初始化客户端和命令树
145
- class MyClient(commands.Bot):
146
- def __init__(self, *args, **kwargs):
147
- super().__init__(*args, **kwargs)
148
- self.tree = app_commands.CommandTree(self)
149
-
150
- async def setup_hook(self):
151
- # 在这里你可以注册动态生成的命令
152
- await self.load_commands()
153
-
154
- async def load_commands(self):
155
- # 从 JSON 文件中加载命令
156
- with open('discord.json', 'r') as f:
157
- commands_data = json.load(f)
158
-
159
- # 遍历每个命令,动态创建并注册到命令树
160
- for command_data in commands_data['commands']:
161
- await self.create_and_register_command(command_data)
162
-
163
- async def create_and_register_command(self, command_data):
164
- # 动态创建命令回调
165
- async def dynamic_command(interaction: discord.Interaction, **kwargs):
166
- """
167
- # 可以根据不同的命令执行不同的逻辑
168
- if command_data["name"] == "greet":
169
- name = kwargs.get("name")
170
- await interaction.response.send_message(f"Hello, {name}!")
171
- elif command_data["name"] == "sum":
172
- a = kwargs.get("a")
173
- b = kwargs.get("b")
174
- result = a + b
175
- await interaction.response.send_message(f"The sum is: {result}")
176
- """
177
-
178
- # 动态设置命令参数
179
- command_params = []
180
- """
181
- for param_name, param_data in command_data["params"].items():
182
- if param_data["type"] == "str":
183
- param_type = str
184
- elif param_data["type"] == "int":
185
- param_type = int
186
- else:
187
- raise ValueError(f"Unsupported parameter type: {param_data['type']}")
188
-
189
- command_params.append(
190
- app_commands.Parameter(
191
- name=param_name,
192
- description=param_data["description"],
193
- type=param_type
194
- )
195
- )
196
- """
197
- command_params = [app_commands.Parameter(name=param["name"], description=param["description"], type=TYPE_MAPPING[param["type"]])
198
- for param in command["parameters"]]
199
-
200
- # 创建并注册命令
201
- command = app_commands.Command(
202
- name=command_data["name"],
203
- description=command_data["description"],
204
- callback=dynamic_command,
205
- parameters=command_params
206
- )
207
- # 将命令添加到命令树
208
- self.tree.add_command(command)
209
-
210
-
211
- bot = MyClient(command_prefix='>', intents=intents)
212
 
213
 
214
  @bot.command()
 
113
  return f'Position in queue: {details["queue_position"]}, wait time: {details["wait_time"]}s'
114
 
115
 
116
+
117
  # 根据 json 数据动态创建命令
118
  for command in json_data["command"]:
119
  async def dynamic_command(interaction: discord.Interaction, **kwargs):
 
133
  name=command["name"],
134
  description=command["description"],
135
  callback=dynamic_command,
136
+ parameters=params
137
  )
138
 
139
  # 将命令添加到 bot 的 command tree
140
  bot.tree.add_command(tree_command)
 
 
141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
143
 
144
  @bot.command()