Search code examples
pythondiscorddiscord.py

Check if message contains a link; Discord.py


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?


Solution

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

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