抱歉,这个奇怪的问题我不能以其他方式提交。我在试着做一个聊天机器人命令。但我总是犯愚蠢的错误。
这是我的密码:
@client.command()
async def chatbot(ctx, *, msg):
chatbot1 = requests.get(f"https://chatbot-api.therealenny1.repl.co/?message={quote(msg)}")
resp = chatbot1.json()
await ctx.send(resp)以下是错误:
Ignoring exception in command chatbott:
Traceback (most recent call last):
File "/home/runner/m/venv/lib/python3.8/site-packages/requests/models.py", line 972, in json
return complexjson.loads(self.text, **kwargs)
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/runner/m/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 50, in chatbott
resp = chatbot1.json()
File "/home/runner/m/venv/lib/python3.8/site-packages/requests/models.py", line 976, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/runner/m/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/home/runner/m/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/home/runner/m/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: JSONDecodeError: Expecting value: line 1 column 1 (char 0)修好了。
修正:
@client.command()
async def chatbot(ctx, msg):
url = f"https://chatbot-api.therealenny1.repl.co/?message={quote(msg)}"
data = requests.get(url)
await ctx.send(data.text)发布于 2022-06-21 20:55:13
虽然识别json错误并不总是那么容易,但您所得到的错误是在第一个字符上。Expecting value: line 1 column 1 (char 0),我猜你根本没有收到jsone消息。首先,检查resp.status_code,看看您是否收到了200个响应或其他内容。您还可以通过使用resp.text打印/记录响应的文本来进行调试,这可能会给您带来额外的线索。
发布于 2022-06-22 18:26:56
找到了解决办法。
@client.command()
async def chatbot(ctx, msg):
url = f"https://chatbot-api.therealenny1.repl.co/?message={quote(msg)}"
data = requests.get(url)
await ctx.send(data.text)https://stackoverflow.com/questions/72706959
复制相似问题