From 1a7b63055b7a879bd5a4c6f5d18f24e51a5a051e Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Wed, 24 Feb 2021 15:06:13 -0500 Subject: misc tidying --- tracer.py | 68 +++++++++++++++++++++++++++++---------------------------------- 1 file changed, 31 insertions(+), 37 deletions(-) (limited to 'tracer.py') diff --git a/tracer.py b/tracer.py index f683146..b8706ef 100644 --- a/tracer.py +++ b/tracer.py @@ -4,7 +4,6 @@ from irctokens import build, Line from ircrobots import Bot as BaseBot from ircrobots import Server as BaseServer from ircrobots import ConnectionParams, SASLUserPass -from random import randint, choice from tracery.modifiers import base_english import asyncio import configparser @@ -19,17 +18,17 @@ HELPTEXT = "helo i'm a tracery bot that makes cool things from tracery grammars REPOLINK = "https://tildegit.org/ben/tracer" DB = {} + +# read configuration config = configparser.ConfigParser( converters={"list": lambda x: [i.strip() for i in x.split(",")]} ) config.read("config.ini") -bot = config["irc"] +config = config["irc"] -# read account info if it exists -if os.path.isfile("account.ini"): - account = configparser.ConfigParser() - account.read("account.ini") - account = account["nickserv"] +account = configparser.ConfigParser() +account.read("account.ini") +account = account["nickserv"] def grammar(rules): @@ -55,7 +54,7 @@ def populate(): for p in glob.glob("/home/*/.tracery/*"): name, ext = os.path.splitext(p) name = os.path.basename(name) - if name.startswith(".") or ext not in (".json", ""): + if name.startswith(".") or ext not in [".json", ""]: continue if p in DB: DB[name].append(p) @@ -74,10 +73,7 @@ def generate(rule): def listify(col): - if type(col) == type([]): - return col - else: - return [col] + return col if type(col) == type([]) else [col] def shuffle(col): @@ -106,36 +102,34 @@ def fuse(argv): def think(line): - chan = line.params[0] words = line.params[1].split(" ") if len(words) > 0: if words[0] == "!!list": - res = "" - for k in DB: - res += k + " " - return res + return " ".join(DB) + elif words[0] == "!!source": + return REPOLINK + elif words[0] in ["!!help", "!botlist"]: + return HELPTEXT elif words[0] == "!!fuse": if "|" in words: - res = fuse(words[1 : words.index("|")]) + w = words.index("|") + print(words[1:w]) + res = fuse(words[1:w]) if res: - return " ".join(words[words.index("|") + 1 :]) + " " + res + return " ".join(words[w + 1 :]) + " " + res else: + print(words[1:]) res = fuse(words[1:]) + print(res) if res: return res - elif words[0] == "!!source": - return REPOLINK - elif words[0] == "!botlist" or words[0] == "!!help": - return HELPTEXT - elif words[0][0:2] == "!!": + elif words[0].startswith("!!"): res = generate(words[0][2:]) if res: - if len(words) >= 3: - if words[1] == "|": - return " ".join(words[2:]) + " " + res - else: - return res + if "|" in words: + w = words.index("|") + return " ".join(words[w + 1 :]) + " " + res else: return res @@ -147,7 +141,7 @@ class Server(BaseServer): async def line_read(self, line: Line): print(f"{self.name} < {line.format()}") if line.command == "001": - await self.send(build("JOIN", [",".join(bot.getlist("channels"))])) + await self.send(build("JOIN", [",".join(config.getlist("channels"))])) await self.send(build("MODE", [self.nickname, "+B"])) if line.command == "INVITE": @@ -178,18 +172,18 @@ class Bot(BaseBot): async def main(): - ircrobot = Bot() + bot = Bot() params = ConnectionParams( - bot["nick"], - host=bot["server"], - port=bot.getint("port"), - tls=bot.getboolean("tls"), + config["nick"], + host=config["server"], + port=config.getint("port"), + tls=config.getboolean("tls"), sasl=SASLUserPass(account["username"], account["password"]), ) - await ircrobot.add_server("tilde", params) - await ircrobot.run() + await bot.add_server("tilde", params) + await bot.run() if __name__ == "__main__": -- cgit 1.4.1