简体   繁体   English

如何使用 Python 中的 Discord 机器人获取用户输入?

[英]How do I grab user input with my Discord bot in Python?

I would like to grab user input from the user when they enter a certain command and store the input into a variable (but not the command) so that I can do things with it.我想在用户输入某个命令时从用户那里获取用户输入并将输入存储到一个变量(但不是命令)中,以便我可以用它做事。

As an example, when the user types "!id 500", I want the bot to type "500" in the channel.例如,当用户输入“!id 500”时,我希望机器人在频道中输入“500”。 I have tried just using message.content but it doesn't work properly because the bot spams it indefinitely, plus it shows the command as well.我试过只使用 message.content 但它不能正常工作,因为机器人无限期地向它发送垃圾邮件,而且它还显示命令。

@client.event
async def on_message(message):
    if message.content.startswith("!id"):
        await client.send_message(message.channel, message.content)

Sorry if this is a silly question, but any help would mean a lot.对不起,如果这是一个愚蠢的问题,但任何帮助都意味着很多。

If you want the bot to just send the message of the user without the command part ("!id") you'll have to specify which part of the string you want to send back.如果您希望机器人只发送用户的消息而没有命令部分(“!id”),您必须指定要发回的字符串部分。 Since strings are really just lists of characters you can specify what part of the string you want by asking for an index.由于字符串实际上只是字符列表,因此您可以通过请求索引来指定所需的字符串部分。
For example:例如:

"This is an example"[1]

returns "h".返回“h”。

In your case you just want to get rid of the beginning part of the string which can be done like so:在您的情况下,您只想摆脱字符串的开头部分,可以这样做:

@client.event
async def on_message(message):
    if message.content.startswith("!id"):
        await client.send_message(message.channel, message.content[3:])

this works because it sends the string back starting with the 4th index place (indexes start at 0 so really [3] is starting at the fourth value)这是有效的,因为它从第 4 个索引位置开始发送字符串(索引从 0 开始,所以实际上 [3] 从第四个值开始)

This also applies if you just want to save the user's message as a variable as you can just do this:如果您只想将用户的消息保存为变量,这也适用,因为您可以这样做:

userInput = message.content[3:]

You read more about string manipulation at the python docs .您可以在python 文档中阅读有关字符串操作的更多信息。

I would use commands extension - https://github.com/Rapptz/discord.py/blob/async/examples/basic_bot.py我会使用命令扩展 - https://github.com/Rapptz/discord.py/blob/async/examples/basic_bot.py

Then make a command like,然后发出这样的命令,

@bot.command(pass_context=True)
async def id(ctx, number : int):
    await bot.say(number)

The reason your bot spams the text is because your bot is saying the command, which it follow.您的机器人发送垃圾邮件的原因是您的机器人正在说出它所遵循的命令。 You need to block your bot from counting your output as a command.您需要阻止您的机器人将您的输出计算为命令。 You could block it by:您可以通过以下方式阻止它:

if message.author.bot: return . if message.author.bot: return

This will return None if the author is a bot.如果作者是机器人,这将返回 None。 Or, you could just block your bot's own id.或者,您可以阻止您的机器人自己的 ID。 You won't need to do this if you actually get your command to work correctly, but you should still add it.如果您确实让命令正常工作,则不需要这样做,但您仍然应该添加它。 Second of all, the way I separate the command and arguments is其次,我将命令和参数分开的方式是

cmd = message.content.split()[0].lower()[1:]
args = message.content.split()[1:]

With these variables, you could do有了这些变量,你可以做

@client.event
async def on_message(message):
    if message.content.startswith('!'):
        if message.author.bot: return # Blocks bots from using commands.
        cmd = message.content.split()[0].lower()[1:]
        args = message.content.split()[1:]
        if command == "id":
            await client.send_message(message.channel, ' '.join(args)

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

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