我正在使用discord.py制作一个bot。如果来自任何用户的消息(在bot存在的服务器上)是Integers
,Characters
和Strings
的mixture
,例如122-PG-CSAI-2022
,我希望bot能够触发。
我该怎么做?
我不确定你认为在这种情况下char
和string
之间的差异是什么,但这段代码将检查它是否包含至少一个字母和一个int
:
on_message(message):
has_number = any(char.isdigit() for char in message.content)
has_non_number = any(char.islapha() for char in message.content)
if has_number and has_non_number:
# do something
编辑:
on_message(message):
splits = message.content.split("-")
# it's not in the wanted format
if len(splits) != 4:
# return or something else
cont = True
# check if first is a number within range <1, 999>
try:
if int(splits[0]) <= 0 and int(splits[0] >= 1000:
cont = False
except ValueError:
cont = False
# check if second is one of the options
if splits[1] not in ["UG", "PG", "D"]:
cont = False
# check if third is one of the options
if splits[2] not in ["some", "options", "here"]:
cont = False
# check if fourth is a number from range <2022 ...>
try:
if int(splits[3]) < 2022:
cont = False
except ValueError:
cont = False
# it fits all the criteria
if cont:
# do stuff
对于整数、字符和字符串,我假设您的意思是没有特殊字符(即@、!、*、&等)。
on_message(message):
for char in message.content:
if not char.isdigit() and not char.isalpha(): return False
#trigger bot here