misc tidying
This commit is contained in:
parent
f9c16f0e42
commit
1a7b63055b
76
tracer.py
76
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
|
||||
elif words[0] == "!!fuse":
|
||||
if "|" in words:
|
||||
res = fuse(words[1 : words.index("|")])
|
||||
if res:
|
||||
return " ".join(words[words.index("|") + 1 :]) + " " + res
|
||||
else:
|
||||
res = fuse(words[1:])
|
||||
if res:
|
||||
return res
|
||||
return " ".join(DB)
|
||||
elif words[0] == "!!source":
|
||||
return REPOLINK
|
||||
elif words[0] == "!botlist" or words[0] == "!!help":
|
||||
elif words[0] in ["!!help", "!botlist"]:
|
||||
return HELPTEXT
|
||||
elif words[0][0:2] == "!!":
|
||||
elif words[0] == "!!fuse":
|
||||
if "|" in words:
|
||||
w = words.index("|")
|
||||
print(words[1:w])
|
||||
res = fuse(words[1:w])
|
||||
if res:
|
||||
return " ".join(words[w + 1 :]) + " " + res
|
||||
else:
|
||||
print(words[1:])
|
||||
res = fuse(words[1:])
|
||||
print(res)
|
||||
if res:
|
||||
return res
|
||||
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__":
|
||||
|
|
Loading…
Reference in New Issue