From 35e3df7b277e55a445b72648ef63c0bf16234cbf Mon Sep 17 00:00:00 2001 From: randomuser Date: Wed, 21 Jul 2021 00:13:01 -0500 Subject: [PATCH] add database calls + some bugfixes --- db.py | 2 +- main.py | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/db.py b/db.py index d8468b3..e8de1ef 100644 --- a/db.py +++ b/db.py @@ -13,7 +13,7 @@ class DuckDB: str(reltime), channel, ) - ) + )) def parse(self, fd): lines = [i.rstrip() for i in fd.readlines()] diff --git a/main.py b/main.py index 3dbf10e..a29578e 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,9 @@ import asyncio import random import time +from db import DuckDB +from db import DuckEvent + from irctokens import build, Line from ircrobots import Bot as BaseBot from ircrobots import Server as BaseServer @@ -19,33 +22,47 @@ class Server(BaseServer): duckactive = False duckactivetime = 0 lastduck = 0 + db = "duckdb" + async def msg(self, chan, msg, usr=None): if usr != None: await self.send(build("PRIVMSG", [chan, usr + ": " + msg])) else: await self.send(build("PRIVMSG", [chan, msg])) + async def msgall(self, msg): [await self.msg(channel, msg) for channel in self.channels] + async def new_duck(self): self.messages = 0 self.duckactive = True self.duckactivetime = time.time() await self.msgall(lang["duck"]) + async def duck_test(self): if self.messages > 1 and random.randint(0, 99) < 10: await self.new_duck() + async def misstime(self): return format(time.time() - self.lastduck, '.2f') + async def coughttime(self): return format(self.lastduck - self.duckactivetime, '.2f') + async def duck_action(self, user, chan): + db = DuckDB(self.db) if self.duckactive: self.duckactive = False self.messages = 0 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: - 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: 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): print(f"{self.name} < {line.format()}") if line.command == "001": @@ -62,9 +79,10 @@ class Server(BaseServer): return self.messages += 1 - self.duck_test() + await self.duck_test() elif line.command == "INVITE": await self.send(build("JOIN", [line.params[1]])) + async def line_send(self, line: Line): print(f"{self.name} > {line.format()}")