0

我想问一下如何让我的 python irc bot 运行仅由指定 nick 给出的命令?

我的机器人有 Skybot - Python IRC 机器人核心,它基于它。

那是在每个命令中导入的模块

import inspect
import re


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


def sieve(func):
    if func.func_code.co_argcount != 5:
        raise ValueError(
                'sieves must take 5 arguments: (bot, input, func, type, args)')
    _hook_add(func, ['sieve', (func,)])
    return func

def command(arg=None, **kwargs):
    args = {}

def command_wrapper(func):
    args.setdefault('name', func.func_name)
    _hook_add(func, ['command', (func, args)], 'command')
    return func

if kwargs or not inspect.isfunction(arg):
    if arg is not None:
        args['name'] = arg
    args.update(kwargs)
    return command_wrapper
else:
    return command_wrapper(arg)


def event(arg=None, **kwargs):
    args = kwargs

def event_wrapper(func):
    args['name'] = func.func_name
    args.setdefault('events', ['*'])
    _hook_add(func, ['event', (func, args)], 'event')
    return func

if inspect.isfunction(arg):
    return event_wrapper(arg, kwargs)
else:
    if arg is not None:
        args['events'] = arg.split()
    return event_wrapper


def singlethread(func):
    func._thread = True
    return func


def regex(regex, flags=0, **kwargs):
    args = kwargs

def regex_wrapper(func):
    args['name'] = func.func_name
    args['regex'] = regex
    args['re'] = re.compile(regex, flags)
    _hook_add(func, ['regex', (func, args)], 'regex')
    return func

if inspect.isfunction(regex):
    raise ValueError("regex decorators require a regex to match against")
else:
    return regex_wrapper

这就是一个简单命令的工作原理:

from util import hook, http

@hook.command
def calc(inp):
    '''.calc <term> google calc result'''

    h = http.get_html('http://www.google.com/search', q=inp)

    m = h.xpath('//h2[@class="r"]/text()')

    if not m:
        return "Could not calculate " + inp

    res = ' '.join(m[0].split())

    return res

另外,我想让它有 2 个管理员,就像只有 2 个人能够指挥它一样。我自己会这样做,但是看着代码我不知道该怎么做:?

谢谢!

4

1 回答 1

0

向您的函数添加nick关键字参数,然后根据允许执行管理员命令的昵称列表进行检查:

administrators = ('nick1', 'nick2')

@hook.command
def someadmincommand(inp, nick=''):
    if nick not in administratiors:
        return 'I don't think so, {0}'.format(nick)

    # `nick` is an administrator, do your worst
于 2012-12-25T16:33:56.333 回答