discord.py 更改前缀命令缩进错误

时间:2021-01-15 20:45:21

标签: python discord discord.py

<块引用>

我使用 python 3.8.7

我目前正在开发一个不和谐的机器人,我试图发出更改机器人前缀的命令,但出现此错误:

File "d:/Documents/Discord/Bot/Giveaway Bot/bot.py", line 53
    prefixes[str(message.guild.id)] = '>'
                                        ^
IndentationError: unindent does not match any outer indentation level

我尝试了很多东西,但似乎不起作用。该错误没有很好地解释。这似乎不是缩进错误。它是别的东西。

from discord.ext import commands
import json

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




client = commands.Bot(command_prefix= get_prefix)


@client.event 
async def on_ready():
    print('Ready')




@client.event
async def on_guild_join(guild):
     with open('prefix.json', 'r') as f:
        prefixes = json.load(f)
    
    prefixes[str(message.guild.id)] = '>'


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


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

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



@client.command()
async def changeprefix(ctx, prefix):
    with open('prefix.json', 'r') as f:
        prefixes = json.load(f)
    
    prefixes[str(ctx.guild.id)] = prefix


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

    await ctx.send(f'The prefix was changed to {prefix}', delete_after=30.0)

有什么办法可以解决吗?

1 个答案:

答案 0 :(得分:0)

@client.event
async def on_guild_join(guild):
     with open('prefix.json', 'r') as f:
        prefixes = json.load(f)
    
    prefixes[str(message.guild.id)] = '>'


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

with open('prefix.json', 'r') as f: 缩进 1 个空格。

每当您看到 IndentationError 时,请检查每行前面的空格数。我也推荐一个像 Kate 这样带有缩进线的编辑器。