问题描述
我现在正在制作一个不和谐机器人,如果我使用大写字母/不使用大写字母,它不会响应.机器人有没有办法检测消息,即使它们是大写/小写的?机器人还有没有办法对空格做同样的事情(如果我不小心忘记放空格,或者不小心将两个单词合并在一起......)?
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 机器人可以不区分大小写吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!