简体   繁体   English

discord python bot kick 命令

[英]discord python bot kick command

i am trying to make a discord bot that can kick people with the command .kick我正在尝试制作一个不和谐的机器人,它可以用命令 .kick 踢人
I have it display a message that says (username) has been kicked from the server and the message still shows up, but it doesn't actually kick them.我让它显示一条消息,说(用户名)已被从服务器踢出并且该消息仍然显示,但实际上并没有踢他们。

Here is my code:这是我的代码:

    import discord
    from discord.ext import commands
    
    client = commands.Bot(command_prefix=".")
    
    @client.event
    async def on_ready():
        print("Bot is Ready")
        
    @client.command(aliases=['c'])
    @commands.has_permissions(manage_messages = True)
    async def clear(ctx,amount=2):
        await ctx.channel.purge(limit = amount)
     
    @client.command(aliases=['k'])
    @commands.has_permissions(kick_members = True)
    async def kick(ctx,member : discord.Member,*,reason= "I do not need a reason"):
        await ctx.send(member.name + " has been kicked from the server, because "+reason)
        await member.kick(reason=reason)

    @client.command(aliases=['b'])
    @commands.has_permissions(ban_members = True)
    async def ban(ctx,member : discord.Member,*,reason= "I do not need a reason"):
        await ctx.send(member.name + " has been banned from the server, because"+reason)
        await member.ban(reason=reason)

the error message says:错误消息说:

    File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
        ret = await coro(*args, **kwargs)
      File "C:\Users\user\discordbot\bot.py", line 18, in kick
        await member.kick(reason=reason)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\member.py", line 512, in kick
        await self.guild.kick(self, reason=reason)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\guild.py", line 1849, in kick
        await self._state.http.kick(user.id, self.id, reason=reason)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\http.py", line 241, in request
        raise Forbidden(r, data)
    discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions

The above exception was the direct cause of the following exception:上述异常是以下异常的直接原因:

    Traceback (most recent call last):
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
        await ctx.command.invoke(ctx)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
        await injected(*ctx.args, **ctx.kwargs)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
        raise CommandInvokeError(exc) from exc
    discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: 403 Forbidden (error code: 50013): Missing Permissions

please note that my .clear command is working perfectly fine, it is just kick and ban请注意,我的 .clear 命令运行良好,它只是踢和禁止

please help, I am on python 3.9.0请帮忙,我在python 3.9.0上

@client.command()
@commands.has_permissions(administrator=True)
async def kick(ctx, member: discord.Member):
    await member.kick()
    await ctx.message.add_reaction("reaction")

    await ctx.send(f"{member.name} has been kicked by {ctx.author.name}!")

    await log_channel.send(f"{ctx.author.name} has kicked {member.display_name}")

This is what I used for my discord bot.这是我用于我的不和谐机器人的。 It only lets the people with the kick permission use this command.它只允许有踢权限的人使用这个命令。 I hope this helped.我希望这有帮助。

This type of error comes when either user who is giving this command or your bot has not been given such admin permissions.发出此命令的用户或您的机器人未获得此类管理员权限时,就会出现此类错误 In your server you can simply make one role for your bot or can give all privileges to your bot if it is managed by you.在您的服务器中,您可以简单地为您的机器人设置一个角色,或者如果您的机器人由您管理,则可以将所有权限授予您的机器人。

link below may help you regarding this https://support.discord.com/hc/en-us/articles/206029707-How-do-I-set-up-Permissions-下面的链接可以帮助您解决这个问题https://support.discord.com/hc/en-us/articles/206029707-How-do-I-set-up-Permissions-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM