【发布时间】: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