def command_translate_handle(message): if len(get_raw_message(message).split(' ')) >= 2: content = "" for i in range(len(get_raw_message(message).split(' '))): if i == 0: continue content += " " + get_raw_message(message).split(' ')[i] return translate_api(content) else: return '命令格式有误,发送“#帮助”获得更多信息'
def command_study_handle(message): if len(get_raw_message(message).split(' ')) == 3: new_quest = get_raw_message(message).split(' ')[1] new_reply = get_raw_message(message).split(' ')[2] if study_info(new_quest, new_reply): logging_put("用户" + str(get_user_id(message)) + "教我新内容:" + new_quest + "--" + new_reply) return '学习成功,收获新知识[CQ:face,id=63]' else: return '学习失败' else: return '命令格式有误,发送“#帮助”获得更多信息'
def group_msg_handle(message): msg_dict = { "msg_type": "group", "number": get_number(message), "msg": "我听不懂" } #检测 general_detect = group_msg_general_detection(message) db_detect = group_msg_db_detection(message) if general_detect != '': msg_dict["msg"] = general_detect elif db_detect != '': msg_dict["msg"] = db_detect else: logging_put("群聊中用户请求没有对应的信息:" + str(get_user_id(message)) + "-" + get_raw_message(message)) msg_dict["msg"] = group_msg_error(message) #没有at,则有四分之一的概率回复消息;at了则一定回复 if at_robot(message): send_msg(msg_dict) else: if 1 == random.randint(1, 12): send_msg(msg_dict) return
def group_msg_db_detection(message): reply = '' prefix = "[CQ:at,qq=" + str(get_user_id(message)) + "]" if at_robot(message): #方便还原message ori = get_raw_message(message) #更新原数据的原始信息 set_raw_message(message, get_raw_message(message).split(' ', 1)[1]) reply = get_reply(get_raw_message(message)) if reply != '': reply = prefix + reply #还原消息 set_raw_message(message, ori) else: reply = get_reply(get_raw_message(message)) return reply
def message_handle(message): logging_put("收到消息" + get_raw_message(message) + "来自" + str(get_number(message))) if get_message_type(message) == 'private': private_msg_handle(message) elif get_message_type(message) == 'group': group_msg_handle(message) return 0
def command_broadcast_handle(msg): if str(get_number(msg)) in admin_number: if len(get_raw_message(msg).split(' ', 1)) == 2: main_msg = get_raw_message(msg).split(' ', 1)[1] #得到好友和群组列表 friends = get_friends_array() groups = get_groups_array() #得到黑名单 blacklist = get_blacklist() for num in friends: if not num in blacklist: #发送广播内容 msg_dict = { "msg_type": "private", "number": num, "msg": "" } #防止广播信息闭合 main_msg.replace('"', '"') main_msg.replace("'", ''') msg_dict['msg'] = "[CQ:face,id=72]广播通知:" + main_msg send_msg(msg_dict) for num in groups: if not num in blacklist: #发送广播内容 msg_dict = {"msg_type": "group", "number": num, "msg": ""} #防止广播信息闭合 main_msg.replace('"', '"') main_msg.replace("'", ''') msg_dict['msg'] = "[CQ:face,id=72]广播通知:" + main_msg send_msg(msg_dict) return '广播成功[CQ:face,id=63]' else: return '请指明想要广播的内容' else: return '你没有权利让我广播'
def private_msg_handle(message): msg_dict = { "msg_type": "private", "number": get_number(message), "msg": "我听不懂" } #检测 general_detect = private_msg_general_detection(message) db_detect = private_msg_db_detection(message) if general_detect != '': msg_dict["msg"] = general_detect elif db_detect != '': msg_dict["msg"] = db_detect else: logging_put("私聊中用户请求没有对应的信息:" + str(get_user_id(message)) + "-" + get_raw_message(message)) msg_dict["msg"] = private_msg_error() send_msg(msg_dict) return
def private_msg_db_detection(message): reply = '' reply = get_reply(get_raw_message(message)) return reply
def group_msg_general_detection(message): reply = '' #处理有at的指令 if at_robot(message): #回复消息的前缀(at信息) prefix = "[CQ:at,qq=" + str(get_user_id(message)) + "]" #方便还原message ori = get_raw_message(message) #更新原数据的原始信息 set_raw_message(message, get_raw_message(message).split(' ', 1)[1]) #得到特殊指令 command = get_raw_message(message).split(' ')[0] if command == "#帮助": reply = prefix + command_help_handle(get_message_type(message)) elif command == "翻译": reply = prefix + command_translate_handle(message) elif command == "号码信息": reply = prefix + command_number_information_handle(message) elif command == "壁纸" or command == "高清壁纸": reply = prefix + command_wallpaper_handle() elif command == "头像": reply = prefix + command_head_picture_handle(message) elif command == "来点色图": reply = prefix + command_sex_picture_handle() elif command == "百度收录域名查询": reply = prefix + command_baidu_record_handle(message) elif command == "夜深了": reply = prefix + command_feeling_a_word_handle() elif command == "新闻": reply = prefix + command_news_handle() elif command == "#订阅": if len(get_raw_message(message).split(' ')) == 2: content = get_raw_message(message).split(' ')[1] if content == "每日英语": reply = command_sub_handle("famousremark", message) elif content == "每日必应壁纸": reply = command_sub_handle("subwallpaper", message) elif content == "网易云每日推荐": reply = command_sub_handle("submusic", message) elif content == "每日微博热搜": reply = command_sub_handle("subweibohot", message) elif content == "每日色图": reply = command_sub_handle("subsexpic", message) elif content == "每日生活小窍门": reply = command_sub_handle("sublifeskill", message) elif content == "考研倒计时": reply = command_sub_handle("subkaoyan", message) else: reply = "频道不存在" else: reply = "请指定频道" elif command == "#取消订阅": if len(get_raw_message(message).split(' ')) == 2: if content == "每日英语": reply = command_no_sub_handle("famousremark", message) elif content == "每日必应壁纸": reply = command_no_sub_handle("subwallpaper", message) elif content == "网易云每日推荐": reply = command_no_sub_handle("submusic", message) elif content == "每日微博热搜": reply = command_no_sub_handle("subweibohot", message) elif content == "每日色图": reply = command_no_sub_handle("subsexpic", message) elif content == "每日生活小窍门": reply = command_no_sub_handle("sublifeskill", message) elif content == "考研倒计时": reply = command_no_sub_handle("subkaoyan", message) else: reply = "频道不存在" else: reply = "请指定频道" #还原消息 set_raw_message(message, ori) else: #没有@机器人,则有五分之一的检测关键字 command = get_raw_message(message).split(' ')[0] #回复消息的前缀(at信息) prefix = "[CQ:at,qq=" + str(get_user_id(message)) + "]" if command == "#帮助": reply = command_help_handle(get_message_type(message)) elif command == "夜深了": reply = command_feeling_a_word_handle() elif command == "色图" or command == "来点色图": reply = prefix + "满足你," + command_sex_picture_handle() return reply
def at_robot(message): if get_raw_message(message).split(' ')[0] == "[CQ:at,qq=1750133059]": return True return False
def private_msg_general_detection(message): reply = '' #得到特殊指令 command = get_raw_message(message).split(' ')[0] if command == "#学习": reply = command_study_handle(message) elif command == "#广播": reply = command_broadcast_handle(message) elif command == "#帮助": reply = command_help_handle(get_message_type(message)) elif command == "翻译": reply = command_translate_handle(message) elif command == "号码信息": reply = command_number_information_handle(message) elif command == "壁纸" or command == "高清壁纸": reply = command_wallpaper_handle() elif command == "头像": reply = command_head_picture_handle(message) elif command == "来点色图": reply = command_sex_picture_handle() elif command == "百度收录域名查询": reply = command_baidu_record_handle(message) elif command == "夜深了": reply = command_feeling_a_word_handle() elif command == "新闻": reply = command_news_handle() elif command == "#订阅": if len(get_raw_message(message).split(' ')) == 2: content = get_raw_message(message).split(' ')[1] if content == "每日英语": reply = command_sub_handle("famousremark", message) elif content == "每日必应壁纸": reply = command_sub_handle("subwallpaper", message) elif content == "网易云每日推荐": reply = command_sub_handle("submusic", message) elif content == "每日微博热搜": reply = command_sub_handle("subweibohot", message) elif content == "每日色图": reply = command_sub_handle("subsexpic", message) elif content == "每日生活小窍门": reply = command_sub_handle("sublifeskill", message) elif content == "考研倒计时": reply = command_sub_handle("subkaoyan", message) else: reply = "频道不存在" else: reply = "请指定频道" elif command == "#取消订阅": if len(get_raw_message(message).split(' ')) == 2: content = get_raw_message(message).split(' ')[1] if content == "每日英语": reply = command_no_sub_handle("famousremark", message) elif content == "每日必应壁纸": reply = command_no_sub_handle("subwallpaper", message) elif content == "网易云每日推荐": reply = command_no_sub_handle("submusic", message) elif content == "每日微博热搜": reply = command_no_sub_handle("subweibohot", message) elif content == "每日色图": reply = command_no_sub_handle("subsexpic", message) elif content == "每日生活小窍门": reply = command_no_sub_handle("sublifeskill", message) elif content == "考研倒计时": reply = command_no_sub_handle("subkaoyan", message) else: reply = "频道不存在" else: reply = "请指定频道" return reply
def check_pass(message): if pick.get_post_type(message) == "message": if pick.get_message_type(message) == "private": command = pick.get_raw_message(message).split(" ")[0] number = str(pick.get_number(message)) msg_dict = { "msg_type": "private", "number": pick.get_number(message), "msg": "" } #消息来源在黑名单中,则只识别是不是“#开机” if exist_number(number): #当前发消息者面前,我们是关机的 if command == "#开机": logging_put(number + "开启了我,执行者:" + str(pick.get_user_id(message))) #从黑名单中移除该number del_number(number) msg_dict['msg'] = "我们又见面了" send_msg(msg_dict) #这次消息都不予通过 return False else: #当前发消息者面前,我们是开机的 if command == "#关机": logging_put(number + "关闭了我,执行者:" + str(pick.get_user_id(message))) #向黑名单中加人该number add_number(number) msg_dict['msg'] = "使用“#开机”就可以再次开启我" send_msg(msg_dict) return False elif command == "#开机": msg_dict['msg'] = "我现在就是开机状态[CQ:face,id=22]" send_msg(msg_dict) return False else: return True elif pick.get_message_type(message) == "group": if pick.get_raw_message(message).split( ' ')[0] == "[CQ:at,qq=1750133059]": #方便还原message ori = pick.get_raw_message(message) #更新原数据的原始信息 pick.set_raw_message( message, pick.get_raw_message(message).split(' ', 1)[1]) command = pick.get_raw_message(message).split(" ")[0] number = str(pick.get_number(message)) msg_dict = { "msg_type": "group", "number": pick.get_number(message), "msg": "" } #消息来源在黑名单中,则只识别是不是“#开机” if exist_number(number): #当前发消息者面前,我们是关机的 if command == "#开机": logging_put(number + "开启了我,执行者:" + str(pick.get_user_id(message))) #从黑名单中移除该number del_number(number) msg_dict['msg'] = "我们又见面了" send_msg(msg_dict) #这次消息都不予通过 return False else: #当前发消息者面前,我们是开机的 if command == "#关机": logging_put(number + "关闭了我,执行者:" + str(pick.get_user_id(message))) #向黑名单中加人该number add_number(number) msg_dict['msg'] = "使用“#开机”就可以再次开启我" send_msg(msg_dict) return False elif command == "#开机": msg_dict['msg'] = "我现在就是开机状态[CQ:face,id=22]" send_msg(msg_dict) return False else: #还原消息 pick.set_raw_message(message, ori) return True else: if exist_number(number): return False else: return True elif pick.get_post_type(message) == "notice" or pick.get_post_type( message) == "request": try: user_id = message['user_id'] except BaseException as e: #如果user_id都没有,则直接拦截 return False #再尝试得到群号 try: group_id = message['group_id'] except BaseException as e: #有user_id,没有群id if exist_number(message['user_id']): return False else: return True #有群id if exist_number(message['group_id']): return False else: return True else: #三大消息以外应该是心跳消息,通行 return True
def group_msg_handle(msg): if (main.flag == False): #从数据库中提取ban_list列表 db = pymysql.connect("", "", "", "") cursor = db.cursor() sql = '''select * from ban''' cursor.execute(sql) tuple_list = cursor.fetchall() global ban_list ban_list = list(chain.from_iterable(tuple_list)) cursor.close() main.flag = True message = get_raw_message(msg) id = str(get_user_id(msg)) id_in_group = get_card(msg) title = ['#ark', '#bh3', '#ghs', '#fgo'] print(ban_list) if (main.status == True): if (id not in ban_list): #爬 if ('#pa' in message): text = pa(message, id_in_group) msg_dict = { "msg_type": "group", "number": get_number(msg), "msg": text } send_msg(msg_dict) return #舔狗日记 elif (message == '#dog'): text = dog() msg_dict = { "msg_type": "group", "number": get_number(msg), "msg": text } send_msg(msg_dict) return #abs elif ('#abs' in message): text = abs(message) msg_dict = { "msg_type": "group", "number": get_number(msg), "msg": text } send_msg(msg_dict) return #ban 管理员权限 id填qq号 elif ('#ban' in message and id == ""): text = ban(message) msg_dict = { "msg_type": "group", "number": get_number(msg), "msg": text } send_msg(msg_dict) return #unban 同上 elif ('#unban' in message and id == ""): text = unban(message) msg_dict = { "msg_type": "group", "number": get_number(msg), "msg": text } send_msg(msg_dict) return #菜单 elif (message == '#help'): text = help() msg_dict = { "msg_type": "group", "number": get_number(msg), "msg": text } send_msg(msg_dict) return elif (message == '#hello'): text = hello() msg_dict = { "msg_type": "group", "number": get_number(msg), "msg": text } send_msg(msg_dict) return #追番功能 elif ('#anime' in message): text = anime(message) msg_dict = { "msg_type": "group", "number": get_number(msg), "msg": text } send_msg(msg_dict) return #picture elif (message in title): text = picture(message) msg_dict = { "msg_type": "group", "number": get_number(msg), "msg": text } send_msg(msg_dict) return #beauty elif (message == '#beauty'): text = beauty() msg_dict = { "msg_type": "group", "number": get_number(msg), "msg": text } send_msg(msg_dict) return # trans elif ("#trans" in message): text = trans(message) msg_dict = { "msg_type": "group", "number": get_number(msg), "msg": text } send_msg(msg_dict) return elif ('find' in message): text = find(get_message(msg), get_number(msg)) msg_dict = { "msg_type": "group", "number": get_number(msg), "msg": text } send_msg(msg_dict) return #随机数 elif ('#rand' in message): text = rand(message, id) msg_dict = { "msg_type": "group", "number": get_number(msg), "msg": text } send_msg(msg_dict) return #简单的互动功能 elif ('下班' in message and id == ""): text = "那我就先走啦" msg_dict = { "msg_type": "group", "number": get_number(msg), "msg": text } main.status = False send_msg(msg_dict) return else: return else: return elif (main.status == False and id == "" and '上班' in message): main.status = True text = "嗯?这么想我吗" msg_dict = { "msg_type": "group", "number": get_number(msg), "msg": text } send_msg(msg_dict) return else: return
def command_baidu_record_handle(message): if len(get_raw_message(message).split(' ')) == 2: domain = get_raw_message(message).split(' ')[1] return baidu_record_api(domain) else: return '命令格式有误,格式:百度收录域名查询 [域名地址]'
def command_head_picture_handle(message): if len(get_raw_message(message).split(' ')) == 2: type = get_raw_message(message).split(' ')[1] return head_picture_api(type) else: return '命令格式有误,发送“#帮助”获得更多信息'
def command_number_information_handle(message): if len(get_raw_message(message).split(' ')) == 2: content = get_raw_message(message).split(' ')[1] return number_information_api(content) else: return '命令格式有误,发送“#帮助”获得更多信息'