2021-07-20 16:53:33 -05:00
|
|
|
import asyncio
|
|
|
|
import random
|
2021-07-20 23:06:28 -05:00
|
|
|
import time
|
2021-07-20 16:53:33 -05:00
|
|
|
|
2021-07-21 00:13:01 -05:00
|
|
|
from db import DuckDB
|
|
|
|
from db import DuckEvent
|
2021-07-21 00:30:52 -05:00
|
|
|
from db import DuckStats
|
2021-07-21 00:13:01 -05:00
|
|
|
|
2021-07-21 00:57:09 -05:00
|
|
|
from channels import ChannelDB
|
|
|
|
|
2021-07-20 16:53:33 -05:00
|
|
|
from irctokens import build, Line
|
|
|
|
from ircrobots import Bot as BaseBot
|
|
|
|
from ircrobots import Server as BaseServer
|
|
|
|
from ircrobots import ConnectionParams
|
|
|
|
|
2021-07-20 23:16:15 -05:00
|
|
|
lang = {
|
|
|
|
"noduck": "there was no duck! you missed by {} seconds!",
|
|
|
|
"noduckstart": "there was no duck!",
|
|
|
|
"duckcought": "duck has been cought by {} in channel {} in {} seconds!",
|
|
|
|
"duck": "・゜゜・。。・゜゜\_o< QUACK!",
|
2021-07-21 00:30:52 -05:00
|
|
|
"stats": "{} has befriended {} ducks in {} different channels, having a befriend/loss ratio of {}.",
|
2021-07-20 23:16:15 -05:00
|
|
|
}
|
|
|
|
|
2021-07-21 00:20:03 -05:00
|
|
|
class DuckLogic:
|
2021-07-20 23:06:28 -05:00
|
|
|
async def new_duck(self):
|
|
|
|
self.messages = 0
|
|
|
|
self.duckactive = True
|
|
|
|
self.duckactivetime = time.time()
|
2021-07-20 23:16:15 -05:00
|
|
|
await self.msgall(lang["duck"])
|
2021-07-21 00:13:01 -05:00
|
|
|
|
2021-07-20 23:23:58 -05:00
|
|
|
async def duck_test(self):
|
|
|
|
if self.messages > 1 and random.randint(0, 99) < 10: await self.new_duck()
|
2021-07-21 00:13:01 -05:00
|
|
|
|
2021-07-20 23:18:31 -05:00
|
|
|
async def misstime(self):
|
|
|
|
return format(time.time() - self.lastduck, '.2f')
|
2021-07-21 00:13:01 -05:00
|
|
|
|
2021-07-20 23:18:31 -05:00
|
|
|
async def coughttime(self):
|
|
|
|
return format(self.lastduck - self.duckactivetime, '.2f')
|
2021-07-21 00:13:01 -05:00
|
|
|
|
2021-07-20 23:06:28 -05:00
|
|
|
async def duck_action(self, user, chan):
|
2021-07-21 00:13:01 -05:00
|
|
|
db = DuckDB(self.db)
|
2021-07-20 23:06:28 -05:00
|
|
|
if self.duckactive:
|
|
|
|
self.duckactive = False
|
|
|
|
self.messages = 0
|
|
|
|
self.lastduck = time.time()
|
2021-07-21 00:13:01 -05: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-20 23:16:15 -05:00
|
|
|
elif self.lastduck != 0:
|
2021-07-21 00:13:01 -05: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-20 23:06:28 -05:00
|
|
|
else:
|
2021-07-20 23:18:31 -05:00
|
|
|
await self.msg(chan, lang["noduckstart"], user)
|
2021-07-21 00:13:01 -05:00
|
|
|
db.add("M", user, time.time(), -1, chan)
|
|
|
|
db.write(self.db)
|
|
|
|
|
2021-07-21 00:20:03 -05:00
|
|
|
class Server(BaseServer, DuckLogic):
|
|
|
|
messages = 0
|
|
|
|
duckactive = False
|
|
|
|
duckactivetime = 0
|
|
|
|
lastduck = 0
|
|
|
|
db = "duckdb"
|
2021-07-21 00:57:09 -05:00
|
|
|
chandb = "chandb"
|
2021-07-21 00:20:03 -05:00
|
|
|
|
|
|
|
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]
|
|
|
|
|
2021-07-20 16:53:33 -05:00
|
|
|
async def line_read(self, line: Line):
|
|
|
|
print(f"{self.name} < {line.format()}")
|
|
|
|
if line.command == "001":
|
2021-07-21 00:57:09 -05:00
|
|
|
chans = ChannelDB(self.chandb)
|
|
|
|
for i in chans.list():
|
|
|
|
await self.send(build("JOIN", [i]))
|
2021-07-20 23:06:28 -05:00
|
|
|
elif line.command == "PRIVMSG":
|
|
|
|
print(line.params)
|
|
|
|
print(line.hostmask.nickname)
|
2021-07-20 16:53:33 -05:00
|
|
|
if line.params[1][0] == '%':
|
2021-07-21 00:30:52 -05:00
|
|
|
cmd = line.params[1].split(' ')[0][1:]
|
2021-07-20 23:06:28 -05:00
|
|
|
chan = line.params[0]
|
|
|
|
user = line.hostmask.nickname
|
2021-07-21 00:30:52 -05:00
|
|
|
args = line.params[1].split(' ')[1:]
|
2021-07-20 23:06:28 -05:00
|
|
|
if cmd == "bef": await self.duck_action(user, chan)
|
2021-07-21 00:20:03 -05:00
|
|
|
elif cmd == "trigger": await self.new_duck()
|
2021-07-21 00:30:52 -05: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]),
|
2021-07-21 00:32:36 -05:00
|
|
|
format(stats.ratio(args[0]), ".3f")
|
2021-07-21 00:30:52 -05:00
|
|
|
), user)
|
2021-07-20 23:06:28 -05:00
|
|
|
return
|
2021-07-20 16:53:33 -05:00
|
|
|
|
|
|
|
self.messages += 1
|
2021-07-21 00:13:01 -05:00
|
|
|
await self.duck_test()
|
2021-07-20 23:06:28 -05:00
|
|
|
elif line.command == "INVITE":
|
2021-07-21 00:57:09 -05:00
|
|
|
chans = ChannelDB(self.chandb)
|
2021-07-20 23:06:28 -05:00
|
|
|
await self.send(build("JOIN", [line.params[1]]))
|
2021-07-21 00:57:09 -05:00
|
|
|
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 00:13:01 -05:00
|
|
|
|
2021-07-20 16:53:33 -05: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)
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
bot = Bot()
|
|
|
|
params = ConnectionParams("test", "beepboop.systems", 6667, False)
|
|
|
|
await bot.add_server("beep", params)
|
|
|
|
await bot.run()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
asyncio.run(main())
|