所以我试图允许使用!作为某事的前缀。这里我有一些正则表达式,但我几乎不知道该怎么做:
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+)(.*)'
我可以通过更改 [!] 来更改命令的前缀,但我想这样做,所以我可以使前缀加倍 !!'ed,例如 !!test 将起作用。谢谢。
编辑:
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