From bfcae02ce426dc8963e66e4015e34cabc77e01f1 Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 23 May 2016 17:50:13 +0200 Subject: [PATCH] Add infobot --- infobot.py | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 infobot.py diff --git a/infobot.py b/infobot.py new file mode 100644 index 0000000..32e82f5 --- /dev/null +++ b/infobot.py @@ -0,0 +1,102 @@ +import yaboli +import operator + +class InfoBot(yaboli.Bot): + """ + Display information about the clients connected to a room in its nick. + """ + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + self.bot_description = ("This bot displays information about the clients connected to the\n" + "current room in its nick: (p: , b: , l: )\n" + "Created by @Garmy using yaboli (Yet Another Bot Library)") + + self.add_command("info", self.info_command, "Show more detailed info.", + ("!info @bot [ --list[=] | --name= ]\n" + "--list= : list user names and client and session ids\n" + " can be: people, bots, lurkers, all\n" + " is the name of the person (without leading '@')\n\n" + "Shows different information about the clients connected to the\n" + "current room.")) + + self.room.add_callback("sessions", self.update_nick) + + self.update_nick() + + def update_nick(self): + """ + update_nick(self) -> None + + Change the name to display the correct values. + """ + + nick = "\001(p: {}, b: {}, l: {})".format( + len(self.room.get_people()), + len(self.room.get_bots()), + len(self.room.get_lurkers()) + ) + + self.room.set_nick(nick) + + def info_command(self, message, arguments, flags, options): + """ + info_command(message, arguments, flags, options) -> None + + Send a more verbose message. + """ + + if "name" in options and "list" in options: + msg = "Sorry, the --list and --name options can't be used simultaneously." + + elif ("name" in options and options["name"] is not True) or "list" in options: + if "name" in options: + name = self.room.mentionable(options["name"]).lower() + + clients = [c for c in self.room.get_sessions() + if self.room.mentionable(c.name).lower() == name] + + elif "list" in options: + if options["list"] is True or options["list"] == "all": + clients = self.room.get_sessions() + elif options["list"] == "people": + clients = self.room.get_people() + elif options["list"] == "bots": + clients = self.room.get_bots() + elif options["list"] == "lurkers": + clients = self.room.get_lurkers() + + if clients: + msg = "" + for client in sorted(clients, key=operator.attrgetter("name")): + msg += "id={}, session_id={}, name={}\n".format( + repr(client.id), + repr(client.session_id), + repr(client.name) + ) + + msg = msg[:-1] # remove trailing newline + else: + return + + else: + people = len(self.room.get_people()) + accounts = len(self.room.get_accounts()) + bots = len(self.room.get_bots()) + lurkers = len(self.room.get_lurkers()) + + msg = "people: {}\n with accounts: {}\nbots: {}\nlurkers: {}\n\ntotal: {}\ntotal visible: {}" + msg = msg.format( + people, + accounts, + bots, + lurkers, + people + bots + lurkers, + people + bots + ) + + self.room.send_message(msg, message.id) + +manager = yaboli.BotManager(InfoBot, default_nick="infobot") +manager.create("test")