import re
from enum import Enum
import dave.config as config
def ratelimit(value, per):
def add_attribute(function):
function.ratelimit = {
"value": value,
"per": per
}
return function
return add_attribute
def match(value):
def add_attribute(function):
if not hasattr(function, "rule"):
function.rule = []
function.rule.append({
"named": False,
"pattern": re.compile(value)
})
return function
return add_attribute
def command(commands, parameters="?$"):
def add_attribute(function):
if not hasattr(function, "rule"):
function.rule = []
function.rule.append({
"named": True,
"pattern": re.compile("^(?:{}) {}".format("|".join(commands), parameters)),
"commands": commands
})
return function
return add_attribute
def help(message, name=None):
def add_attribute(function):
if name is None:
for rule in function.rule:
if "commands" in rule:
n = rule["commands"][0]
else:
n = name
if not hasattr(function, "priority"):
function.priority = Priority.NORMAL
function.help = {
"message": message,
"name": n
}
return function
return add_attribute
def priority(priority):
def add_attribute(function):
function.priority = priority
return function
return add_attribute
def dont_always_run_if_run():
def add_attribute(function):
function.dont_always_run = True
return function
return add_attribute
def always_run():
def add_attribute(function):
function.always_run = True
return function
return add_attribute
class Priority(Enum):
HIGHEST = -2
HIGH = -1
NORMAL = 0
LOW = 1
LOWEST = 2