universalducks/main.py

137 lines
4.6 KiB
Python
Raw Permalink Normal View History

2021-07-20 21:53:33 +00:00
import asyncio
import random
2021-07-21 04:06:28 +00:00
import time
2021-07-21 21:24:23 +00:00
import argparse
2021-07-20 21:53:33 +00:00
2021-07-21 05:13:01 +00:00
from db import DuckDB
from db import DuckEvent
2021-07-21 05:30:52 +00:00
from db import DuckStats
2021-07-21 05:13:01 +00:00
from channels import ChannelDB
2021-07-21 21:29:44 +00:00
from lang import lang
from admin import Admin
2021-07-21 22:01:07 +00:00
# fill in with password = "password"
from secret import password
2021-07-20 21:53:33 +00:00
from irctokens import build, Line
from ircrobots import Bot as BaseBot
from ircrobots import Server as BaseServer
from ircrobots import ConnectionParams
2021-07-21 05:20:03 +00:00
class DuckLogic:
2021-07-21 04:06:28 +00:00
async def new_duck(self):
self.messages = 0
self.duckactive = True
self.duckactivetime = time.time()
2021-07-21 04:16:15 +00:00
await self.msgall(lang["duck"])
2021-07-21 05:13:01 +00:00
async def duck_test(self):
2021-07-21 21:40:39 +00:00
if self.messages > 100 and random.randint(0, 99) < 10: await self.new_duck()
2021-07-21 05:13:01 +00:00
2021-07-21 04:18:31 +00:00
async def misstime(self):
return format(time.time() - self.lastduck, '.2f')
2021-07-21 05:13:01 +00:00
2021-07-21 04:18:31 +00:00
async def coughttime(self):
return format(self.lastduck - self.duckactivetime, '.2f')
2021-07-21 05:13:01 +00:00
2021-07-21 04:06:28 +00:00
async def duck_action(self, user, chan):
2021-07-21 05:13:01 +00:00
db = DuckDB(self.db)
2021-07-21 04:06:28 +00:00
if self.duckactive:
self.duckactive = False
self.messages = 0
self.lastduck = time.time()
2021-07-21 05:13:01 +00:00
await self.msgall(lang["duckcought"].format(user, chan, await self.coughttime()))
db.add("B", user, time.time(), float(await self.coughttime()), chan)
2021-07-21 04:16:15 +00:00
elif self.lastduck != 0:
2021-07-21 05:13:01 +00:00
await self.msg(chan, lang["noduck"].format(await self.misstime()), user)
db.add("M", user, time.time(), float(await self.misstime()), chan)
2021-07-21 04:06:28 +00:00
else:
2021-07-21 04:18:31 +00:00
await self.msg(chan, lang["noduckstart"], user)
2021-07-21 05:13:01 +00:00
db.add("M", user, time.time(), -1, chan)
db.write(self.db)
2021-07-21 05:20:03 +00:00
class Server(BaseServer, DuckLogic):
messages = 0
duckactive = False
duckactivetime = 0
lastduck = 0
db = "duckdb"
chandb = "chandb"
2021-07-21 05:20:03 +00:00
async def msg(self, chan, msg, usr=None):
2021-07-21 22:01:07 +00:00
await self.send(build("PRIVMSG", ["ChanServ", "identify {}".format(password)]))
2021-07-21 05:20:03 +00:00
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]
2021-07-20 21:53:33 +00:00
async def line_read(self, line: Line):
print(f"{self.name} < {line.format()}")
if line.command == "001":
chans = ChannelDB(self.chandb)
for i in chans.list():
await self.send(build("JOIN", [i]))
2021-07-21 04:06:28 +00:00
elif line.command == "PRIVMSG":
2021-07-20 21:53:33 +00:00
if line.params[1][0] == '%':
2021-07-21 05:30:52 +00:00
cmd = line.params[1].split(' ')[0][1:]
2021-07-21 04:06:28 +00:00
chan = line.params[0]
user = line.hostmask.nickname
2021-07-21 05:30:52 +00:00
args = line.params[1].split(' ')[1:]
2021-07-21 04:06:28 +00:00
if cmd == "bef": await self.duck_action(user, chan)
2021-07-21 21:40:39 +00:00
# elif cmd == "trigger": await self.new_duck()
2021-07-21 05:30:52 +00:00
elif cmd == "stats":
db = DuckDB(self.db)
stats = DuckStats(db)
await self.msg(chan, lang["stats"].format(
args[0],
stats.cought(args[0]),
stats.channels(args[0]),
format(stats.ratio(args[0]), ".3f")
2021-07-21 05:30:52 +00:00
), user)
2021-07-21 04:06:28 +00:00
return
2021-07-20 21:53:33 +00:00
self.messages += 1
2021-07-21 05:13:01 +00:00
await self.duck_test()
2021-07-21 04:06:28 +00:00
elif line.command == "INVITE":
chans = ChannelDB(self.chandb)
2021-07-21 04:06:28 +00:00
await self.send(build("JOIN", [line.params[1]]))
chans.add(line.params[1])
chans.write(self.chandb)
elif line.command == "KICK":
chans = ChannelDB(self.chandb)
chans.remove(line.params[0])
chans.write(self.chandb)
2021-07-21 05:13:01 +00:00
2021-07-20 21:53:33 +00:00
async def line_send(self, line: Line):
print(f"{self.name} > {line.format()}")
class Bot(BaseBot):
def create_server(self, name: str):
return Server(self, name)
2021-07-21 21:24:23 +00:00
async def main(srv, port, tls, nick):
print(srv, port, tls, nick)
2021-07-20 21:53:33 +00:00
bot = Bot()
2021-07-21 21:24:23 +00:00
params = ConnectionParams(nick, srv, port, tls)
await bot.add_server("main", params)
2021-07-20 21:53:33 +00:00
await bot.run()
2021-07-21 21:24:23 +00:00
def parse_args():
parser = argparse.ArgumentParser(description="cross-channel duck bot")
parser.add_argument('-s', '--host', default="beepboop.systems")
parser.add_argument('-p', '--port', default=6667, type=int)
parser.add_argument('-t', '--tls', action="store_true")
parser.add_argument('-n', '--nick', default="test")
2021-07-21 22:05:47 +00:00
parser.add_argument('-a', '--password', default=password)
2021-07-21 21:24:23 +00:00
return parser.parse_args()
2021-07-20 21:53:33 +00:00
if __name__ == "__main__":
2021-07-21 21:24:23 +00:00
args = parse_args()
2021-07-21 22:05:04 +00:00
password = args.password
2021-07-21 21:24:23 +00:00
asyncio.run(main(args.host, args.port, args.tls, args.nick))