1
0
mirror of https://github.com/MarkParker5/STARK.git synced 2025-02-17 11:55:35 +02:00
STARK/Command/Callback.py

33 lines
1.0 KiB
Python

from .Command import Command
from .Response import Response
import re
class Callback:
def __init__(this, patterns, quiet = False, once = True):
this.patterns = patterns
this.quiet = quiet
this.once = once
def setStart(this, function):
this.start = function
def start(this, params):
pass
def answer(this, string):
for pattern in this.patterns:
if match := re.search(re.compile(Command.compilePattern(pattern)), string):
return this.start({**match.groupdict(), 'string':string})
return None
@staticmethod
def background(answer = '', voice = ''):
def decorator(cmd):
def wrapper(text):
finish_event = Event()
thread = RThread(target=cmd, args=(text, finish_event))
thread.start()
return Response(voice = voice, text = answer, thread = {'thread': thread, 'finish_event': finish_event} )
return wrapper
return decorator