为什么我在 nextcord if 语句中出现缩进错误?

Why am I getting an Indentation Error on a nextcord if statement?

我试图让我的机器人向加入一个特定服务器的用户发送嵌入的 DM,并向任何其他服务器发送一条消息说 'Welcome to Unknown Server'。我试图通过让机器人获取用户加入的服务器的服务器 ID 并将其与我输入的服务器进行比较来做到这一点,但是,我收到此错误:

C:\Users\mnot7>"C:\PC Code\Python Code Files\Discord Bot\Next cord\bot file name - NextCord.py"
  File "C:\PC Code\Python Code Files\Discord Bot\Next cord\bot file name - NextCord.py", line 87
    if  guild_id == '442806943153651722':
                                         ^
IndentationError: unindent does not match any outer indentation level

我的代码:

@bot.event
async def on_member_join(member):
        guild_id = bot.get_guild(guild_id)
        channel = bot.get_channel(channel id here)
    if  guild_id == 'server id here':
        await channel.send(f"<@my user id> {member} has joined Server name example!")
        await member.send("Welcome to Server name example!")

        em = nextcord.Embed(title="Example", description="Example", color=0xFF0000)
        em.add_field(name="Example", value="Example", inline=False)
        await member.send(embed=em)
    else:
        await channel.send(f"<@my user id> {member} has joined `Unknown Server`!")
        await member.send("Welcome to `Unknown Server`!")

是否有更好的方法来执行此操作?如果没有,为什么我会收到错误消息?我已经检查过我只使用制表符进行缩进。

您代码中的 ifelse 缩进不正确,它们需要与函数下的其余行处于同一级别——但是,您想要缩进的行if 里面的 execute 应该缩进。

@bot.event
async def on_member_join(member):
        guild_id = bot.get_guild(guild_id)
        channel = bot.get_channel(channel id here)
        if  guild_id == 'server id here':
            await channel.send(f"<@my user id> {member} has joined Server name example!")
            await member.send("Welcome to Server name example!")

            em = nextcord.Embed(title="Example", description="Example", color=0xFF0000)
            em.add_field(name="Example", value="Example", inline=False)
            await member.send(embed=em)
        else:
            await channel.send(f"<@my user id> {member} has joined `Unknown Server`!")
            await member.send("Welcome to `Unknown Server`!")

这应该可行,确保有一致的缩进标准(例如相同的空格数,始终使用制表符)。