Port to yaboli rewrite 5
This commit is contained in:
parent
8f39839e1b
commit
986f7ced19
6 changed files with 175 additions and 179 deletions
13
.gitignore
vendored
13
.gitignore
vendored
|
|
@ -1,3 +1,12 @@
|
||||||
**/__pycache__
|
# python stuff
|
||||||
|
__pycache__/
|
||||||
|
|
||||||
|
# venv stuff
|
||||||
|
bin/
|
||||||
|
include/
|
||||||
|
lib/
|
||||||
|
lib64
|
||||||
|
pyvenv.cfg
|
||||||
|
|
||||||
|
# config files
|
||||||
*.conf
|
*.conf
|
||||||
*.cookie
|
|
||||||
|
|
|
||||||
2
LICENSE
2
LICENSE
|
|
@ -1,6 +1,6 @@
|
||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2018 Garmelon
|
Copyright (c) 2018 - 2019 Garmelon
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
|
||||||
5
bot.conf.default
Normal file
5
bot.conf.default
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
[general]
|
||||||
|
nick = ()
|
||||||
|
|
||||||
|
[rooms]
|
||||||
|
test
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
[general]
|
|
||||||
nick = ()
|
|
||||||
cookiefile = infobot.cookie
|
|
||||||
|
|
||||||
[rooms]
|
|
||||||
# Format:
|
|
||||||
# room
|
|
||||||
# room=password
|
|
||||||
test
|
|
||||||
324
infobot.py
324
infobot.py
|
|
@ -1,201 +1,191 @@
|
||||||
import asyncio
|
|
||||||
import configparser
|
|
||||||
import logging
|
import logging
|
||||||
import re
|
|
||||||
|
|
||||||
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.
|
Displays information about the clients connected to a room in its nick.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
async def on_command_specific(self, room, message, command, nick, argstr):
|
ALIASES = ["InfoBot", "PBL", "(PBL)", "PBLN", "(PBLN)"]
|
||||||
is_mentioned = similar(nick, room.session.nick) or similar(nick, "infobot")
|
HELP_GENERAL = "/me counts the types of clients in its nick"
|
||||||
is_mentioned = is_mentioned or re.fullmatch("p?bl?n?|\(p?bl?n?\)", normalize(nick))
|
|
||||||
if is_mentioned:
|
|
||||||
if not argstr:
|
|
||||||
await self.botrulez_ping(room, message, command)
|
|
||||||
await self.botrulez_uptime(room, message, command)
|
|
||||||
await self.botrulez_kill(room, message, command)
|
|
||||||
await self.botrulez_restart(room, message, command)
|
|
||||||
|
|
||||||
await self.command_recount(room, message, command)
|
HELP_SPECIFIC = [
|
||||||
|
"Displays information about the clients in a room in its nick:"
|
||||||
|
" (<people>P\u00A0<bots>B\u00A0<lurkers>L\u00A0<bot-lurkers>N)",
|
||||||
|
"You can also use @InfoBot, @PBL or @(PBL) for bot commands.",
|
||||||
|
"",
|
||||||
|
"!recount {atmention} - Recount people in the room",
|
||||||
|
#"!detail {atmention} - Detailed list of clients in this room",
|
||||||
|
#"!detail {atmention} @person - Detailed info regarding @person",
|
||||||
|
#"!hosts {atmention} [--mention] - Lists all hosts currently in this room",
|
||||||
|
"",
|
||||||
|
"Created by @Garmy using https://github.com/Garmelon/yaboli.",
|
||||||
|
"For additional info, try \"!help {atmention} <topic>\". Topics:",
|
||||||
|
"\tcount, lurkers, changelog",
|
||||||
|
]
|
||||||
|
|
||||||
await self.command_help(room, message, command, argstr)
|
HELP_COUNT = [
|
||||||
await self.command_detail(room, message, command, argstr)
|
"This bot counts the number of clients connected to a room. If you"
|
||||||
|
" open a room in two different tabs, the bot counts you twice.",
|
||||||
|
"The euphoria client, on the other hand, usually displays all"
|
||||||
|
" connections of an account with the same nick as one in the nick"
|
||||||
|
" list. Because of that, this bot's count is always at least as high"
|
||||||
|
" as the number of nicks on the nick list, similar to the number on"
|
||||||
|
" the button to toggle the nick list.",
|
||||||
|
"",
|
||||||
|
#"If the bot's count is off, try a !recount or a !restart {atmention}.",
|
||||||
|
"If the bot's count is off, try a !recount.",
|
||||||
|
]
|
||||||
|
|
||||||
async def on_command_general(self, room, message, command, argstr):
|
HELP_LURKERS = [
|
||||||
if not argstr:
|
"People or bots who are connected to the room but haven't chosen a"
|
||||||
await self.botrulez_ping(room, message, command)
|
" nick are lurkers. The euphoria client doesn't display them in the"
|
||||||
await self.botrulez_help(room, message, command, text="I count the types of clients in my nick")
|
" nick list.",
|
||||||
|
"This bot differentiates between people (L) and bots (N) who are"
|
||||||
|
" lurking.",
|
||||||
|
]
|
||||||
|
|
||||||
await self.command_hosts(room, message, command, argstr)
|
HELP_CHANGELOG = [
|
||||||
|
"(2019-04-12) update to yaboli rewrite 5",
|
||||||
|
]
|
||||||
|
|
||||||
@yaboli.command("help")
|
HELP_TOPICS = {
|
||||||
async def command_help(self, room, message, argstr):
|
"count": HELP_COUNT,
|
||||||
nick = mention(room.session.nick)
|
"lurkers": HELP_LURKERS,
|
||||||
args = self.parse_args(argstr)
|
"changelog": HELP_CHANGELOG,
|
||||||
if not args:
|
}
|
||||||
text = (
|
|
||||||
"Displays information about the clients in a room in its nick:"
|
|
||||||
" (<people>P\u00A0<bots>B\u00A0<lurkers>L\u00A0<bot-lurkers>N)\n"
|
|
||||||
"You can also use @InfoBot, @PBL or @(PBL) for bot commands.\n"
|
|
||||||
"\n"
|
|
||||||
"!recount {nick} - Recount people in the room\n"
|
|
||||||
"!detail {nick} - Detailed list of clients in this room\n"
|
|
||||||
"!detail {nick} @person - Detailed info regarding @person\n"
|
|
||||||
"!hosts {nick} [--mention] - Lists all hosts currently in this room\n"
|
|
||||||
"\n"
|
|
||||||
"Created by @Garmy using https://github.com/Garmelon/yaboli.\n"
|
|
||||||
"For additional info, try \"!help {nick} <topic>\". Topics:\n"
|
|
||||||
" count, lurkers, changelog"
|
|
||||||
).format(nick=nick)
|
|
||||||
await room.send(text, message.mid)
|
|
||||||
else:
|
|
||||||
for topic in args:
|
|
||||||
if topic == "count":
|
|
||||||
text = (
|
|
||||||
"This bot counts the number of clients connected to a room. If you"
|
|
||||||
" open a room in two different tabs, the bot counts you twice.\n"
|
|
||||||
"The euphoria client, on the other hand, usually displays all"
|
|
||||||
" connections of an account with the same nick as one in the nick list."
|
|
||||||
" Because of that, this bot's count is always as high as, or higher than,"
|
|
||||||
" the number of nicks on the nick list, similar to the number on the"
|
|
||||||
" button to toggle the nick list.\n"
|
|
||||||
"\n"
|
|
||||||
"If the bot's count is off, try a !recount or a !restart {nick}."
|
|
||||||
).format(nick=nick)
|
|
||||||
elif topic == "lurkers":
|
|
||||||
text = (
|
|
||||||
"People or bots who are connected to the room but haven't chosen a"
|
|
||||||
" nick are lurkers. The euphoria client doesn't display them in the"
|
|
||||||
" nick list.\n"
|
|
||||||
"This bot differentiates between people (L) and bots (N) who are"
|
|
||||||
" lurking."
|
|
||||||
)
|
|
||||||
elif topic == "changelog":
|
|
||||||
text = (
|
|
||||||
"- add !recount command\n"
|
|
||||||
"- fix bot counting incorrectly\n"
|
|
||||||
"- port to rewrite-4 of yaboli\n"
|
|
||||||
"- add !detail and !manager commands\n"
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
text = f"Topic {topic!r} does not exist."
|
|
||||||
|
|
||||||
await room.send(text, message.mid)
|
def __init__(self, config_file):
|
||||||
|
super().__init__(config_file)
|
||||||
|
self.register_botrulez(help_=False, kill=True) # using our own help functions
|
||||||
|
self.register_general("help", self.cmd_help_general, args=False)
|
||||||
|
self.register_specific("help", self.cmd_help_specific)
|
||||||
|
self.register_specific("recount", self.cmd_recount, args=False)
|
||||||
|
|
||||||
async def update_nick(self, room):
|
async def cmd_help_specific(self, room, message, args):
|
||||||
p = len(room.listing.get(types=["account", "agent"], lurker=False))
|
if not args.has_args():
|
||||||
b = len(room.listing.get(types=["bot"], lurker=False))
|
await message.reply(self.format_help(room, self.HELP_SPECIFIC))
|
||||||
l = len(room.listing.get(types=["account", "agent"], lurker=True))
|
return
|
||||||
n = len(room.listing.get(types=["bot"], lurker=True))
|
|
||||||
|
|
||||||
name = []
|
if len(args.basic()) > 5:
|
||||||
if p > 0: name.append(f"{p}P")
|
await message.reply("A maximum of 5 help topics is allowed.")
|
||||||
if b > 0: name.append(f"{b}B")
|
return
|
||||||
if l > 0: name.append(f"{l}L")
|
|
||||||
if n > 0: name.append(f"{n}N")
|
|
||||||
name = "\u0001(" + " ".join(name) + ")"
|
|
||||||
|
|
||||||
if room.session.nick != name:
|
for topic in args.basic():
|
||||||
await room.nick(name)
|
help_lines = self.HELP_TOPICS.get(topic.lower())
|
||||||
|
if help_lines is None:
|
||||||
|
await message.reply(f"Module {topic!r} not found.")
|
||||||
|
else:
|
||||||
|
await message.reply(self.format_help(room, help_lines))
|
||||||
|
|
||||||
async def on_connected(self, room, log):
|
async def cmd_recount(self, room, message, args):
|
||||||
await self.update_nick(room)
|
await self.update_nick(room)
|
||||||
|
await message.reply("Recalibrated.")
|
||||||
|
|
||||||
async def on_join(self, room, session):
|
# Updating the nick
|
||||||
await self.update_nick(room)
|
|
||||||
await room.who()
|
|
||||||
await self.update_nick(room)
|
|
||||||
|
|
||||||
async def on_part(self, room, session):
|
def format_nick(self, users):
|
||||||
await self.update_nick(room)
|
people = 0
|
||||||
await room.who()
|
bots = 1 # room.who() doesn't include the bot itself.
|
||||||
await self.update_nick(room)
|
lurkers = 0
|
||||||
|
nurkers = 0
|
||||||
|
|
||||||
async def on_nick(self, room, sid, uid, from_nick, to_nick):
|
for user in users:
|
||||||
await self.update_nick(room)
|
if user.is_bot:
|
||||||
await room.who()
|
if user.nick:
|
||||||
await self.update_nick(room)
|
bots += 1
|
||||||
|
else:
|
||||||
|
nurkers += 1
|
||||||
|
else: # user is person or something else
|
||||||
|
if user.nick:
|
||||||
|
people += 1
|
||||||
|
else:
|
||||||
|
lurkers += 1
|
||||||
|
|
||||||
@yaboli.command("recount")
|
info = []
|
||||||
async def command_recount(self, room, message):
|
|
||||||
await room.who()
|
|
||||||
await self.update_nick(room)
|
|
||||||
await room.send("Recalibrated.", message.mid)
|
|
||||||
|
|
||||||
@yaboli.command("detail")
|
if people > 0:
|
||||||
async def command_detail(self, room, message, argstr):
|
info.append(f"{people}P")
|
||||||
sessions = room.listing.get()
|
|
||||||
args = self.parse_args(argstr)
|
|
||||||
|
|
||||||
if args:
|
info.append(f"{bots}B")
|
||||||
lines = []
|
|
||||||
for arg in args:
|
|
||||||
if arg.startswith("@") and arg[1:]:
|
|
||||||
nick = arg[1:]
|
|
||||||
else:
|
|
||||||
nick = arg
|
|
||||||
|
|
||||||
for ses in sessions:
|
if lurkers > 0:
|
||||||
if similar(ses.nick, nick):
|
info.append(f"{lurkers}L")
|
||||||
lines.append(self.format_session(ses))
|
|
||||||
|
|
||||||
if lines:
|
if nurkers > 0:
|
||||||
text = "\n".join(lines)
|
info.append(f"{nurkers}N")
|
||||||
else:
|
|
||||||
text = "No sessions found that match any of the nicks."
|
|
||||||
await room.send(text, message.mid)
|
|
||||||
|
|
||||||
else:
|
return "\u0001(" + " ".join(info) + ")"
|
||||||
sessions = sorted(sessions, key=lambda s: s.uid)
|
|
||||||
lines = [self.format_session(s) for s in sessions]
|
|
||||||
text = "\n".join(lines)
|
|
||||||
await room.send(text, message.mid)
|
|
||||||
|
|
||||||
@staticmethod
|
async def update_nick(self, room):
|
||||||
def format_session(s):
|
users = await room.who()
|
||||||
is_staff = "yes" if s.is_staff else "no"
|
new_nick = self.format_nick(users)
|
||||||
is_manager = "yes" if s.is_manager else "no"
|
await room.nick(new_nick)
|
||||||
return f"UID: {s.uid}\t| SID: {s.sid}\t| staff: {is_staff}\t| host: {is_manager}\t| nick: {s.nick!r}"
|
|
||||||
|
|
||||||
@yaboli.command("hosts")
|
async def on_connected(self, room):
|
||||||
async def command_hosts(self, room, message, argstr):
|
await self.update_nick(room)
|
||||||
flags, args, kwargs = self.parse_flags(self.parse_args(argstr))
|
|
||||||
sessions = room.listing.get()
|
|
||||||
sessions = sorted(set(s.nick for s in sessions if s.is_manager))
|
|
||||||
|
|
||||||
if "ping" in kwargs:
|
async def on_join(self, room, user):
|
||||||
sessions = [mention(s) for s in sessions]
|
await self.update_nick(room)
|
||||||
else:
|
|
||||||
sessions = [s for s in sessions]
|
|
||||||
|
|
||||||
if sessions:
|
async def on_part(self, room, user):
|
||||||
text = "Hosts that are currently in this room:\n" + "\n".join(sessions)
|
await self.update_nick(room)
|
||||||
else:
|
|
||||||
text = "No hosts currently in this room."
|
|
||||||
await room.send(text, message.mid)
|
|
||||||
|
|
||||||
def main(configfile):
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
|
||||||
|
|
||||||
config = configparser.ConfigParser(allow_no_value=True)
|
# @yaboli.command("detail")
|
||||||
config.read(configfile)
|
# async def command_detail(self, room, message, argstr):
|
||||||
|
# sessions = room.listing.get()
|
||||||
nick = config.get("general", "nick")
|
# args = self.parse_args(argstr)
|
||||||
cookiefile = config.get("general", "cookiefile", fallback=None)
|
#
|
||||||
bot = InfoBot(nick, cookiefile=cookiefile)
|
# if args:
|
||||||
|
# lines = []
|
||||||
for room, password in config.items("rooms"):
|
# for arg in args:
|
||||||
if not password:
|
# if arg.startswith("@") and arg[1:]:
|
||||||
password = None
|
# nick = arg[1:]
|
||||||
bot.join_room(room, password=password)
|
# else:
|
||||||
|
# nick = arg
|
||||||
asyncio.get_event_loop().run_forever()
|
#
|
||||||
|
# for ses in sessions:
|
||||||
|
# if similar(ses.nick, nick):
|
||||||
|
# lines.append(self.format_session(ses))
|
||||||
|
#
|
||||||
|
# if lines:
|
||||||
|
# text = "\n".join(lines)
|
||||||
|
# else:
|
||||||
|
# text = "No sessions found that match any of the nicks."
|
||||||
|
# await room.send(text, message.mid)
|
||||||
|
#
|
||||||
|
# else:
|
||||||
|
# sessions = sorted(sessions, key=lambda s: s.uid)
|
||||||
|
# lines = [self.format_session(s) for s in sessions]
|
||||||
|
# text = "\n".join(lines)
|
||||||
|
# await room.send(text, message.mid)
|
||||||
|
#
|
||||||
|
# @staticmethod
|
||||||
|
# def format_session(s):
|
||||||
|
# is_staff = "yes" if s.is_staff else "no"
|
||||||
|
# is_manager = "yes" if s.is_manager else "no"
|
||||||
|
# return f"UID: {s.uid}\t| SID: {s.sid}\t| staff: {is_staff}\t| host: {is_manager}\t| nick: {s.nick!r}"
|
||||||
|
#
|
||||||
|
# @yaboli.command("hosts")
|
||||||
|
# async def command_hosts(self, room, message, argstr):
|
||||||
|
# flags, args, kwargs = self.parse_flags(self.parse_args(argstr))
|
||||||
|
# sessions = room.listing.get()
|
||||||
|
# sessions = sorted(set(s.nick for s in sessions if s.is_manager))
|
||||||
|
#
|
||||||
|
# if "ping" in kwargs:
|
||||||
|
# sessions = [mention(s) for s in sessions]
|
||||||
|
# else:
|
||||||
|
# sessions = [s for s in sessions]
|
||||||
|
#
|
||||||
|
# if sessions:
|
||||||
|
# text = "Hosts that are currently in this room:\n" + "\n".join(sessions)
|
||||||
|
# else:
|
||||||
|
# text = "No hosts currently in this room."
|
||||||
|
# await room.send(text, message.mid)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main("infobot.conf")
|
yaboli.enable_logging(level=logging.DEBUG)
|
||||||
|
yaboli.run(InfoBot)
|
||||||
|
|
|
||||||
1
requirements.txt
Normal file
1
requirements.txt
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
yaboli >=0.2.0, <0.3.0
|
||||||
Loading…
Add table
Add a link
Reference in a new issue