【问题标题】:How to kick users on command如何按命令踢用户
【发布时间】:2018-02-16 21:35:42
【问题描述】:

我对 Python 了解不多,还在学习中。

我一直在大幅修改一个用 Python 2.7 编码的开源 Discord 机器人。

我想添加一个功能,允许我根据命令踢用户。

类似的东西 [commandprefix]kickuser [userid] 但是我不知道如何让机器人从我发送的消息中获取用户 ID,并且当我尝试使其超级特定以踢我的第二个帐户作为测试时,它也不起作用。

    if message_content == ',purgeIdiots':
        await kick('userid')
        return

这是我在文档中手动输入用户 ID 的超特定代码。但我无法让它工作。

我是新手,希望能得到一些帮助。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    如果您正在研究创建命令的过程,我建议您进一步阅读有关 discord.py 的 discord.ext.commands 子库的信息。

    这是一个关于它的常见问题解答,以及使用它的机器人示例如下:

    FAQ

    Bot Example

    此扩展将允许您使用前缀创建命令。

    关于您关于通过用户 id 踢的问题,您可以使用命令扩展执行以下操作:

    @bot.command(pass_context = True)
    async def kick(ctx, userName: discord.User):
        await bot.kick(userName)
    

    我相信这应该可行,但我还不能编译它来检查。但是,请务必了解有关命令扩展的更多信息,因为它对您的帮助远不止检查消息的内容。

    您首先需要导入 discord.ext,您可以使用程序顶部的from discord.ext import commands 来完成。

    然后您需要定义 bot 以确保您可以使用像 @bot.command 这样的东西,因为您需要它。这样做是这样的:bot = commands.Bot(command_prefix=',', description=description),现在逗号被定义为命令前缀。

    这应该允许我最初添加的代码 sn-p 与能够键入 ,kick <username> 的用户一起使用。

    【讨论】:

    • 谢谢你!对此,我真的非常感激。是的,我肯定也会查看您发送给我的链接。
    • 原来我不太确定如何使用它。我删除了 '@bot.command(pass_context = True)' 行,因为我收到错误说'bot'尚未定义。我用@owner_only 替换了它,当我输入'kick'时它似乎没有做任何事情。所以我在 kick 前面添加了 'cmd_' 但同样,当我输入 ",kick (player)" (逗号是我的命令前缀)时,它仍然不起作用。如果你能给我一个如何使用它的例子,我真的很感激。谢谢!
    • 当然!我将编辑我的答案以反映您的问题,因为我这里没有足够的空间。
    【解决方案2】:

    这是我在我的机器人中使用的踢命令 注意:你需要在写下面的命令之前写这个东西==> from discord.ext.commands import has_permissions, CheckFailure, BadArgument

    @bot.command(pass_context=True, name="kick")
    

    @has_permissions(kick_members=True)

    async def kick(ctx, *, target: Member):

    if target.server_permissions.administrator:
    
        await bot.say("Target is an admin")
    
    else:
        try:
            await bot.kick(target)
            await bot.say("Kicked")
        except Exception:
            await bot.say("Something went wrong")
    

    @kick.error

      async def kick_error(error, ctx):
    

    if isinstance(error, CheckFailure):

         await bot.send_message(ctx.message.channel, "You do not have permissions")
    
    elif isinstance(error, BadArgument):
    
        await bot.send_message(ctx.message.channel, "Could not identify target")
    
    else:
        raise error
    

    所以现在命令@bot.command(pass_context=True)

    @has_permissions(kick_members=True) ==> 它检查是否 使用该命令的用户是否具有该权限。其余部分是不言自明的。 @kick.error 部分检查该 kick 命令的错误。注意:如果在第一部分中你对 async def kick_command @kick.error 你必须纠正@kick_command.error。

    另请注意: 在你的机器人命令中你写了@client=command.Bot(command_prefix='YOUR_PREFIX')

    在@bot.command() 您只需将@bot 更改为您在该@client 中编写的内容: 例如。如果你写了@mybot.command_Bot(command_prefix='YOUR_PREFIX') 你必须将@bot 更改为@mybot.command()。如果您有任何问题,请随时提出

    【讨论】:

      【解决方案3】:
      @commands.has_permissions(kick_members=True)
      @bot.command()
      async def kick(ctx, user: discord.Member, *, reason="No reason provided"):
              await user.kick(reason=reason)
              kick = discord.Embed(title=f":boot: Kicked {user.name}!", description=f"Reason: {reason}\nBy: {ctx.author.mention}")
              await ctx.message.delete()
              await ctx.channel.send(embed=kick)
              await user.send(embed=kick)
      

      【讨论】:

        猜你喜欢
        • 2021-08-04
        • 2021-06-11
        • 2021-01-15
        • 2021-06-01
        • 2019-05-13
        • 2021-07-27
        • 2021-11-13
        • 2019-10-04
        • 2020-09-16
        相关资源
        最近更新 更多