def on_group_message(self, from_group: int, from_qq: int, readable_name: str, content: str, origin_msg: dict) -> None: logger.debug( "on_group_message, group: {}, qq: {}, name: {}, content: {}".format( from_group, from_qq, readable_name, content) ) # start pipeline # should_continue = True for _pipeline in self.pipelines: result = _pipeline.process(from_group, from_qq, readable_name, content) should_continue = result if not should_continue: break # pipeline stop # if not should_continue: return if content.startswith("%"): command_list = re.split(r"\s+", content) input_command_name = command_list[0] command_instance: Optional[BaseCommand] = self.commands.get(input_command_name, None) if command_instance is None: self.commands.get("%help").process(from_group, from_qq, readable_name, command_list) else: try: command_instance.process(from_group, from_qq, readable_name, command_list) except Exception as e: tbe = traceback.TracebackException(*sys.exc_info()) full_err = ''.join(tbe.format()) CQApi().send_group_message(from_group, from_qq, "Unknown Error! e: {}\n{}".format(e, full_err))
def _worker(self): logger.debug("{} start.".format(self.name)) while self.is_running(): # 判断当前时间是否为凌晨7点 # windows上没有好用的crontab,所以只能每隔1分钟进行一次判断 now = datetime.datetime.now() if now.hour == 7 and now.minute == 5: logger.info("Ryuo timer work!") # 从db中获取前24个小时的发言内容 results: list = RyuoModel.instance.get_today_cnt() ryuo = results[0] ryuo_qq = ryuo[0] ryuo_nickname = ryuo[1].get("nickname") ryuo_cnt = ryuo[1].get("cnt") logger.debug("ryuo: {}".format(ryuo)) message = "昨日群内发言统计:\n" for qq, data in results: message += "{}({}) -> {}\n".format(data["nickname"], qq, data["cnt"]) message += "\n恭喜{}获得喷水龙的称号,获取管理员权限一天!\n来喷个水给大家康康。".format( QuickAt.build_at_msg(ryuo[0])) # 获取前一天的龙王 last_ryuo = LastRyuoModel.instance.get_latest_ryuo() # 把龙王信息存起来 LastRyuoModel.instance.create(qq=ryuo_qq, cnt=ryuo_cnt, nickname=ryuo_nickname) # 设置专属称号,并取消前一个人的称号 self.CQApi.set_group_special_title(self.target_group, ryuo_qq, "今日份的喷水龙王") if last_ryuo: self.CQApi.set_group_special_title(self.target_group, last_ryuo.qq, "") # 设置为管理员,并取消前一天的管理员 AdminUserModel.instance.add_admin(ryuo_qq) if last_ryuo: AdminUserModel.instance.remove_admin(last_ryuo.qq) # 发到群里 self.CQApi.send_group_message(self.target_group, None, message) self.ev.wait(60) else: self.ev.wait(60)
def _worker(self): logger.debug("{} start!".format(self.name)) while self.is_running(): self.ev.wait(30) # 1. 取出所有hp和sp不满的玩家 players: List[ PlayerModel] = PlayerModel.instance.get_no_full_hp_or_sp_player( ) logger.info("有{}个记录需要更新".format(len(players))) for _player in players: qq = _player.qq regain_obj = PlayerRegainModel.instance.filter(qq=qq).first() if regain_obj is None: regain_obj = PlayerRegainModel.instance.create(qq=qq) current_time = datetime.datetime.now() logger.debug("next_hp_ts: {}, next_sp_ts: {}".format( regain_obj.next_hp_time, regain_obj.next_sp_time)) max_hp = _player.max_hp current_hp = _player.current_hp if current_time > regain_obj.next_hp_time and current_hp < max_hp: # 玩家当前的血量是0,死亡玩家,走复活流程,给盾 if _player.current_hp == 0: with transaction.atomic(): _player.current_hp = _player.max_hp _player.save() regain_obj.shield_time = datetime.datetime.now( ) + datetime.timedelta(hours=6) regain_obj.save() self.CQApi.send_group_message( self.target_group, _player.qq, "已复活,并获得6小时护盾,护盾期间主动攻击将失去护盾。") else: with transaction.atomic(): PlayerModel.instance.update_x("hp", qq) PlayerRegainModel.instance.update_next_time( "hp", qq) max_sp = _player.max_sp current_sp = _player.current_sp if current_time > regain_obj.next_sp_time and current_sp < max_sp: with transaction.atomic(): PlayerModel.instance.update_x("sp", qq) PlayerRegainModel.instance.update_next_time("sp", qq)
def process(self, from_group: int, from_qq: int, name: str, message: str) -> bool: global last_message, repeat_count if message == last_message: # 是复读的消息,累加计数 repeat_count += 1 else: # 不是复读的消息,更新计数 last_message = message repeat_count = 1 logger.debug("last_message: {}, count: {}".format( last_message, repeat_count)) # 根据复读次数,进行概率ban # 1次、2次 不禁言 # 10次以上 100%禁言 if repeat_count in (1, 2): return True if repeat_count >= 10: self.CQApi.set_group_ban(from_group, from_qq, 10) self.CQApi.send_group_message(from_group, from_qq, "您怕不是个复读机吧?\n劝你次根香蕉冷静冷静!") return False # r = 1- (n-1)/n # n = 3 => r = 1/3 # n = 4 => r = 1/4 # n = 5 => r = 1/5 # n = 6 => r = 1/6 # ... # n = 9 => r = 1/9 # 1 / (12 - n) prob = 2.0 / (11 - repeat_count) point = random.random() logger.info(f"prob: {prob}, point: {point}") if point < prob: self.CQApi.set_group_ban(from_group, from_qq, repeat_count * repeat_count / 2) self.CQApi.send_group_message(from_group, from_qq, "嘤嘤嘤,复读被抓住了呢!") return False return True
def set_group_ban(self, group: int, qq: int, duration: int, use_async=True): if use_async: method_name = "set_group_ban_async" else: method_name = "set_group_ban" params = { "group_id": group, "user_id": qq, "duration": int(duration * 60) } logger.debug("set_group_ban params: {}".format(params)) self._call(method_name, params)
def _worker(self): logger.debug("{} start!".format(self.name)) while self.is_running(): # 获取所有的未完成活动 active_events = AppointmentModel.instance.get_all_active_event() now = datetime.datetime.now() for _event in active_events: # 看看有没有已经完成的活动 if now > _event.event_time: _event.has_end = True _event.save() logger.debug("更新活动{}-{}为完成状态".format( _event.event_name, _event.id)) # 看看有没有 5 分钟到期的活动 # 如果距离活动开始5分钟,还有人没有确认,自动设置为咕咕咕 if _event.event_time - datetime.timedelta(minutes=5) < now: invited_users = AppointmentUserModel.instance.get_invited_user_by_id( _event.id) for iu in invited_users: if iu.accepted == -1 and iu.gu == -1: iu.gu = 1 iu.title_end_time = datetime.datetime.now( ) + datetime.timedelta(hours=12) iu.save() self.CQApi.set_group_special_title( self.target_group, iu.user_id, "鸽子精咕咕咕") logger.info("标记{}为鸽子精".format(iu.user_id)) # 取消到期的title un_process_users = AppointmentUserModel.instance.get_gu_user() for _user in un_process_users: if _user.title_end_time < now: # 取消title self.CQApi.set_group_special_title(self.target_group, _user.user_id, "") _user.has_title = 1 _user.save() logger.debug("取消{}的鸽子精称号!".format(_user)) self.ev.wait(60)
def _call(self, method: str, params: dict): url = self.api_list.get(method) headers = {"Authorization": "Bearer {}".format(self.token)} response = requests.post(url, json=params, headers=headers) logger.debug("call response: {}".format(response.content))
def post(self, request): logger.debug("Receive post data: {}".format(request.body)) self.__message_handler.execute(request.body) return HttpResponse("")