如何根据命令踢用户

How to kick users on command

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

我一直在大量修改 Python 2.7 中编码的开源 Discord 机器人。

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

有点像 [命令前缀]kickuser [用户 ID] 但我不知道如何让机器人从我发送的消息中获取用户 ID,当我尝试使其超特定以启动我的第二个帐户作为测试时,它也不起作用。

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

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

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

如果您正在研究制作命令的过程,我建议您进一步阅读 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.command 之类的东西,因为您需要它。这样做是这样的:bot = commands.Bot(command_prefix=',', description=description),逗号现在被定义为命令前缀。

这应该允许我最初添加的代码片段在用户能够键入 ,kick <username>.

的情况下运行

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

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

@has_permissions(kick_members=真)

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(错误,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 parts 检查那个 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()。如果您有任何问题,请随时提问

@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)