简体   繁体   English

如何让我的机器人在使用不同的消息时执行相同的操作?

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

相关问题 有什么方法可以让我的 discord 机器人在不同的服务器上发送相同的消息(不跳过旧消息) - Is there any way I can make my discord bot send same messages(without skipping the older messages) in different servers 如何让 Telegram 机器人收听来自与 Python 的不同聊天的消息? - How can I make a Telegram bot listen to messages from a different chat with Python? 如何让我的 Discord Bot 删除频道中的所有消息? - How can I make my Discord Bot delete all messages in a channel? 如何让我的 discord 机器人从 a.txt 文件(python)发送消息 - How can I make my discord bot send messages from a .txt file (python) 如何让我的 Discord Bot 对列表中的不同单词回复相同的答案? Python - How do I make my Discord Bot reply a same answer to different words from a list? Python 如何在使用命令树时让 discord.py 机器人离开 - How Can I Make a discord.py Bot Leave While Using a Command Tree 如何使用 Tkinter 使两个不同的事件触发相同的操作? - How can I make two different events trigger the same action with Tkinter? 如何制作一个回复不同关键字的 Twitter 机器人? - How can I make a Twitter bot that replies to different keywords? 如何让我的 discord 机器人理解我的单词列表? 并执行它? - How do I make my discord bot understand my word list? And execute it? 如何让我的机器人更快地弹奏钢琴曲? - How can I make my bot that plays piano tiles faster?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM