【问题标题】:custom prefix discord.py, json file自定义前缀 discord.py,json 文件
【发布时间】:2021-11-01 15:25:32
【问题描述】:

我查看了所有的教程,所有的论坛,他们都对不和谐机器人的自定义前缀说了同样的话。 我已经尝试过了,但它不起作用。 我什至试图复制粘贴代码并更改名称。 不知道现在该做什么,这是我的代码:

def get_prefix(client, message):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)
    return prefixes[str(message.guild.id)]

client = commands.Bot(command_prefix = get_prefix, help_command = None)
slash = SlashCommand(client, sync_commands = True)

@client.event
async def on_ready():
    activity = discord.Game(name="!help", type=3)
    await client.change_presence(activity=activity)
    print('online !')

@client.event
async def on_guild_join(guild):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)

    prefixes[str(guild.id)] = '!'

    with open('prefixes.json', 'w') as f:
        json.dump(prefixes, f, indent=4)

@client.event
async def on_guild_remove(guild):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)

    prefixes.pop(str(guild.id))

    with open('prefixes.json', 'w') as f:
        json.dump(prefixes, f, indent=4)

这是我的错误信息:

Ignoring exception in on_guild_join
Traceback (most recent call last):
  File "C:\Users\ethom\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\ethom\PycharmProjects\discord\rocket_bot\rocket.py", line 32, in on_guild_join
    prefixes = json.load(f)
  File "C:\Users\ethom\AppData\Local\Programs\Python\Python38\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "C:\Users\ethom\AppData\Local\Programs\Python\Python38\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\ethom\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\ethom\AppData\Local\Programs\Python\Python38\lib\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)

【问题讨论】:

    标签: python json discord.py prefix


    【解决方案1】:

    我已经回答了我自己的问题。 它说没有找到json文件,但实际上,这不是代码问题。 这是一个文件问题,我删除了__pycache__ 文件,它突然可以正常工作了。 所以,我的结论是你的 bot 文件夹中必须没有目录。

    所以,祝你有美好的一天!希望我的回复对你有用。

    【讨论】:

      【解决方案2】:

      在大多数情况下,您的 json.load - JSONDecodeError: Expecting value: line 1 column 1 (char 0) 错误是由于:

      1. 不符合 JSON 的引用
      2. XML/HTML 输出(即以
      3. 字符编码不兼容

      最终错误告诉你,字符串在第一个位置已经不符合 JSON。

      尝试调试以验证您阅读的文件的内容:

      @client.event
      async def on_guild_join(guild):
          with open('prefixes.json', 'r') as f:
              #Verify the content is really "{}"
              print(f.read())
              prefixes = json.load(f)
              
          prefixes[str(guild.id)] = '!'
      
          with open('prefixes.json', 'w') as f:
              json.dump(prefixes, f, indent=4)
      

      如果此验证未通过,您需要将“prefixes.json”的修复路径指定为“complete_path/prefixes.json”以确保读取正确的文件。

      如果不是路径问题,你也可以尝试在读取文件时更改编码:

       with open('prefixes.json', 'r') as f:
            prefixes = json.loads(f.read().encode("utf-8"))
      

      【讨论】:

      • 在它刚刚拥有的 json 文件上{}
      • 如果您想为所有用户定义一个简单的自定义前缀,您需要更改: bot = commands.Bot(command_prefix="$") 如果您想为每个用户定义一个自定义前缀,您将需要在开始时定义一个默认前缀,然后添加一个手动更改前缀的命令,实际上可以存储在一个 JSON 文件中。
      • 还有,你能把代码发过来吗?因为我没听懂你说的XD
      • 你到底想做什么我不清楚?什么或谁的自定义前缀?之后我将使用功能代码编辑我的回复。
      • 好的,我想为我的机器人做一个自定义前缀,可以通过命令自定义:!prefix (newprefix);每个服务器的前缀可以不同。喜欢这个视频:youtube.com/watch?v=yrHbGhem6I4&t=533s
      猜你喜欢
      • 2021-03-31
      • 2021-04-04
      • 2020-12-31
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 2021-03-16
      • 2023-01-20
      相关资源
      最近更新 更多