[英]How can I make my bot execute the same action while using different messages?
Let's say I have a list.假设我有一个清单。
List = ["Hello", "Sup", "Hi", "Yo"]
I want my bot to send the same message even if I use any of these words.即使我使用这些词中的任何一个,我也希望我的机器人发送相同的消息。 If the person says Hello, bot will reply with Hello, If the user says Hi, bot will reply with Hello, and so on.
如果这个人说你好,机器人会回复你好,如果用户说你好,机器人会回复你好,依此类推。
Now, I want to use message.content.startswith for this case, so that if the person types Hello along with other things (for example "Hello bot! Nice to see you.") the bot will still reply.现在,我想在这种情况下使用 message.content.startswith,这样如果用户输入 Hello 以及其他内容(例如“Hello bot!很高兴见到你。”),机器人仍会回复。 I tried to use message.content, but if I have other words besides the "key" one, the bot won't reply.
我尝试使用 message.content,但如果除了“关键”之外我还有其他词,机器人将不会回复。
I tried我试过
List = ["Hello", "Sup", "Hi", "Yo"]
if message.content.startswith(List):
await bla bla bla
but I got an error saying that the first arg.但我收到一个错误,说第一个 arg. for startswith cannot be a list.
因为startswith 不能是一个列表。 And so yeah, that's basically it.
所以是的,基本上就是这样。 I tried using the "any" function, but I noticed that's not really what I need (as far as I'm concerned, perhaps it is and I am wrong).
我尝试使用“any”函数,但我注意到这并不是我真正需要的(就我而言,也许是这样,但我错了)。 But this is basically what I want: the bot to reply with the same thing whether if the person says Hello, Sup, Hi, etc...
但这基本上是我想要的:无论这个人说你好,Sup,Hi 等,机器人都用同样的东西回复......
if message.content.split()[0] is in List:
#Do something
This should work and allows you to change whatever in your list and it still work这应该有效并允许您更改列表中的任何内容并且它仍然有效
You could also use message.split()[0].upper() and make sure every word in the list is fully uppercase so it doesnt matter what case the user enters您还可以使用 message.split()[0].upper() 并确保列表中的每个单词都完全大写,因此用户输入的大小写无关紧要
str.startswith()
can take a tuple
as an argument, so you can just do str.startswith()
可以将tuple
作为参数,所以你可以这样做
tup = ("Hello", "Sup", "Hi", "Yo",)
if message.content.startswith(tup):
# do something
Best practices suggest using a different variable name than List.最佳实践建议使用与 List 不同的变量名。 That said:
那说:
if message.split(' ')[0] is in List:
await bla bla bla
will check if the first word of the message is in your list, and if so then execute the rest of your code.将检查消息的第一个单词是否在您的列表中,如果是,则执行其余代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.