fix channel joining
This commit is contained in:
parent
80a61da9a0
commit
73945d8ec2
81
tracer.py
81
tracer.py
|
@ -13,6 +13,7 @@ import random
|
|||
|
||||
DB = {}
|
||||
|
||||
|
||||
def grammar(rules):
|
||||
try:
|
||||
res = tracery.Grammar(rules)
|
||||
|
@ -21,6 +22,7 @@ def grammar(rules):
|
|||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
|
||||
def load_rules(path):
|
||||
try:
|
||||
with open(path) as f:
|
||||
|
@ -28,6 +30,7 @@ def load_rules(path):
|
|||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
|
||||
def populate():
|
||||
global DB
|
||||
DB = {}
|
||||
|
@ -35,17 +38,19 @@ def populate():
|
|||
d = f"/home/{subdir}/.tracery"
|
||||
if os.path.isdir(d):
|
||||
for p in os.listdir(d):
|
||||
rule = d+"/"+p
|
||||
rule = d + "/" + p
|
||||
name = p.replace(".json", "")
|
||||
#print(p, rule, name)
|
||||
# print(p, rule, name)
|
||||
if p in DB:
|
||||
DB[name].append(grammar(load_rules(rule)))
|
||||
else:
|
||||
DB[name] = [grammar(load_rules(rule))]
|
||||
|
||||
|
||||
populate()
|
||||
print(DB)
|
||||
|
||||
|
||||
def generate(rule):
|
||||
populate()
|
||||
if rule in DB:
|
||||
|
@ -59,10 +64,11 @@ def listify(col):
|
|||
else:
|
||||
return [col]
|
||||
|
||||
|
||||
def shuffle(col):
|
||||
a = random.choice(col.keys())
|
||||
b = random.choice(col.keys())
|
||||
if 'origin' in [a,b]:
|
||||
if "origin" in [a, b]:
|
||||
print("origin discard")
|
||||
return col
|
||||
temp = col[a]
|
||||
|
@ -70,6 +76,7 @@ def shuffle(col):
|
|||
col[b] = temp
|
||||
return col
|
||||
|
||||
|
||||
def fuse(argv):
|
||||
populate()
|
||||
raw = {}
|
||||
|
@ -78,7 +85,7 @@ def fuse(argv):
|
|||
g = random.choice(DB[gk]).raw
|
||||
for k in g:
|
||||
if k in raw:
|
||||
raw[k] = listify(raw[k])+listify(g[k])
|
||||
raw[k] = listify(raw[k]) + listify(g[k])
|
||||
else:
|
||||
raw[k] = g[k]
|
||||
for i in range(20):
|
||||
|
@ -90,43 +97,37 @@ server = "127.0.0.1"
|
|||
channels = ["#bots"]
|
||||
if len(sys.argv) > 1:
|
||||
for c in sys.argv[1:]:
|
||||
channels.append("#"+c)
|
||||
channels.append("#" + c)
|
||||
botnick = "tracer"
|
||||
|
||||
|
||||
def rawsend(msg):
|
||||
print(msg)
|
||||
ircsock.send(f"{msg}\r\n".encode())
|
||||
|
||||
def joinchan(chan):
|
||||
rawsend(f"JOIN {chan}")
|
||||
|
||||
def sendmsg(chan, msg):
|
||||
def send(chan, msg):
|
||||
# This is the send message function, it simply sends messages to the channel.
|
||||
rawsend(f"PRIVMSG {chan} :{msg}")
|
||||
rawsend(f"PRIVMSG #{chan} :{msg}")
|
||||
|
||||
# def wexec(msg):
|
||||
# ircsock.send("EXEC -msg "+channel+" "+msg)
|
||||
|
||||
def send(channel, s):
|
||||
sendmsg(f"#{channel}", s)
|
||||
|
||||
def think(chan, nick, msg):
|
||||
words = re.split('[ \t\s]+', msg)
|
||||
words = re.split("[ \t\s]+", msg)
|
||||
if len(words) > 0 and nick != "tracer":
|
||||
if words[0] == "!!list":
|
||||
res = ""
|
||||
for k in DB:
|
||||
res += k+" "
|
||||
res += k + " "
|
||||
send(chan, res[:475])
|
||||
elif words[0] == "!!fuse":
|
||||
if "|" in words:
|
||||
res = fuse(words[1:words.index("|")])
|
||||
res = fuse(words[1 : words.index("|")])
|
||||
if res:
|
||||
send(chan, " ".join(words[words.index("|")+1:])+" "+res)
|
||||
send(chan, " ".join(words[words.index("|") + 1 :]) + " " + res)
|
||||
else:
|
||||
res = fuse(words[1:])
|
||||
if res:
|
||||
send(chan,res[0:475])
|
||||
send(chan, res[0:475])
|
||||
elif words[0][0:2] == "!!":
|
||||
print(words)
|
||||
res = generate(words[0][2:])
|
||||
|
@ -139,33 +140,31 @@ def think(chan, nick, msg):
|
|||
else:
|
||||
send(chan, res)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
ircsock.connect((server, 6667)) # Here we connect to the server using port 6667
|
||||
rawsend(f"NICK {botnick}") # here we actually assign the nick to the bot
|
||||
rawsend(f"USER {botnick} 0 * :tracery bot") # user authentication
|
||||
|
||||
for c in channels:
|
||||
joinchan(c)
|
||||
ircsock.connect((server, 6667)) # Here we connect to the server using port 6667
|
||||
rawsend(f"NICK {botnick}") # here we actually assign the nick to the bot
|
||||
rawsend(f"USER {botnick} 0 * :tracery bot") # user authentication
|
||||
|
||||
while 1:
|
||||
ircmsg = ircsock.recv(2048)
|
||||
ircmsg = ircmsg.decode().strip('\n\r')
|
||||
print(ircmsg)
|
||||
msg = ircsock.recv(2048).decode().split("\r\n")
|
||||
for ircmsg in msg:
|
||||
print(ircmsg)
|
||||
|
||||
if ircmsg.find("PING") != -1:
|
||||
rawsend(ircmsg.replace("I", "O"))
|
||||
if "PING" in ircmsg:
|
||||
rawsend(ircmsg.replace("I", "O", 1))
|
||||
|
||||
if ircmsg.find("001") != -1:
|
||||
for c in channels:
|
||||
joinchan(c)
|
||||
if "001" in ircmsg:
|
||||
for c in channels:
|
||||
rawsend(f"JOIN {c}")
|
||||
|
||||
m = re.match(':(?P<nick>[^ ]+)!.*PRIVMSG #(?P<chan>\w+) :(?P<msg>.*)', ircmsg)
|
||||
if m and m.groupdict():
|
||||
m = m.groupdict()
|
||||
try:
|
||||
think(m["chan"], m["nick"], m["msg"])
|
||||
except Exception as e:
|
||||
print("ERROR" + str(m))
|
||||
print(e)
|
||||
m = re.match(":(?P<nick>[^ ]+)!.*PRIVMSG #(?P<chan>\w+) :(?P<msg>.*)", ircmsg)
|
||||
if m and m.groupdict():
|
||||
m = m.groupdict()
|
||||
try:
|
||||
think(m["chan"], m["nick"], m["msg"])
|
||||
except Exception as e:
|
||||
print("ERROR" + str(m))
|
||||
print(e)
|
||||
|
||||
|
|
Loading…
Reference in New Issue