Use new yaboli version
This commit is contained in:
parent
84ad3ee74e
commit
3e40aaf9f7
1 changed files with 39 additions and 105 deletions
138
infobot.py
138
infobot.py
|
|
@ -1,122 +1,56 @@
|
||||||
import sys
|
import sys
|
||||||
import yaboli
|
import yaboli
|
||||||
|
from yaboli.utils import *
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class InfoBot(yaboli.Bot):
|
class InfoBot(yaboli.Bot):
|
||||||
"""
|
"""
|
||||||
Display information about the clients connected to a room in its nick.
|
Display information about the clients connected to a room in its nick.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, name):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(name)
|
||||||
|
|
||||||
self.bot_description = ("This bot displays information about the clients connected to the\n"
|
self.help_specific = (
|
||||||
"current room in its nick: (<people>P <bots>B <lurkers>L)\n\n"
|
"Displays information about the clients in a room in its nick:\n"
|
||||||
"Created by @Garmy using yaboli (https://github.com/Garmelon/yaboli)\n"
|
"(<people>P <bots>B [<lurkers>L] [<bot-lurkers>N])\n\n"
|
||||||
"Github: https://github.com/Garmelon/infobot")
|
"Created by @Garmy using yaboli (https://github.com/Garmelon/yaboli)\n"
|
||||||
|
"Github: https://github.com/Garmelon/infobot (complies with botrulez)"
|
||||||
self.add_command("info", self.info_command, "Show more detailed info.",
|
|
||||||
("!info @bot [ --list[=<who>] | --name=<name> ]\n"
|
|
||||||
"--list=<who> : list user names and client/session ids\n"
|
|
||||||
"--name=<name> : list info for users with the name specified\n"
|
|
||||||
"<who> can be: people, bots, lurkers, all\n"
|
|
||||||
"<name> is the name of the person\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()) + 1, # we are a bot
|
|
||||||
len(self.room.get_lurkers())
|
|
||||||
)
|
)
|
||||||
|
|
||||||
self.room.set_nick(nick)
|
async def update_nick(self):
|
||||||
|
p = len(self.room.listing.get(types=["account", "agent"], lurker=False))
|
||||||
|
b = 1 + len(self.room.listing.get(types=["bot"], lurker=False))
|
||||||
|
l = len(self.room.listing.get(types=["account", "agent"], lurker=True))
|
||||||
|
n = len(self.room.listing.get(types=["bot"], lurker=True))
|
||||||
|
|
||||||
def info_command(self, message, arguments, flags, options):
|
name = []
|
||||||
"""
|
if p > 0: name.append(f"{p}P")
|
||||||
info_command(message, arguments, flags, options) -> None
|
if b > 0: name.append(f"{b}B")
|
||||||
|
if l > 0: name.append(f"{l}L")
|
||||||
|
if n > 0: name.append(f"{n}N")
|
||||||
|
name = "\u0001(" + " ".join(name) + ")"
|
||||||
|
|
||||||
Send a more verbose message.
|
await self.set_nick(name)
|
||||||
"""
|
|
||||||
|
|
||||||
if "name" in options and "list" in options:
|
async def on_join(self, session):
|
||||||
msg = "Sorry, the --list and --name options can't be used simultaneously."
|
await self.update_nick()
|
||||||
|
|
||||||
elif ("name" in options and options["name"] is not True) or "list" in options:
|
async def on_part(self, session):
|
||||||
sessions = self.room.get_sessions() + [self.room.session]
|
await self.update_nick()
|
||||||
clients = []
|
|
||||||
|
|
||||||
if "name" in options:
|
async def on_snapshot(self, user_id, session_id, version, listing, log, nick=None,
|
||||||
name = self.room.mentionable(options["name"]).lower()
|
pm_with_nick=None, pm_with_user_id=None):
|
||||||
|
await self.update_nick()
|
||||||
|
|
||||||
clients = [c for c in sessions
|
def main():
|
||||||
if self.room.mentionable(c.name).lower() == name]
|
if len(sys.argv) != 2:
|
||||||
|
print("USAGE:")
|
||||||
|
print(f" {sys.argv[0]} <room>")
|
||||||
|
return
|
||||||
|
|
||||||
if name[:1] == "@":
|
run_bot(InfoBot, sys.argv[1], "()")
|
||||||
name = name[1:]
|
|
||||||
clients.extend([c for c in 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() + [self.room.session]
|
|
||||||
elif options["list"] == "people":
|
|
||||||
clients = self.room.get_people()
|
|
||||||
elif options["list"] == "bots":
|
|
||||||
clients = self.room.get_bots() + [self.room.session]
|
|
||||||
elif options["list"] == "lurkers":
|
|
||||||
clients = self.room.get_lurkers()
|
|
||||||
|
|
||||||
if clients:
|
|
||||||
msg = ""
|
|
||||||
for client in sorted(clients, key=lambda c: c.name.lower()):
|
|
||||||
msg += "id={}, session_id={}, name={}\n".format(
|
|
||||||
repr(client.id),
|
|
||||||
repr(client.session_id),
|
|
||||||
repr(client.name)
|
|
||||||
)
|
|
||||||
|
|
||||||
msg = msg[:-1] # remove trailing newline
|
|
||||||
else:
|
|
||||||
msg = "No clients"
|
|
||||||
|
|
||||||
else:
|
|
||||||
people = len(self.room.get_people())
|
|
||||||
accounts = len(self.room.get_accounts())
|
|
||||||
bots = len(self.room.get_bots()) + 1
|
|
||||||
lurkers = len(self.room.get_lurkers())
|
|
||||||
|
|
||||||
msg = "people: {} (with accounts: {})\nbots: {}\nlurkers: {}\ntotal: {}"
|
|
||||||
msg = msg.format(people, accounts, bots, lurkers, people + bots + lurkers)
|
|
||||||
|
|
||||||
self.room.send_message(msg, message.id)
|
|
||||||
|
|
||||||
def main(rooms, imode=False):
|
|
||||||
manager = yaboli.BotManager(InfoBot, default_nick="infobot")
|
|
||||||
|
|
||||||
for room in rooms:
|
|
||||||
manager.create(room)
|
|
||||||
|
|
||||||
#if imode:
|
|
||||||
#manager.interactive_mode()
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
args = sys.argv[1:]
|
main()
|
||||||
|
|
||||||
if "-i" in args:
|
|
||||||
imode = True
|
|
||||||
args.remove("-i")
|
|
||||||
else:
|
|
||||||
imode=False
|
|
||||||
|
|
||||||
main(args, imode=imode)
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue