无法禁止成员离开服务器 .py cog

Not able to ban members out of the server .py cog

            @commands.command()
            @commands.guild_only()
            @commands.has_guild_permissions(ban_members=True)
            async def ban(self, ctx, member: discord.Member, *, reason=None):
                if member.top_role >= ctx.author.top_role:
                    await ctx.send(f"You can only ban members below your role.")         
                    return
                
                
                channel = self.bot.get_channel(864083000211406849)
                embed = discord.Embed(title=f"{ctx.author.name} banned: {member.name}", description=reason)
                await channel.send(embed=embed)
                await ctx.send(embed=embed)
                await member.send(f"You've been banned from : `{ctx.guild}` for `reason`: {reason}.")
                await ctx.guild.ban(user=member, reason=reason)
        
            @ban.error
            async def ban_error(self,ctx, error):
                if isinstance(error, commands.CheckFailure):
                    
                    title=f"{ctx.author.mention} you do not have the required permission(s)/role(s) to run this command!"
                    await ctx.send(title)

所以我希望我的代码能够基本上踢出使用他们的 ID 离开我的服务器的成员。我查看了以前的问题,但似乎对我没有帮助。

您正在将成员输入转换为 discord.Member,它需要在服务器中。而是将其转换为不需要在您的服务器中的 discord.User

async def ban(self, ctx, member: discord.User, *, reason=None):

请记住,当您使用 discord.User 时,您的顶级角色查找和比较将不起作用,您必须事先获取实际成员,如下所示:

server_member = ctx.guild.get_member(member.id)
if server_member != None:
    if server_member.top_role >= ctx.author.top_role:
        await ctx.send(f"You can only ban members below your role.")         
        return

此外,如果您尝试向不与您的机器人共享服务器或已阻止您的机器人等的人发送消息,您也会收到错误消息,我会为此使用 try/except 块:

try:
    await member.send(f"You've been banned from : `{ctx.guild}` for `reason`: {reason}.")
except discord.errors.Forbidden:
    pass