pythondiscordpycordpurge

Regex in discord.py purge check?


Is it possible to implement a regex check in the check argument of discord.TextChannel.purge?

I believe that I have to use lambda or something, but I'm really not sure. Any example would be appreciated!

I'm using the latest version of both pycord and re.


Solution

  • Lambda can be used. You have to define a method to check if the message matches the Regex.

    def matches(message, regex):
        if re.match(regex, message): return True
        return False
    
    
    #Either get your regex from the command/message content or just set it to a variable.
    arg = "\d+" #arbitrary
    await ctx.channel.purge(limit=10, check=lambda message, regex=arg: matches(message, regex))
    

    Edit: Fixed code. Was wrong. Now right.