問題描述
我現在正在制作一個不和諧機器人,如果我使用大寫字母/不使用大寫字母,它不會響應.機器人有沒有辦法檢測消息,即使它們是大寫/小寫的?機器人還有沒有辦法對空格做同樣的事情(如果我不小心忘記放空格,或者不小心將兩個單詞合并在一起......)?
I'm making a discord bot right now, and it won't respond if I use capitals/don't use capitals. Is there a way for a bot to detect messages even if they're capital/lowercase? Is there also a way for a bot to be able to do the same with spaces (if I accidentally forget to put a space, or accidentally merge two words together...)?
還是我必須對 if 語句進行無窮無盡的跟蹤?
Or do I have to make an endless trail of if statements?
推薦答案
假設你在 msg
變量中得到你的消息,那么如果你使用 msg.lower()
您將獲得小寫版本的響應,您可以檢查它以查看命令是什么.
assuming you get your message in the msg
variable, then if you use msg.lower()
you'll get the lowercase version of the response and you can check on that to see what the command was.
另外,運行 msg.replace(' ', '')
將創建一個刪除所有空格的值,您可以檢查此變量的相應值而不是原始值.
also, running msg.replace(' ', '')
would create a value where all spaces are removed, and you can check the corresponding value of this variable instead of the original one.
例如:
msg = 'HeLlO wOrLd'
print(msg.lower().replace(' ', ''))
>> helloworld
假設這是一個命令行界面,我真的建議您遵循內置 argparse 庫中的指南.
assuming this is a command line interface, I truly suggest you follow the guidelines in the builtin argparse library.
請參見此處:https://docs.python.org/3.3/library/argparse.html
這篇關于discord.py 機器人可以不區分大小寫嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!