检查消息是否包含 link; Discord.py
Check if message contains a link; Discord.py
我想制作一个 Discord 机器人来检查消息中是否存在潜在的 Discord Nitro 诈骗机器人。至此,检测的相关代码为:
keywords = ["GIFT", "GIFTS", "NITRO", "CATCH", "FOR", "FREE", "@EVERYONE", "@HERE"]
keywords_met = 0
@bot.event
async def on_message(message):
if message.author == bot.user:
return
content = message.content.split()
print(content)
for word in content:
print("Loop iteration")
if word.upper() in keywords:
global keywords_met
keywords_met += 1
if keywords_met >= 3:
channel = bot.get_channel(ID of a mod channel)
await channel.send("<@ID of a role> nitro scam detected")
keywords_met = 0
根据经验,keywords
是那些消息中经常使用的词。但是,我想检查消息是否也包含 link。问题是机器人发送的 URL 经常变化,所以我无法检查一个特定的 link。
一般有没有办法检查 links?
使用方法 in
检查 string.Example 中的子字符串:
if 'https://' in word:
# logic here
我会尝试使用这个逻辑兄弟!
test_string = "https://www.google.com"
test_list = ['.com', '.ru', '.net', '.org', '.info', '.biz', '.io', '.co', "https://", "http://"]
link_matches = [ele for ele in test_list if(ele in test_string)]
if link_matches: # Courtesy of my bro Jyr!
print("I got a match bro")
我想制作一个 Discord 机器人来检查消息中是否存在潜在的 Discord Nitro 诈骗机器人。至此,检测的相关代码为:
keywords = ["GIFT", "GIFTS", "NITRO", "CATCH", "FOR", "FREE", "@EVERYONE", "@HERE"]
keywords_met = 0
@bot.event
async def on_message(message):
if message.author == bot.user:
return
content = message.content.split()
print(content)
for word in content:
print("Loop iteration")
if word.upper() in keywords:
global keywords_met
keywords_met += 1
if keywords_met >= 3:
channel = bot.get_channel(ID of a mod channel)
await channel.send("<@ID of a role> nitro scam detected")
keywords_met = 0
根据经验,keywords
是那些消息中经常使用的词。但是,我想检查消息是否也包含 link。问题是机器人发送的 URL 经常变化,所以我无法检查一个特定的 link。
一般有没有办法检查 links?
使用方法 in
检查 string.Example 中的子字符串:
if 'https://' in word:
# logic here
我会尝试使用这个逻辑兄弟!
test_string = "https://www.google.com"
test_list = ['.com', '.ru', '.net', '.org', '.info', '.biz', '.io', '.co', "https://", "http://"]
link_matches = [ele for ele in test_list if(ele in test_string)]
if link_matches: # Courtesy of my bro Jyr!
print("I got a match bro")