def on_enable(event: Event): """ 机器人上线 demo :param event: :return: """ if 'master' in settings: cqp.send_private_msg(settings['master'], '我上线啦!') else: cqp.add_log(10, __name__, '在settings.json中添加 `"master": 你的qq号` ,即可在机器人上线时收到问候啦!')
def on_group_message(event: Event): """ 复读机 demo :param event: :return: """ global REPEAT_MODE if 'master' not in settings: cqp.add_log(10, __name__, '在settings.json中添加 `"master": 你的qq号` ,即可体验复读机的快乐。') if event.event_args['from_qq'] == settings['master'] and '复读开始' in event.event_args['msg']: REPEAT_MODE = True cqp.add_log(10, __name__, '复读开始!') cqp.send_group_msg(event.event_args['from_group'], '复读开始!') return elif event.event_args['from_qq'] == settings['master'] and '复读结束' in event.event_args['msg']: REPEAT_MODE = False cqp.add_log(10, __name__, '复读结束!') cqp.send_group_msg(event.event_args['from_group'], '复读结束!') return if REPEAT_MODE is True: cqp.send_group_msg(event.event_args['from_group'], event.event_args['msg'])
def to_object(self, cls: Type): if hasattr(cls, '_bin_pack'): kwargs = {} fmt = getattr(cls, '_bin_pack') for name, tp in fmt: if tp is long: kwargs[name] = self.pop_int64() elif tp is int: kwargs[name] = self.pop_int32() elif tp is str: kwargs[name] = self.pop_str() elif tp is bytes: kwargs[name] = self.pop_token() elif tp is bool: kwargs[name] = self.pop_bool() elif tp is Enum or Enum in tp.__mro__: kwargs[name] = tp(self.pop_int32()) cqp.add_log( 0, __name__, f'pop field {name} of type {tp}, value {kwargs[name]}') return cls(**kwargs) raise ValueError(f'不可反序列化的类型{cls},类定义中不包含 _bin_pack 字段。')
def dispatch(event: Event): cqp.add_log(0, __name__, f'python 模块收到事件 {event.event_type.name}') callbacks = callback_registry.get(event.event_type, []) try: for fn, priority in callbacks: cqp.add_log(0, __name__, f'调用事件处理函数 {fn}') fn(event) if not event.propagate: break except Exception: logger.exception('在处理事件时发生异常') cqp.add_log(20, __name__, f'处理事件时发生异常\n{traceback.format_exc()}') return -1 return 0
def emit(self, record: logging.LogRecord): cqp.add_log(record.levelno, cqp.APP_ID, record.message)
def pop_int64(self) -> int: cqp.add_log(0, __name__, f'pop buf {self.buffer[self.offset:self.offset + 8]}') ret, = struct.unpack_from('!q', self.buffer, self.offset) self.offset += 8 return ret
def __init__(self, buf: bytes) -> None: cqp.add_log(0, __name__, f'buf size {len(buf)}') self.buffer = buf self.offset = 0