commit cd7b61670065baaf91f7bc8d335c5027cc57eb1b Author: randomuser Date: Mon Jun 21 21:04:10 2021 +0000 better late than never diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..debe898 --- /dev/null +++ b/bot.py @@ -0,0 +1,83 @@ +import asyncio + +from irctokens import build, Line +from ircrobots import Bot as BaseBot +from ircrobots import Server as BaseServer +from ircrobots import ConnectionParams + +from botany import IRCBotany as Botany + +from secrets import PASSWORD + +channels = [ + "#bots", +# "#club", +] +helpmessage = "hey, i'm botanybot. i water plants on ~club. my prefix is % and i was made by randomuser. check out https://ttm.sh/Fki.txt for more information." + +class Server(BaseServer): + admin = 'rndusr' + async def msg(self, chan, string, user=None): + if user == None: await self.send(build("PRIVMSG", [chan, string])) + else: await self.send(build("PRIVMSG", [chan, user + ": " + string])) + async def line_read(self, line: Line): + print(f"{self.name} < {line.format()}") + if line.command == "001": + await self.send(build("MODE", ["botanybot", "+B"])) + await self.msg("nickserv", "identify " + PASSWORD) + for i in channels: + await self.send(build("JOIN", [i])) + if line.command == "PRIVMSG": + user = line.hostmask.nickname + channel = line.params[0] + if line.params[-1] == "!rollcall": + await self.msg(channel, helpmessage, user) + if line.params[-1][0] == '%': + commands = line.params[-1][1:].split(' ') + if commands[0] == "desc": + if len(commands) == 2: + b = Botany(commands[1]) + await self.msg(channel, b.plantDescription(), user) + else: + await self.msg(channel, "specify user", user) + elif commands[0] == "water": + if len(commands) == 2: + b = Botany(commands[1]) + if(b.water("{} (via IRC)".format(user))): + await self.msg(channel, b.watered(), user) + else: + await self.msg(channel, b.cantWater(), user) + elif commands[0] == "help": + await self.msg(channel, helpmessage, user) + elif commands[0] == "join": + if len(commands) == 2: + if user == self.admin: + await self.send(build("JOIN", [commands[1]])) + await self.msg(channel, "joined the channel {}".format(commands[1]), user) + elif commands[0] == "chowner": + if len(commands) == 2: + if user == self.admin: + self.admin = commands[1] + await self.msg(channel, "admin changed to {}".format(commands[1]), user) + return + else: + await self.msg(channel, "error: you must be an admin!", user) + return + else: + await self.msg(channel, "two arguments required", user) + return + 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("botanybot", "tilde.chat", 6697, True) + await bot.add_server("tilde", params) + await bot.run() + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/botany.py b/botany.py new file mode 100644 index 0000000..2f2f2ab --- /dev/null +++ b/botany.py @@ -0,0 +1,73 @@ +import json +import time +import os + +class Botany: + visitors = "/home/{}/.botany/visitors.json" + plant = "/home/{}/.botany/{}_plant_data.json" + def __init__(self, user): + self.user = user + def getVisitors(self): + formatted = self.visitors.format(self.user) + try: + if os.stat(formatted).st_size > 0: + with open(formatted) as fd: + return json.load(fd) + else: + return [] + except FileNotFoundError: + print("FileNotFoundError in getVisitors()") + return False + def insertVisitor(self, data, waterer): + if data == False: return False + formatted = self.visitors.format(self.user) + data.append({"timestamp": int(time.time()), "user": waterer}) + try: + with open(formatted, "w") as fd: + json.dump(data, fd, indent=4) + return True + except PermissionError: + print("PermissionError in insertVisitor()") + return False + except FileNotFoundError: + print("FileNotFoundError in insertVisitor()") + return False + def getInfo(self): + formatted = self.plant.format(self.user, self.user) + try: + with open(formatted) as fd: + return json.load(fd) + except: + print("error in getInfo()") + return [] + +class IRCBotany(Botany): + def plantDescription(self): + string = "" + data = self.getInfo() + if(data == []): return "{}'s invisible plant".format(self.user) + + string += self.user + print(self.user) + string += "'s " + if(data['is_dead']): string += "dead " + string += data['description'] + string += ' ' + string += "generation " + string += str(data['generation']) + string += ' ' + string += "plant" + + return string + + def cantWater(self): + return "I can't water {}!".format(self.plantDescription()) + + def watered(self): + return "I watered {}!".format(self.plantDescription()) + + def dead(self): + return "Your {} is dead!".format(self.plantDescription()) + + def water(self, waterer): + return self.insertVisitor(self.getVisitors(), waterer) diff --git a/secrets.py b/secrets.py new file mode 100644 index 0000000..1850fef --- /dev/null +++ b/secrets.py @@ -0,0 +1 @@ +PASSWORD='randomuser_bots'