[英]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.