【问题标题】:unexpected unindent (Discord.py)意外的缩进(Discord.py)
【发布时间】:2021-12-18 13:21:29
【问题描述】:

Discord.py 的代码中有一个“意外的缩进”,代码是:

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

完整代码:

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)



【问题讨论】:

  • 您的@client.command() 显然缩进不正确。它应该与下一行的async def kick(ctx, member: discord.Member, reason=None): 处于相同的缩进级别。
  • 第一个sn-ps和第二个sn-ps的缩进不同,需要打个minimal reproducible example

标签: python discord discord.py


【解决方案1】:

错误

Line 16

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

很明显第一行没有正确缩进。

line 24

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

这里,第三行没有正确缩进。

修复缩进,您的代码应如下所示

import os
import discord
import json

from discord.ext import commands
from discord.ext.commands import Bot
from discord.ext.commands import has_permissions, MissingPermissions, is_owner


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)

【讨论】:

    【解决方案2】:

    这不是 discord.py 问题。 你的缩进有缺陷

    第 16 行

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

    应该是

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

    如果这不能解决您的问题,请检查您是否使用制表符而不是空格,反之亦然。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 2020-03-03
      • 1970-01-01
      • 2018-02-04
      相关资源
      最近更新 更多