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 机器人可以不区分大小写吗?
基础教程推荐
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
