so I'm trying to allow using ! as a prefix for something. Here I have some regular expressions, but I almost have no idea how to do so:
if inp.chan == inp.nick: # private message, no command prefix
prefix = r'^(?:[!!]?|'
else:
prefix = r'^(?:[!]|'
command_re = prefix + inp.conn.nick
command_re += r'[:,]+\s+)(\w+)(?:$|\s+)(.*)'
I can change command's prefix by changing [!], but I want to make it so I can make the prefix double !!'ed, such as !!test will work. Thanks.
Edit:
import re
import random
from util import hook, http
re_lineends = re.compile(r'[\r\n]*')
command_prefix = re.compile(r'^\!+')
@hook.command(command_prefix)
def exl(inp,nick=""):
""
res = http.get("http://eval.appspot.com/eval", statement=inp).splitlines()
if len(res) == 0:
return
res[0] = re_lineends.split(res[0])[0]
if not res[0] == 'Traceback (most recent call last):':
return res[0]
else:
return res[-1]
@hook.command:
def _hook_add(func, add, name=''):
if not hasattr(func, '_hook'):
func._hook = []
func._hook.append(add)
if not hasattr(func, '_filename'):
func._filename = func.func_code.co_filename
if not hasattr(func, '_args'):
argspec = inspect.getargspec(func)
if name:
n_args = len(argspec.args)
if argspec.defaults:
n_args -= len(argspec.defaults)
if argspec.keywords:
n_args -= 1
if argspec.varargs:
n_args -= 1
if n_args != 1:
err = '%ss must take 1 non-keyword argument (%s)' % (name,
func.__name__)
raise ValueError(err)
args = []
if argspec.defaults:
end = bool(argspec.keywords) + bool(argspec.varargs)
args.extend(argspec.args[-len(argspec.defaults):
end if end else None])
if argspec.keywords:
args.append(0) # means kwargs present
func._args = args
if not hasattr(func, '_thread'): # does function run in its own thread?
func._thread = False