You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.py运行出现unexpected unindent错误该如何解决?

错误原因定位
  • "unexpected unindent"属于Python基础语法错误,由代码缩进层级不匹配触发
  • 你提供的代码中共存在两处缩进问题:
    1. kick函数的@client.command()装饰器行前方多了2个多余空格,和同层级的ban函数装饰器、@client.event等缩进层级不一致
    2. on_ready事件回调内的print语句没有匹配函数的缩进层级,缺少4个前置空格
修复方案
  • 删除@client.command()(装饰kick函数的那行)前方的所有多余空格,和其他顶层装饰器对齐
  • 给print('we have logged in as {0.user}'.format(client))前方加4个空格,匹配async def on_ready():的缩进层级
  • 后续开发建议统一使用4个空格作为缩进单位,不要混用空格和制表符,规避同类缩进问题
修正后的完整代码
import os
import discord
from discord.ext import commands
from discord.ext.commands import Bot
from discord.ext.commands import has_permissions, MissingPermissions, is_owner
import json

client = commands.Bot(command_prefix='.')
status=discord.Status.idle

@client.command()
async def ban(ctx, member: discord.Member, reason=None):
    await member.ban(reason=reason)
    await ctx.send("The Member Was Banned.")

@client.command()
async def kick(ctx, member: discord.Member, reason=None):
    await member.kick(reason=reason)
    await ctx.send("The Member Was Kicked.")

  
@client.event
async def on_ready():
    print('we have logged in as {0.user}'.format(client))


my_secret = os.environ['Token']

client.run(my_secret)

内容的提问来源于stack exchange,提问作者Mixu lol

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 22:18:02