Home > front end >  Check if message contains a link; Discord.py
Check if message contains a link; Discord.py

Time:01-23

I want to make a Discord bot that checks messages for potential Discord Nitro scam bots. So far, the relevant code for detection is:

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

Speaking from experience, keywords are words that are often used in those messages. However, I would like to check if the message contains a link, too. The problem is that the URL that the bots send changes frequently, so I can't check for one specific link. Is there a way to check for links in general?

CodePudding user response:

Use method in to check a substring inside a string.Example:

if 'https://' in word:
    # logic here

CodePudding user response:

I'll try to use this logic bro!

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")
  •  Tags:  
  • Related