Search code examples
pythondiscorddiscord.py

How to use on_message using discord.py in this way?


I'm making a bot using discord.py. I want the bot to get triggered if a message from any user(on the server where the bot is present) is a mixture of Integers, Characters and Strings like this e.g 122-PG-CSAI-2022

How can I do so?


Solution

  • I'm not sure what you think the difference is between a char and string in this case, but this code will check if it contains at least one alphabetical letter and one 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
    

    edit:

    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