Spaces:
Running
Running
Fixed: Command.__init__() got an unexpected keyword argument 'params'
Browse files- discord_bot.py +67 -1
discord_bot.py
CHANGED
@@ -28,7 +28,7 @@ check_functions = {
|
|
28 |
|
29 |
intents = discord.Intents.default()
|
30 |
intents.message_content = True
|
31 |
-
bot = commands.Bot(command_prefix='>', intents=intents)
|
32 |
tree = bot.tree
|
33 |
|
34 |
|
@@ -117,6 +117,7 @@ async def generateStatus(id: str):
|
|
117 |
return f'Position in queue: {details["queue_position"]}, wait time: {details["wait_time"]}s'
|
118 |
|
119 |
|
|
|
120 |
# 根据 json 数据动态创建命令
|
121 |
for command in json_data["command"]:
|
122 |
async def dynamic_command(interaction: discord.Interaction, **kwargs):
|
@@ -141,6 +142,71 @@ for command in json_data["command"]:
|
|
141 |
|
142 |
# 将命令添加到 bot 的 command tree
|
143 |
bot.tree.add_command(tree_command)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
|
146 |
@bot.event
|
|
|
28 |
|
29 |
intents = discord.Intents.default()
|
30 |
intents.message_content = True
|
31 |
+
#bot = commands.Bot(command_prefix='>', intents=intents)
|
32 |
tree = bot.tree
|
33 |
|
34 |
|
|
|
117 |
return f'Position in queue: {details["queue_position"]}, wait time: {details["wait_time"]}s'
|
118 |
|
119 |
|
120 |
+
"""
|
121 |
# 根据 json 数据动态创建命令
|
122 |
for command in json_data["command"]:
|
123 |
async def dynamic_command(interaction: discord.Interaction, **kwargs):
|
|
|
142 |
|
143 |
# 将命令添加到 bot 的 command tree
|
144 |
bot.tree.add_command(tree_command)
|
145 |
+
"""
|
146 |
+
|
147 |
+
|
148 |
+
# 初始化客户端和命令树
|
149 |
+
class MyClient(discord.Client):
|
150 |
+
def __init__(self, *args, **kwargs):
|
151 |
+
super().__init__(*args, **kwargs)
|
152 |
+
self.tree = app_commands.CommandTree(self)
|
153 |
+
|
154 |
+
async def setup_hook(self):
|
155 |
+
# 在这里你可以注册动态生成的命令
|
156 |
+
await self.load_commands()
|
157 |
+
|
158 |
+
async def load_commands(self):
|
159 |
+
# 从 JSON 文件中加载命令
|
160 |
+
with open('discord.json', 'r') as f:
|
161 |
+
commands_data = json.load(f)
|
162 |
+
|
163 |
+
# 遍历每个命令,动态创建并注册到命令树
|
164 |
+
for command_data in commands_data['commands']:
|
165 |
+
await self.create_and_register_command(command_data)
|
166 |
+
|
167 |
+
async def create_and_register_command(self, command_data):
|
168 |
+
# 动态创建命令回调
|
169 |
+
async def dynamic_command(interaction: discord.Interaction, **kwargs):
|
170 |
+
# 可以根据不同的命令执行不同的逻辑
|
171 |
+
if command_data["name"] == "greet":
|
172 |
+
name = kwargs.get("name")
|
173 |
+
await interaction.response.send_message(f"Hello, {name}!")
|
174 |
+
elif command_data["name"] == "sum":
|
175 |
+
a = kwargs.get("a")
|
176 |
+
b = kwargs.get("b")
|
177 |
+
result = a + b
|
178 |
+
await interaction.response.send_message(f"The sum is: {result}")
|
179 |
+
|
180 |
+
# 动态设置命令参数
|
181 |
+
command_params = []
|
182 |
+
for param_name, param_data in command_data["params"].items():
|
183 |
+
if param_data["type"] == "str":
|
184 |
+
param_type = str
|
185 |
+
elif param_data["type"] == "int":
|
186 |
+
param_type = int
|
187 |
+
else:
|
188 |
+
raise ValueError(f"Unsupported parameter type: {param_data['type']}")
|
189 |
+
|
190 |
+
command_params.append(
|
191 |
+
app_commands.Parameter(
|
192 |
+
name=param_name,
|
193 |
+
description=param_data["description"],
|
194 |
+
type=param_type
|
195 |
+
)
|
196 |
+
)
|
197 |
+
|
198 |
+
# 创建并注册命令
|
199 |
+
command = app_commands.Command(
|
200 |
+
name=command_data["name"],
|
201 |
+
description=command_data["description"],
|
202 |
+
callback=dynamic_command,
|
203 |
+
parameters=command_params
|
204 |
+
)
|
205 |
+
# 将命令添加到命令树
|
206 |
+
self.tree.add_command(command)
|
207 |
+
|
208 |
+
|
209 |
+
bot = MyClient(intents=intents)
|
210 |
|
211 |
|
212 |
@bot.event
|