Compare commits

...

3 Commits

Author SHA1 Message Date
randomuser 35e3df7b27 add database calls + some bugfixes 2021-07-21 00:13:01 -05:00
randomuser 615d3850f2 fix minor bug in db.py 2021-07-21 00:04:17 -05:00
randomuser be25f97cbf add 'add' method to db.DuckDB 2021-07-20 23:58:37 -05:00
2 changed files with 34 additions and 5 deletions

15
db.py
View File

@ -2,7 +2,18 @@ class DuckDB:
def __init__(self, location=None): def __init__(self, location=None):
self.location = location self.location = location
self.db = [] self.db = []
if location != None: read(location) if location != None: self.read(location)
def add(self, state, nick, abstime, reltime, channel):
self.db.append(DuckEvent(
"{}{} {} {} {}".format(
state,
nick,
str(abstime),
str(reltime),
channel,
)
))
def parse(self, fd): def parse(self, fd):
lines = [i.rstrip() for i in fd.readlines()] lines = [i.rstrip() for i in fd.readlines()]
@ -21,7 +32,7 @@ class DuckDB:
def write(self, location): def write(self, location):
fd = open(location, "w") fd = open(location, "w")
self.output(fd) self.output(fd)
fd.close fd.close()
class DuckEvent: class DuckEvent:
def __init__(self, line=None): def __init__(self, line=None):

24
main.py
View File

@ -2,6 +2,9 @@ import asyncio
import random import random
import time import time
from db import DuckDB
from db import DuckEvent
from irctokens import build, Line from irctokens import build, Line
from ircrobots import Bot as BaseBot from ircrobots import Bot as BaseBot
from ircrobots import Server as BaseServer from ircrobots import Server as BaseServer
@ -19,33 +22,47 @@ class Server(BaseServer):
duckactive = False duckactive = False
duckactivetime = 0 duckactivetime = 0
lastduck = 0 lastduck = 0
db = "duckdb"
async def msg(self, chan, msg, usr=None): async def msg(self, chan, msg, usr=None):
if usr != None: if usr != None:
await self.send(build("PRIVMSG", [chan, usr + ": " + msg])) await self.send(build("PRIVMSG", [chan, usr + ": " + msg]))
else: await self.send(build("PRIVMSG", [chan, msg])) else: await self.send(build("PRIVMSG", [chan, msg]))
async def msgall(self, msg): async def msgall(self, msg):
[await self.msg(channel, msg) for channel in self.channels] [await self.msg(channel, msg) for channel in self.channels]
async def new_duck(self): async def new_duck(self):
self.messages = 0 self.messages = 0
self.duckactive = True self.duckactive = True
self.duckactivetime = time.time() self.duckactivetime = time.time()
await self.msgall(lang["duck"]) await self.msgall(lang["duck"])
async def duck_test(self): async def duck_test(self):
if self.messages > 1 and random.randint(0, 99) < 10: await self.new_duck() if self.messages > 1 and random.randint(0, 99) < 10: await self.new_duck()
async def misstime(self): async def misstime(self):
return format(time.time() - self.lastduck, '.2f') return format(time.time() - self.lastduck, '.2f')
async def coughttime(self): async def coughttime(self):
return format(self.lastduck - self.duckactivetime, '.2f') return format(self.lastduck - self.duckactivetime, '.2f')
async def duck_action(self, user, chan): async def duck_action(self, user, chan):
db = DuckDB(self.db)
if self.duckactive: if self.duckactive:
self.duckactive = False self.duckactive = False
self.messages = 0 self.messages = 0
self.lastduck = time.time() self.lastduck = time.time()
await self.msgall(lang["duckcought"].format(user, chan, self.coughttime()))) await self.msgall(lang["duckcought"].format(user, chan, await self.coughttime()))
db.add("B", user, time.time(), float(await self.coughttime()), chan)
elif self.lastduck != 0: elif self.lastduck != 0:
await self.msg(chan, lang["noduck"].format(self.coughttime())), user) await self.msg(chan, lang["noduck"].format(await self.misstime()), user)
db.add("M", user, time.time(), float(await self.misstime()), chan)
else: else:
await self.msg(chan, lang["noduckstart"], user) await self.msg(chan, lang["noduckstart"], user)
db.add("M", user, time.time(), -1, chan)
db.write(self.db)
async def line_read(self, line: Line): async def line_read(self, line: Line):
print(f"{self.name} < {line.format()}") print(f"{self.name} < {line.format()}")
if line.command == "001": if line.command == "001":
@ -62,9 +79,10 @@ class Server(BaseServer):
return return
self.messages += 1 self.messages += 1
self.duck_test() await self.duck_test()
elif line.command == "INVITE": elif line.command == "INVITE":
await self.send(build("JOIN", [line.params[1]])) await self.send(build("JOIN", [line.params[1]]))
async def line_send(self, line: Line): async def line_send(self, line: Line):
print(f"{self.name} > {line.format()}") print(f"{self.name} > {line.format()}")