【问题标题】:(Python)Facing an error while building a bot(Python)构建机器人时遇到错误
【发布时间】:2021-09-14 16:43:31
【问题描述】:

这是我为运行我的 Discord Bot 而编写的代码。它告诉嵌入中 Minecraft 服务器的当前状态。

import discord
import os
import requests
from discord.ext import commands

client = commands.Bot(command_prefix = "//")

@client.command()
async def mc(ctx, arg):    
   url = "https://api.mcsrvstat.us/2/"
   data = requests.get(url + arg ).json()
   print(data)
   ip = data["ip"]
   port = data["port"]
   ping = str(data["debug"]["ping"])
   motd = data["motd"]["clean"]
   onlineplayers = str(data["players"]["online"])
   onlineplayersname = data["players"]["list"]
   version = str(data["version"])
   online = str(data["online"])
   protocol = str(data["protocol"])
   hostname = data["hostname"]

   embed = discord.Embed(
     title = arg + " Server Info",
     description = motd,
     color = 0xff00,
   )

   embed.add_field(
     name = "I.P. Address",
     value = ip
   )
   embed.add_field(
     name = "Port",
     value = port
   )

   embed.add_field(
     name = "Can server be pinged?",
     value = ping
   )

   embed.add_field(
     name = "Number of players online",
     value = onlineplayers
   )

   if onlineplayers>6:
     print("lorem ipsum")

   else:
    embed.add_field(
      name = "Username of people online",
      value = onlineplayersname
    )

   embed.add_field(
     name = "Minecraft version of server",
     value = version
   )

   embed.add_field(
     name = "Is the server currently online",
     value = online
   )

   embed.add_field(
     name = "Protocol being used by the server",
     value = protocol
   )

   embed.add_field(
     name = "Hostname",
     value = hostname
   )
   embed.set_thumbnail(
     url = "https://wallpapercave.com/wp/wp8105351.jpg"
   )

   await ctx.send(embed = embed)


client.run(os.getenv('ENCRYPT'))

我得到的错误:

Ignoring exception in command mc:
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 24, in mc
    data = requests.get(url + arg ).json()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 5 (char 4)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/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: Extra data: line 1 column 5 (char 4)

请告诉我这是什么意思。我尝试过多次,但我的猜测是 JSON 函数无法处理转换如此大量的文件。请帮忙!

【问题讨论】:

  • 你是如何调用命令的?
  • 请求的响应是什么?
  • 示例 - //mc hypixel.net ,其中 // 是命令前缀,mc 是命令名称,hypixel.net 是用户选择的随机服务器
  • 目前我没有得到任何回应,只是我上面提到的错误。
  • 这适用于我本地...我不确定问题出在哪里,但我建议您尝试使用 aiohttp 而不是请求,因为如果服务器需要一段时间来响应,您将冻结机器人使用非异步函数。

标签: python json python-3.x discord.py bots


【解决方案1】:

错误在json文件中,在第1行第5列检查,确保语法正确与否。

【讨论】:

    猜你喜欢
    • 2021-05-22
    • 2021-11-03
    • 1970-01-01
    • 2021-12-14
    • 2021-04-27
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多