def deladmin(msg=None, debug=False): '''(管理员)删除管理员''' admins = config.get('admins') command, options, words = extract_texts(msg.text) non_admins, removed = [], [] failed = [] response = u'' if not words: return u'拜托,你想删除谁的管理员权限?一次性说完啦!' for person in words: _admin = person if person.startswith('@') else '@%s' % person # Cannot delete self if _admin == msg.from_user.name: failed.append(_admin) continue # Cannot delete owner if _admin == config.get('owner'): failed.append(_admin) continue if _admin not in admins: non_admins.append(_admin) else: admins.remove(_admin) removed.append(_admin) if removed: response = u'移除了这些管理员:%s' % u'、'.join(removed) if non_admins: response += u'\n你想删除的 %s 还不是管理员' % u'、'.join(non_admins) if failed: response += u'\n这些管理员不能被你删除:%s' % u'、'.join(failed) return response
def addadmin(msg=None, debug=False): '''(管理员)添加管理员''' command, options, words = extract_texts(msg.text) if not words: return u'拜托,你想添加谁为管理员?一次性说完啦!' admins = config.get('admins') added = [] for person in words: _admin = person if person.startswith('@') else '@%s' % person if _admin not in admins: admins.append(_admin) added.append(_admin) return u'添加了 %d 位管理员:\n%s' % (len(added), u'、'.join(added), )
def handle_client_command(t_msg): global config command, options, words = extract_texts(t_msg.text) if command != "pi": return {"text": u"并不懂你在说什么"} if len(options) > 0: subcommand = options[0][1:] # drop the leading slash else: if len(words) > 0: subcommand = words[0] else: subcommand = "ping" if subcommand in ("ping", "pong"): return {"text": ("pong" if subcommand == "ping" else "ping")} elif subcommand == "uptime": try: import uptime except ImportError: return {"text": u"没有安装 uptime 模块哦"} else: return {"text": u"启动于 北京时间 {}".format(uptime.boottime().strftime("%Y-%m-%d %H:%M:%S"))} elif subcommand == "free": try: import psutil except ImportError: return {"text": u"没有安装 psutil 模块哦"} else: memory_usage = psutil.virtual_memory() swap_usage = psutil.swap_memory() return { "text": (u"内存使用率 {:.2f}%,共有 {:d} MB\n" u"SWAP 使用率 {:.2f}%,共有 {:d} MB").format( memory_usage.percent, memory_usage.total / 1024 / 1024, swap_usage.percent, swap_usage.total / 1024 / 1024, ) } elif subcommand == "photo": if t_msg.from_user.name != config["owner"] and t_msg.from_user.name[1:] != config["owner"]: return {"text": u"区区凡人,竟敢对我下这种命令"} return {"photo": upload_photo()} elif subcommand in ("temp", "temperature", "pressure", "env"): return {"text": read_env(subcommand)} else: return {"text": u"当听不懂你在说什么时,我会假装看风景"}
def addvimtip(msg=None, debug=False): '''(管理员)添加一条 vim 使用技巧''' usage = '命令格式:\n/addvimtip [/forceadd]\n内容\n解释' if msg.text is None: return usage command, options, words = extract_texts(msg.text) if not words: return usage force_add = '/forceadd' in options # We do not want words here, we want lines parts = [i.strip() for i in msg.text.strip().split('\n')] if len(parts) < 3 or not all([bool(i) for i in parts]): return usage content, comment = parts[1], parts[2] tips = get_hash('vimtips') if content in tips: if not force_add: return '这条 tip 已经存在了,希望覆盖的话可以使用 /forceadd 选项' tips.update({ content: comment }) return u'添加了一条 vimtip:\n%s\n%s' % (content, comment, )
def ping(msg=None, debug=False): '''检测机器人在线情况''' time_str = datetime.strftime(datetime.now(), '%m/%d %H:%M:%S') command, options, words = extract_texts(msg.text) response = 'pong' if command == 'ping' else 'ping' return u'%s (%s UTC)' % (response, time_str, )