如何按命令踢用户

2022-01-15 00:00:00 python discord discord.py

问题描述

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

I don't have much knowledge on Python, and still in the process of learning it.

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

I have been significantly modifying an open-source Discord bot coded in Python 2.7.

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

I would like to add a feature that allows me to kick a user based on a command.

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

Something like [commandprefix]kickuser [userid] But I have no idea how to make the bot grab the userid from the message I sent, and when I try to make it ultra-specific to kick my second account as a test, it doesn't work either.

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

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

That's the ultra-specific code where I manually enter a userID into the document. But I cannot get it to work.

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

I'm extremely new and I'd appreciate some help.


解决方案

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

If you're looking into the process of making commands, I'd suggest looking further into reading about the discord.ext.commands sub-library of discord.py.

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

Here's an FAQ about it, and an example of a bot using it following:

常见问题解答

机器人示例

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

This extension will allow you to create commands using prefixes.

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

In regards to your question about kicking via user id, with the command extension you could do something like:

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

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

I believe that should work, but I can't compile it just yet to check. However, do learn more about the command extension as it'll help you out a lot more than checking messages for content.

您首先需要导入 discord.ext,您可以使用程序顶部的 from discord.ext import commands 来做到这一点.

You'll first need to import discord.ext, you can do that with from discord.ext import commands at the top of the program.

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

You'll then need to define bot to ensure you can use stuff like @bot.command, because you'll need that. This is done like this: bot = commands.Bot(command_prefix=',', description=description), with the comma being defined as the command prefix now.

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

This should allow the code snippet I added originally to function with a user being able to type ,kick <username>.

相关文章