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


基础教程推荐
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 包装空间模型 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01