简体   繁体   English

在我的discord机器人键入特定命令后,如何获得所有用户输入的字符串?

[英]How do I get all user input as a string with my discord bot after they type a certain command?

I have a discord bot made with python. 我有一个用python制作的Discord机器人。 I want it to retrieve everything typed after a user types the !loot command, and then store the number given in a text file based on the string/fruit given. 我希望它检索用户键入!loot命令后键入的所有内容,然后根据给定的字符串/水果将给定的数字存储在文本文件中。

For example, if someone types !loot 100 apples, 200 oranges, 300 plums, I would get the user input "100 apples, 200 oranges, 300 plums" in python as a string. 例如,如果有人输入!loot 100个苹果,200个橙子,300个李子,我将在python中让用户输入“ 100个苹果,200个橙子,300个李子”作为字符串。

My idea after getting this string is to split the string based on commas into a list,so the list would look like [100 apples, 200 oranges, 300 plums]. 收到此字符串后,我的想法是将基于逗号的字符串分成一个列表,因此列表看起来像[100个苹果,200个橙子,300李子]。 After this I could check if the keyword matches a text file and if it does extract number and put it in the file. 之后,我可以检查关键字是否与文本文件匹配,以及是否提取了数字并将其放入文件中。

I tried 我试过了

async def loot(ctx, message):
    messagelist = [x.strip() for x in message.split(',')]
    await bot.say(messagelist)

but the bot only gives the first part,which would be [100] in the above example. 但是漫游器仅给出第一部分,在上面的示例中为[100]。

tldr: take multiple item/number pairs separated by commas and put each number in a different text file based on if the items name matches the file name. tldr:取多个用逗号分隔的项目/编号对,然后根据项目名称是否与文件名匹配,将每个编号放在不同的文本文件中。

A feature of discord.py's commands extension framework makes this easy discord.py的命令扩展框架的功能使此操作变得容易

@bot.command(pass_context=True)
async def loot(ctx,*,message):
    await bot.say(message)

If you notice the * , it tells the library to put everything the user types after it into message as a string. 如果您注意到* ,它将告诉库将用户输入的所有内容都以字符串形式放入message中。

暂无
暂无

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

相关问题 如何使用 Python 中的 Discord 机器人获取用户输入? - How do I grab user input with my Discord bot in Python? 如何在 discord.py 中的命令(例如 User: :send 1234 Bot?1234)后重新发送用户输入? - How do I resend user input after a command (e.g. User: !send 1234 Bot: 1234) in discord.py? 如何让我的 python discord bot 检测到命令中提到的某个用户? - how can I make my python discord bot detect a certain user being mentionned in a command? 我试图让我的 discord 机器人等待用户在执行原始命令后发送另一条消息 - I'm trying to get my discord bot to wait for the user to send another message after they did the original command 如何让我的机器人从用户运行命令的位置获取通道 ID? (不和谐.py) - How do I make my bot get the channel Id from where the user ran the command from? (Discord.py) 如何修复我的 Discord Bot 中的 datetime 命令? - How do I fix my datetime command in my Discord Bot? 如何让我的 discord python 机器人在命令后识别聊天中的响应? - How do I make my discord python bot recognize response in chat after command? 如何获取编写 bot 命令的用户? Discord.py - How do I get the user who wrote the bot command? Discord.py 如何让我的 discord 机器人获取所有服务器中的所有文本通道 ID? - How do I make my discord bot get all text channel id's in all servers? 如何在 python discord 机器人中获取用户输入? - How can I get user input in a python discord bot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM