def __on_close_root(self): """ 关闭窗口的事件 :return: 无 """ # 如果正在连接则释放连接 if GlobalValues.is_connected: Conn.release_session_key() # 杀掉root self.root.destroy()
def disconnect(): """ 断开连接 :return: 无 """ if not GlobalValues.is_connected: print(COMMANDS["disconnectedError"]) return # 释放Session Conn.release_session_key() # 修改连接状态 GlobalValues.is_connected = False print(COMMANDS["disconnectSuccess"])
def connect(): """ connect开始连接相关操作 :return: 无 """ if GlobalValues.is_connected: print(COMMANDS["connectedError"]) return # 无法正确连接到mirai的情况 try: Conn.new_session_key() # 连接地址错误 except (requests.exceptions.InvalidURL, requests.exceptions.ConnectionError): print(COMMANDS["connectValueError"]) return # 授权码错误 except WrongAuthkeyException: print(COMMANDS["authkeyError"]) return # Bot不存在错误 except BotNotExistException: print(COMMANDS["botNotExistError"]) return GlobalValues.is_connected = True # 修改上次连接键值对 ConfigOperation.modify_dict( "lastConnection", { "host": GlobalValues.conn_host, "port": GlobalValues.conn_port, "authkey": GlobalValues.conn_authkey, "qq": GlobalValues.conn_qq }) print(COMMANDS["connectSuccess"])
def run(self): while True: time.sleep(1) # 如果未连接,则不接收消息 if not GlobalValues.is_connected: continue # 如果未启用,则不接受消息 # 20210319 使用指令系统时此处会报错,因此直接通过 try: if not GlobalValues.enable_var.get(): continue except AttributeError: pass # 获取消息数据 # 同时处理可能出现的异常,直接忽略 try: data_list = Conn.fetch_message(1)["data"] except Exception as e: # 捕捉到异常后打印并重新循环 # print(e) continue # 获取到的列表为空 if not data_list: continue # 获取数据 self.data = data_list[0] data_type = self.data["type"] if data_type.endswith("Message"): # 判断为消息再读取 msg = Message(self.data) # 调用执行插件内容 PluginHandler.call_on_message(msg) elif data_type.find("Event") != -1: try: event = self.get_event() except TypeError as e: # 20210209更新 # 此处有时会抛出TypeError,怀疑是mirai http本身问题 # print(str(e)) continue # 调用执行插件内容 PluginHandler.call_on_event(event)
def __on_click_send_group_message(self): """ 点击发送消息给群按钮 :return: 无 """ # 获取到选中群的值列表 value_list = self.treeview_group_list.item(self.treeview_group_list.focus(), "values") try: # 获取qq并发送消息 qq = value_list[0] message_chain = MessageChain() text = self.text_group_send.get(1.0, END) if text == "\n": return message_chain.add_plain_text(text) Conn.send_group_message(qq, message_chain) self.text_group_send.delete(1.0, END) except: messagebox.showerror(message=SEND_ERROR_MSG) return
def send_message_back(self, message_chain: MessageChain) -> None: """ 原路发送消息(接收到的消息从哪里来就往哪里发) :param message_chain: 消息内容chain :return: 无 """ if self.is_group_message(): Conn.send_group_message(self.sender_member.group.id, message_chain) if self.is_temp_message(): Conn.send_temp_message(self.sender_member.qq, self.sender_member.group.id, message_chain) if self.is_friend_message(): Conn.send_friend_message(self.sender_friend.qq, message_chain)
def __on_click_refresh_group_list_event(self): """ 点击刷新群列表事件 :return: 无 """ try: # 如果未连接,则可能会抛出异常,此处直接弹出错误消息框 group_list = Conn.get_group_list() except: messagebox.showerror(message=REFRESH_ERROR_MSG) return # 删除列表内容 self.treeview_group_list.delete(*self.treeview_group_list.get_children()) # 解析group_list for group_block in group_list: self.treeview_group_list.insert("", index=END, values=( group_block["id"], group_block["name"], group_block["permission"] ))
def __on_click_refresh_friend_list_event(self): """ 点击刷新好友列表事件 :return: 无 """ try: # 如果未连接,则可能会抛出异常,此处直接弹出错误消息框 friend_list = Conn.get_friend_list() except: messagebox.showerror(message=REFRESH_ERROR_MSG) return # 删除列表内容 self.treeview_friend_list.delete(*self.treeview_friend_list.get_children()) # 解析friend_list for friend_block in friend_list: self.treeview_friend_list.insert("", index=END, values=( friend_block["id"], friend_block["nickname"], friend_block["remark"] ))
def __on_click_connect_event(self): """ 点击连接按钮事件 :return: 无 """ if not GlobalValues.is_connected: # 如果是要连接 # 存到全局使用变量 GlobalValues.conn_host = self.entry_host.get() GlobalValues.conn_port = self.entry_port.get() GlobalValues.conn_authkey = self.entry_authkey.get() try: # 转换为整型后保存 GlobalValues.conn_qq = int(self.entry_qq.get()) except ValueError: self.label_login_status_bar.config(text=LOGIN_STATUS_BAR_TEXT["wrongQQ"], fg=STATUS_BAR_COLOR["failed"]) return # 修改界面上的一些内容为不可修改 self.__set_login_tools_active(False) # 修改按钮内容 self.btn_connect.config(text=BTN_TEXT_CONN["disconnect"]) # 修改状态栏内容 self.label_login_status_bar.config(text=LOGIN_STATUS_BAR_TEXT["connecting"], fg=STATUS_BAR_COLOR["normal"]) # 调用连接 try: Conn.new_session_key() except ( requests.exceptions.InvalidURL, requests.exceptions.ConnectionError, ): # 连接错误 # 错误信息显示到状态栏 self.label_login_status_bar.config( text=LOGIN_STATUS_BAR_TEXT["connectFailed"], fg=STATUS_BAR_COLOR["failed"] ) # 修改文本框为可修改 self.__set_login_tools_active(True) self.btn_connect.config(text=BTN_TEXT_CONN["connect"]) return except WrongAuthkeyException: # 授权码错误 # 显示到状态栏 self.label_login_status_bar.config( text=LOGIN_STATUS_BAR_TEXT["wrongAuthkey"], fg=STATUS_BAR_COLOR["failed"] ) # 修改文本框为可修改 self.__set_login_tools_active(True) self.btn_connect.config(text=BTN_TEXT_CONN["connect"]) return except BotNotExistException: # bot不存在错误 self.label_login_status_bar.config( text=LOGIN_STATUS_BAR_TEXT["qqNotExist"], fg=STATUS_BAR_COLOR["failed"] ) # 修改文本框为可修改 self.__set_login_tools_active(True) self.btn_connect.config(text=BTN_TEXT_CONN["connect"]) return self.label_login_status_bar.config( text=LOGIN_STATUS_BAR_TEXT["connected"], fg=STATUS_BAR_COLOR["passed"] ) # 修改连接状态值 GlobalValues.is_connected = True # 修改上次连接键值对 ConfigOperation.modify_dict("lastConnection", { "host": GlobalValues.conn_host, "port": GlobalValues.conn_port, "authkey": GlobalValues.conn_authkey, "qq": GlobalValues.conn_qq }) # 修改文件中自动连接开关 ConfigOperation.modify_dict("autoConnect", self.auto_connect_var.get()) else: # 如果要断开连接 # 修改文本框为可修改 self.__set_login_tools_active(True) # 修改按钮名称 self.btn_connect.config(text=BTN_TEXT_CONN["connect"]) # 修改属性值 self.label_login_status_bar.config( text=LOGIN_STATUS_BAR_TEXT["disconnectSuccess"], fg=STATUS_BAR_COLOR["normal"] ) # 释放session Conn.release_session_key() # 修改连接状态值 GlobalValues.is_connected = False