async def update_keyword(message_serialization: str) -> MessageItem: _, function, keyword = message_serialization.split("#") if re.match(r"\[mirai:image:{.*}\..*]", keyword): keyword = re.findall(r"\[mirai:image:{(.*?)}\..*]", keyword, re.S)[0] if function not in ImageSenderHandler.functions: return MessageItem(MessageChain.create([Plain(text="非法方法名!")]), QuoteSource(GroupStrategy())) try: orm.update(TriggerKeyword, { "keyword": keyword, "function": function }, { "keyword": keyword, "function": function }) return MessageItem( MessageChain.create( [Plain(text=f"关键词添加成功!\n{keyword} -> {function}")]), QuoteSource(GroupStrategy())) except Exception: logger.error(traceback.format_exc()) orm.session.rollback() return MessageItem( MessageChain.create([Plain(text="发生错误!请查看日志!")]), QuoteSource(GroupStrategy()))
async def update_user_call_count_plus1(group: Group, member: Member, table_column, column_name: str) -> bool: new_value = list(orm.fetchone( select(table_column).where(UserCalledCount.group_id == group.id, UserCalledCount.member_id == member.id)) ) new_value = new_value[0][0] + 1 if new_value else 1 try: orm.update( UserCalledCount, {"group_id": group.id, "member_id": member.id}, {"group_id": group.id, "member_id": member.id, column_name: new_value} ) except Exception: logger.error(traceback.format_exc()) orm.session.rollback() return False try: if not column_name == "chat_count": new_id = list(orm.fetchone(select(FunctionCalledRecord.id).order_by(desc(FunctionCalledRecord.id)), 1)) new_id = new_id[0][0] + 1 if new_id else 1 orm.add( FunctionCalledRecord, { "id": new_id, "time": datetime.datetime.now(), "group_id": group.id, "member_id": member.id, "function": column_name } ) except Exception: logger.error(traceback.format_exc()) orm.session.rollback() return True
async def execute_setting_update(group: Group, member: Member, command: str) -> MessageItem: """ setting -set setu=True real=True 多命令执行 """ commands = command[13:].split(" ") error_commands = [] success_commands = [] for command in commands: try: command = command.strip() # .replace("-", "") if not command: continue func, value = command.split("=") func = camel_to_underscore(func) value = (True if value == "True" else False) if value in [ "True", "False" ] else value if func in command_index.keys(): if command_index[func].is_valid(value): """ update """ if await user_permission_require( group, member, command_index[func].level): try: orm.update(Setting, {"group_id": group.id}, {func: value}) success_commands.append(f"{func} -> {value}") except Exception as e: error_commands.append((command, str(e))) logger.error(traceback.format_exc()) else: error_commands.append( (command, f"权限不足,要求权限等级{command_index[func].level}")) else: error_commands.append(( f"{func} -> {value}", f"期望值:{','.join([str(valid_value) for valid_value in command_index[func].valid_values])}" )) else: error_commands.append((command, "未找到此命令")) except ValueError: error_commands.append((command, "格式非法!应为 func=value")) response_text = f"共解析{len(commands)}条命令,\n其中{len(success_commands)}条执行成功,{len(error_commands)}条失败" if success_commands: response_text += "\n\n成功命令:" for i in success_commands: response_text += f"\n{i}" if error_commands: response_text += "\n\n失败命令:" for i in error_commands: response_text += f"\n{i[0]} | {i[1]}" return MessageItem(MessageChain.create([Plain(text=response_text)]), Normal(GroupStrategy()))
async def modifyGroupSetting(groupId: int, settingName: str, newValue): if newValue in ["true", "false"]: newValue = True if newValue == "true" else False try: orm.update(Setting, {"group_id": groupId}, { "group_id": groupId, settingName: newValue }) except Exception as e: logger.error(f"api error: {e}") orm.rollback() return False return True
async def grant_permission(group_id: int, member_id: int, new_level: int) -> bool: try: orm.update(UserPermission, { "group_id": group_id, "member_id": member_id }, { "group_id": group_id, "member_id": member_id, "level": new_level }) return True except Exception: logger.error(traceback.format_exc()) orm.session.rollback() return False
async def bot_launch_init(self): self.config_check() orm.session.query(Setting).update({"active": False}) group_list = await self.__app.groupList() frequency_limit_dict = {} for group in group_list: frequency_limit_dict[group.id] = 0 try: orm.update(Setting, {"group_id": group.id}, { "group_id": group.id, "group_name": group.name, "active": True }) except Exception: logger.error(traceback.format_exc()) orm.session.rollback() results = orm.fetchall(select(Setting).where(Setting.active == True)) logger.info("本次启动活动群组如下:") for result in results: logger.info(f"群ID: {str(result[0]).ljust(14)}群名: {result[1]}") for result in results: orm.update(UserPermission, { "member_id": self.__config["HostQQ"], "group_id": result[0] }, { "member_id": self.__config["HostQQ"], "group_id": result[0], "level": 4 }) self.__frequency_limit_instance = GlobalFrequencyLimitDict( frequency_limit_dict) threading.Thread(target=frequency_limit, args=(self.__frequency_limit_instance, )).start() exception_resender_instance = ExceptionReSender(self.__app) listener = threading.Thread(target=exception_resender_listener, args=(self.__app, exception_resender_instance, self.__loop)) listener.start()