Discord.py - 机器人无法踢球

Discord.py - Bot unable to kick

我发现我无法使用我的 bot 中的 kick 命令来踢成员或其他 bot。我和机器人都有管理员权限。为什么会这样?我没有编译错误。

@client.command() ##kick
@has_permissions(kick_members = True) # to check the user itself
async def kick(ctx, member : discord.Member, *, reason=None):
    try:
        await member.kick(reason=reason)
        await ctx.send(+member.mention + " has been sent to the ministry of love for reeducation.")
    except:
        await ctx.send("You cannot!")

编辑:感谢您在下方评论中提供的修复。我也开始意识到我试图踢一个同等级别的用户(已弃用的机器人),因此这也在其中发挥了作用,但无法正常工作。我试着踢一个标准用户,效果很好!

@has_permissions(kick_members = True) 行中,您必须添加 commands。这是固定命令。

@client.command()
@commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
    try:
        await member.kick(reason=reason)
        await ctx.send(+member.mention + " has been sent to the ministry of love for reeducation.")
    except:
        await ctx.send("You cannot!")
import discord
from discord.ext import commands
import os

client = commands.Bot(command_prefix = '!')

@client.command()
@commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
  await member.kick(reason=reason)
  await ctx.send(f'User {member} has been kicked')
  await member.send(f"You have been kicked from {member.guild.name} | reason: {reason}")

client.run('WRITE YOUR TOKEN HERE')

猜测从机器人收到的消息是“你不能!”,抛出的异常是TypeError,因为你已经包装了它而被处理在 try/except。为了确定,删除错误处理并检查您的结果。

完整的错误如下所示:TypeError: bad operand type for unary +: 'str'.

资源


错误

在第 6 行中,您在参数列表的开头添加了一个错误的二元运算符 +,但该运算符需要两个操作数,但未提供。

await ctx.send(+member.mention + " has been sent to the ministry of love for reeducation.")

因此 TypeError 被抛出,但它也被处理,所以你将看到的唯一结果是“你不能!”来自机器人的消息。

修复

简单地删除错误的运算符,它应该可以正常工作。

await ctx.send(member.mention + " has been sent to the ministry of love for reeducation.")