Compare commits

...

14 Commits

Author SHA1 Message Date
xfnw d38345c3d9 add admin deleteword command 2022-05-17 23:04:29 -04:00
xfnw 9d9c9dcd34 fix recording nouns
i should not be allowed near a computer
2022-05-17 22:26:01 -04:00
vulpine 3e1c3dac5b
fox comment wording 2022-05-13 21:55:28 -04:00
xfnw 2ac269d7f8 ignore mutations of rawm while iterating 2022-05-12 08:43:29 -06:00
xfnw bccf3c855a fix addspook not deleting itself 2022-05-12 08:38:29 -06:00
vulpine c42e5edcc1
put channel back, whoops 2022-04-28 13:50:14 -04:00
vulpine 790aa9aff5
merge admin.py from upstream oirc 2022-04-28 13:47:47 -04:00
vulpine fd3174b1fe
merge bot.py from upstream oirc 2022-04-28 13:45:08 -04:00
vulpine f67b269b1b
build privmsg instead of using self.message
self.message will somewhat change behavior
2022-04-27 19:59:03 -04:00
xfnw 2a21330e8b update default enmul 2022-03-10 12:04:10 -07:00
xfnw 4d20f602a7 tokenize punctuation before recording words 2022-03-10 12:03:39 -07:00
xfnw dccdda1f5f improve ending balences 2022-03-10 09:41:56 -07:00
xfnw a886b95db6 make enmul stepping smaller 2022-03-09 16:45:50 -07:00
xfnw 7348ef0354 noun count should not be used to end sentances
what in the world was i thinking, and how did i not notice this sooner?
2022-03-09 14:00:51 -07:00
3 changed files with 125 additions and 65 deletions

17
bot.py
View File

@ -19,9 +19,7 @@ def is_admin(func):
):
await func(self, channel, nick, msg)
else:
await message(
self, "core", channel, "you do not have permission to do that"
)
await message(self, channel, "you do not have permission to do that")
return decorator
@ -54,7 +52,7 @@ def rawm(rname):
async def message(self, channel, msg):
modname = os.path.splittext(os.path.basename(inspect.stack()[1].filename))[0]
modname = os.path.splitext(os.path.basename(inspect.stack()[:2][-1].filename))[0]
await self.send(build("PRIVMSG", [channel, f"[\x036{modname}\x0f] {msg}"]))
@ -68,10 +66,10 @@ class Server(BaseServer):
if listener[0] == line.command:
asyncio.create_task(listener[1](self, line))
def line_preread(self, line: Line):
async def line_preread(self, line: Line):
print(f"{self.name} < {line.format()}")
def line_presend(self, line: Line):
async def line_presend(self, line: Line):
print(f"{self.name} > {line.format()}")
async def on_001(self, line):
@ -84,9 +82,9 @@ class Server(BaseServer):
asyncio.create_task(m.init(self))
shared.modules[i] = m
# depricated, to support old modules
async def message(self, channel, msg):
await self.send(build("PRIVMSG", [channel, msg]))
modname = os.path.splitext(os.path.basename(inspect.stack()[:2][-1].filename))[0]
await self.send(build("PRIVMSG", [channel, f"[\x036{modname}\x0f] {msg}"]))
async def on_privmsg(self, line):
if line.tags and "batch" in line.tags and line.tags["batch"] == "1":
@ -105,7 +103,7 @@ class Server(BaseServer):
await self.handle_command(channel, nick, msg)
async def handle_rawm(self, channel, nick, msg):
for i in shared.rawm:
for i in list(shared.rawm):
await shared.rawm[i](self, channel, nick, msg)
async def handle_command(self, channel, nick, msg):
@ -129,7 +127,6 @@ class Bot(BaseBot):
def create_server(self, name: str):
return Server(self, name)
async def main():
bot = Bot()

View File

@ -1,4 +1,4 @@
import importlib, time, asyncio, random
import importlib, time, asyncio, random, sys
from bot import *
quitmessages = [
@ -11,15 +11,17 @@ quitmessages = [
async def commit(self, chan, source, msg):
await self.quit("{} told me to commit {}".format(source, msg))
await self.send(build("QUIT", [f"{source} told me to commit {msg}!"]))
sys.exit()
async def quit(self, chan, source, msg):
await self.send(build("QUIT", [random.choice(quitmessages)]))
sys.exit()
async def reloadmods(self, chan, source, msg):
await self.message(chan, "[\x036admin\x0f] reloading modules...")
await self.message(chan, "reloading modules...")
shared.oldcmd = shared.commands
shared.commands = {}
shared.rawm = {}
@ -29,14 +31,13 @@ async def reloadmods(self, chan, source, msg):
for i in shared.modules:
importlib.reload(shared.modules[i])
await shared.modules[i].init(self)
# await self.message(chan, '[\x036admin\x0f] load {} sucess!'.format(i))
# await self.message(chan, 'load {} sucess!'.format(i))
await self.message(
chan,
"[\x036admin\x0f] done! {} modules reloaded!".format(len(shared.modules)),
chan, "done! {} modules reloaded!".format(len(shared.modules))
)
except:
await self.message(
chan, "[\x036admin\x0f] reload failed... attempting to recover..."
chan, "reload failed... {}...".format(repr(sys.exc_info()[1]))
)
shared.commands = shared.oldcmd
@ -46,104 +47,129 @@ async def rawcmd(self, chan, source, msg):
async def joins(self, chan, source, msg):
await self.message(chan, "[\x036admin\x0f] joining slowly as to not flood...")
await self.message(chan, "joining slowly as to not flood...")
for i in self.chandb.all():
await self.send(build("JOIN", [i["name"]]))
await asyncio.sleep(1)
print("joined {}".format(i["name"]))
await self.message(
chan,
"[\x036admin\x0f] Sucess! i may be laggy for a bit while i sort through all these channels...",
"Sucess! i may be laggy for a bit while i sort through all these channels...",
)
async def aexec(self, code):
async def aexec(self, code, chan=None, source=None, msg=None):
# Make an async function with the code and `exec` it
exec(f"async def __ex(self): " + "".join(f"\n {l}" for l in code.split("\n")))
exec(
f"async def __ex(self, chan, source, msg): "
+ "".join(f"\n {l}" for l in code.split("\\n"))
)
# Get `__ex` from local variables, call it and return the result
return await locals()["__ex"](self)
return await locals()["__ex"](self, chan, source, msg)
async def ev(self, chan, source, msg):
msg = msg.split(" ")
try:
await self.message(
chan,
"[\x036admin\x0f] ok, output: {}".format(
str(await aexec(self, " ".join(msg)))[:400]
),
chan, "ok, output: {}".format(str(await aexec(self, " ".join(msg)))[:400])
)
except:
await self.message(chan, "[\x036admin\x0f] exception in eval!")
await self.message(chan, "ut oh! {}".format(repr(sys.exc_info()[1])))
async def send(self, c, n, m):
msg = m.split(" ")
await self.message(msg.pop(0), " ".join(msg))
await self.message(c, "[\x036admin\x0f] sent")
await self.message(c, "sent")
async def shut(self, c, n, m):
shared.qtime[c] = time.time() + (60 * 10)
await self.message(c, "[\x036admin\x0f] Ok, il be back in 10 minutes")
await self.message(c, "Ok, il be back in 10 minutes")
async def deleteword(self, c, n, m):
starttime = time.time()
deleteme = m.split()
shared.db["conver"].delete(pre=deleteme)
shared.db["prew"].delete(pre=deleteme)
shared.db["conver"].delete(pro=deleteme)
shared.db["prew"].delete(pro=deleteme)
shared.db["noun"].delete(word=deleteme)
shared.db["beg"].delete(word=deleteme)
shared.db["end"].delete(word=deleteme)
await self.message(
c, f"word(s) deleted in {round(time.time()-starttime,2)} seconds"
)
async def schans(self, c, n, m):
self.chandb.delete()
for i in self.channels:
self.chandb.insert(dict(name=i))
await self.message(c, "[\x036admin\x0f] Ok")
await self.message(c, "Ok")
async def addalias(self, c, n, m):
al = m.split(" ")[0]
m = m[len(al) + 1 :] # dont use the list since i want trailing spaces
if al in self.cmd:
await self.message(c, "[\x036admin\x0f] no dont overwrite a command dummy")
return
self.cmd[al] = Alias(m).alias
Alias(al, m)
await self.message(c, '[\x036admin\x0f] added "{}" alias for "{}"'.format(al, m))
await self.message(c, 'added "{}" alias for "{}"'.format(al, m))
async def addcommand(self, c, n, m):
al = m.split(" ")[0]
m = m[len(al) + 1 :] # dont use the list since i want trailing spaces
Command(al, m)
await self.message(c, 'added "{}" alias for "{}"'.format(al, m))
async def addot(self, c, n, m):
al = m.split(" ")[0]
m = m[len(al) + 1 :] # dont use the list since i want trailing spaces
if al in shared.rawm:
await self.message(c, "[\x036admin\x0f] no dont overwrite a command dummy")
await self.message(c, "no dont overwrite a command dummy")
return
shared.rawm[al] = Ot(m, al).ot
Ot(al, m)
await self.message(c, '[\x036admin\x0f] added "{}" trigger for "{}"'.format(al, m))
await self.message(c, 'added "{}" trigger for "{}"'.format(al, m))
async def addspook(self, c, n, m):
al = m.split(" ")[0]
m = m[len(al) + 1 :] # dont use the list since i want trailing spaces
if al in shared.rawm:
await self.message(c, "[\x036admin\x0f] no dont overwrite a command dummy")
await self.message(c, "no dont overwrite a command dummy")
return
shared.rawm[al] = Spook(m, al).spook
Spook(al, m)
await self.message(c, '[\x036admin\x0f] added "{}" trigger for "{}"'.format(al, m))
await self.message(c, 'added "{}" trigger for "{}"'.format(al, m))
async def addtrigger(self, c, n, m):
al = m.split(" ")[0]
m = m[len(al) + 1 :] # dont use the list since i want trailing spaces
if al in shared.rawm:
await self.message(c, "[\x036admin\x0f] no dont overwrite a command dummy")
await self.message(c, "no dont overwrite a command dummy")
return
shared.rawm[al] = Trigger(m, al).trigger
Trigger(al, m)
await self.message(c, '[\x036admin\x0f] added "{}" trigger for "{}"'.format(al, m))
await self.message(c, 'added "{}" trigger for "{}"'.format(al, m))
class Ot:
def __init__(self, ms, al):
ots = {}
def __init__(self, al, ms):
self.ms = str(ms)
self.al = str(al)
self.__class__.ots[self.al] = ms
shared.rawm[self.al] = self.ot
async def ot(alself, self, c, n, m):
if alself.al in m and n != self.nickname:
@ -156,20 +182,30 @@ class Ot:
class Spook:
def __init__(self, ms, al):
spooks = {}
def __init__(self, al, ms):
self.ms = str(ms)
self.al = str(al)
self.__class__.spooks[self.al] = ms
shared.rawm[self.al] = self.spook
async def spook(alself, self, c, n, m):
if alself.al in m and n != self.nickname:
asyncio.create_task(self.message(c, alself.ms.format(m)))
self.send(build("PRIVMSG", [c, alself.ms.format(m)]))
shared.rawm.pop(alself.al)
class Trigger:
def __init__(self, ms, al):
triggers = {}
def __init__(self, al, ms):
self.ms = str(ms)
self.al = str(al)
self.__class__.triggers[al] = ms
shared.rawm[al] = self.trigger
async def trigger(alself, self, c, n, m):
if alself.al in m:
@ -180,9 +216,33 @@ class Trigger:
)
class Alias:
def __init__(self, ms):
class Command:
commands = {}
def __init__(self, cmd, ms):
self.ms = str(ms)
self.__class__.commands[cmd] = ms
shared.commands[cmd] = self.command
async def command(alself, self, chan, source, msg):
try:
out = await aexec(self, alself.ms, chan, source, msg)
except:
await self.message(chan, "ut oh! {}".format(repr(sys.exc_info()[1])))
else:
if out is not None and len(out) > 0:
await self.message(chan, str(out))
class Alias:
aliases = {}
def __init__(self, cmd, ms):
self.ms = str(ms)
self.__class__.aliases[cmd] = ms
shared.commands[cmd] = self.alias
async def alias(alself, self, c, n, m):
asyncio.create_task(
@ -201,8 +261,10 @@ commands = {
"send": send,
"joins": joins,
"shut": shut,
"deleteword": deleteword,
"schans": schans,
"addalias": addalias,
"addcommand": addcommand,
"addtrigger": addtrigger,
"addot": addot,
"addspook": addspook,
@ -214,7 +276,7 @@ commands = {
async def adminHandle(self, chan, source, msg):
msg = msg.split(" ")
if len(msg) < 1 or not msg[0] in commands:
await self.message(chan, "[\x036admin\x0f] Invalid command")
await self.message(chan, "Invalid command")
return
print("[ADMIN MODULE] {} told me to {}!!!".format(source, msg[0]))
asyncio.create_task(commands[msg.pop(0)](self, chan, source, " ".join(msg)))
@ -225,7 +287,6 @@ async def init(self):
self.admins = ["xfnw"]
return
self.cmd["admin"] = adminHandle
self.help["admin"] = [
"admin - various bot owner commands (more for subcommands)",

View File

@ -19,7 +19,7 @@ async def rec(self, m):
beg = shared.db["beg"]
end = shared.db["end"]
words = m.split()
words = re.sub(r"([\.,\?!])", r" \1", m).split()
if words[0] == "admin" or len(words) < 2:
return
@ -40,7 +40,7 @@ async def rec(self, m):
),
["id"],
)
noch.insert(dict(word=w))
noch.insert(dict(word=get(words, w)))
async def getNoun(self, words, c):
@ -78,7 +78,7 @@ async def genOut(self, noun):
out = [noun]
while (
beg.find_one(word=out[0]) is None
or nouns.count(word=out[0]) - 1 > iter * shared.enmul
or beg.count(word=out[0]) - 1 < shared.enmul / (1 + iter / shared.maxiter)
) and iter < shared.maxiter:
try:
out = [
@ -95,13 +95,13 @@ async def genOut(self, noun):
try:
out = [random.choice(list(prew.find(pro=out[0])))["pre"]] + out
except IndexError:
iter += 69
iter += 69420
iter += 1
coun += 1
iter = 0
while (
end.find_one(word=out[-1]) is None
or nouns.count(word=out[-1]) - 1 > iter * shared.enmul
or end.count(word=out[-1]) - 1 < shared.enmul / (1 + iter / shared.maxiter)
) and iter < shared.maxiter:
try:
out.append(
@ -118,14 +118,16 @@ async def genOut(self, noun):
try:
out.append(random.choice(list(prew.find(pre=out[-1])))["pro"])
except IndexError:
iter += 69
iter += 69420
iter += 1
coun += 1
if coun <= 3:
shared.enmul -= 1
elif coun >= shared.maxiter:
if coun <= 4:
shared.enmul += 1
elif coun >= shared.maxiter:
shared.enmul -= 1
print(f"coun {coun} enmul {shared.enmul} maxiter {shared.maxiter}")
return out
@ -160,7 +162,7 @@ async def go(self, c, n, m):
)
if msg[-1] == "\x01" and msg[0] != "\x01":
msg = msg[:-1]
await self.message(c, msg)
await self.send(build("PRIVMSG", [c, msg]))
async def init(self):
@ -171,10 +173,10 @@ async def init(self):
# delay between grabbing random messages and passively
# learning.
shared.learndelay = 1
# sentance ending weight, lower means longer sentances,
# higher means shorter sentances. this will need to slowly
# sentance ending weight, higher means longer sentances,
# lower means shorter sentances. this will need to slowly
# get larger as the database grows
shared.enmul = 9
shared.enmul = 200
shared.maxiter = 14
shared.rawm["nlp"] = filter