botanybot/bot.py

188 lines
8.4 KiB
Python
Raw Normal View History

2021-06-21 16:04:10 -05:00
import asyncio
2021-06-22 17:40:48 -05:00
import os
import random
import time
import subprocess
2021-06-21 16:04:10 -05:00
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
2021-06-22 23:04:09 -05:00
from admin import Admin
2021-06-21 16:04:10 -05:00
from secrets import PASSWORD
channels = [
2021-06-22 22:57:08 -05:00
"#bots",
"#club",
2021-06-22 17:40:48 -05:00
"###",
"#forero",
2021-06-21 16:04:10 -05:00
]
2021-06-22 23:37:40 -05:00
helpmessage = "hey, i'm botanybot. i water plants on ~club. my prefix is % and i was made by randomuser. check out https://ttm.sh/Fs4.txt for more information."
2021-06-22 17:40:48 -05:00
def userchooser(user):
2021-06-23 15:48:24 -05:00
return random.choice([i for i in os.listdir(r"/home") if i.lower()[0] == user.lower()[0]])
2021-06-21 16:04:10 -05:00
2021-06-22 23:33:12 -05:00
# borrowed from kiedtl/ircbot
async def _aexec(self, code):
# Make an async function with the code and `exec` it
exec(f"async def __ex(self): " + "".join(f"\n {l}" for l in code.split("\n")))
# Get `__ex` from local variables, call it and return the result
return await locals()["__ex"](self)
2021-06-21 16:04:10 -05:00
class Server(BaseServer):
2021-06-22 23:04:09 -05:00
admin = Admin('rndusr')
2021-06-21 16:04:10 -05:00
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]))
2021-06-22 17:40:48 -05:00
def isDrunk(self):
2021-06-23 15:48:24 -05:00
if abs(self.drunkentime - int(time.time())) > 60 * 4: return True
2021-06-22 17:40:48 -05:00
return False
2021-06-21 16:04:10 -05:00
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]))
2021-06-22 17:40:48 -05:00
self.drunkentime = 0
2021-06-21 16:04:10 -05:00
if line.command == "PRIVMSG":
user = line.hostmask.nickname
if line.params[-1] == "!rollcall":
await self.msg(user, helpmessage, user)
2021-07-11 13:01:23 -05:00
if line.params[-1] == "!botlist":
await self.msg(user, helpmessage, user)
2021-06-21 16:04:10 -05:00
if line.params[-1][0] == '%':
commands = line.params[-1][1:].split(' ')
2021-06-22 22:57:08 -05:00
if commands[0] == "score":
2021-06-23 16:03:28 -05:00
if len(commands) == 1: commands.append(user)
if self.isDrunk(): b = Botany(commands[1])
else:
b = Botany(userchooser(commands[1]))
while b.getInfo() == []:
2021-06-22 22:57:08 -05:00
b = Botany(userchooser(commands[1]))
2021-06-23 16:03:28 -05:00
i = b.getInfo()
await self.msg(user, "{}'s score: {}".format(b.user, str(int(b.score()))), user)
elif commands[0] == "online":
proc = subprocess.Popen('w', stdout=subprocess.PIPE)
try: out, err = proc.communicate(timeout=10)
except:
proc.kill()
return
out = out.decode("utf-8")
out = out.split("\n")
out = len(set([i.split(" ")[0] for i in out][2:-1]))
await self.msg(user, "{} users are currently online".format(out), user)
2021-06-23 16:52:00 -05:00
elif commands[0] == "pct":
if len(commands) == 1: commands.append(user)
b = Botany(commands[1])
i = b.getInfo()
await self.msg(user, "warning: the %pct command is experimental and could possibly not work. you have been warned.", user)
2021-06-23 16:52:00 -05:00
if len(i) > 1:
pct = max((1 - ((time.time() - i['last_watered'])/86400)) * 100, 0)
await self.msg(user, "plant percentage for {}: {}%".format(b.user, int(pct)), user)
2021-06-23 16:52:00 -05:00
else:
await self.msg(user, "couldn't find plant for {}".format(commands[1]), user)
2021-06-22 22:57:08 -05:00
elif commands[0] == "vodka":
2021-06-22 17:40:48 -05:00
if self.isDrunk():
self.drunkentime = int(time.time())
await self.msg(user, "glug glug glug", user)
2021-06-22 17:40:48 -05:00
else:
await self.msg(user, "vodka? what's vodka? *burp*", user)
2021-06-22 22:57:08 -05:00
elif commands[0] == "desc":
2021-06-23 16:03:28 -05:00
if len(commands) == 1: commands.append(user)
if self.isDrunk():
b = Botany(commands[1])
2021-06-21 16:04:10 -05:00
else:
2021-06-23 16:03:28 -05:00
b = Botany(userchooser(commands[1]))
await self.msg(user, b.plantDescription(), user)
2021-06-21 16:04:10 -05:00
elif commands[0] == "water":
2021-06-23 16:03:28 -05:00
if len(commands) == 1: commands.append(user)
if self.isDrunk():
b = Botany(commands[1])
if b.water("{} (via IRC)".format(user)):
await self.msg(user, b.watered(), user)
2021-06-21 16:04:10 -05:00
else:
await self.msg(user, b.cantWater(), user)
2021-06-23 16:03:28 -05:00
else:
b = Botany(userchooser(commands[1]))
while not b.water("{} (via IRC)".format(user)):
2021-06-22 17:40:48 -05:00
b = Botany(userchooser(commands[1]))
await self.msg(user, b.watered(), user)
2021-06-21 16:04:10 -05:00
elif commands[0] == "help":
await self.msg(user, helpmessage, user)
2021-06-21 16:04:10 -05:00
elif commands[0] == "join":
if len(commands) == 2:
if user == self.admin:
await self.send(build("JOIN", [commands[1]]))
await self.msg(user, "joined the user {}".format(commands[1]), user)
2021-06-23 16:03:28 -05:00
else:
await self.msg(user, "you're not an admin", user)
2021-06-23 16:03:28 -05:00
else:
await self.msg(user, "specify the user", user)
2021-06-22 23:04:09 -05:00
elif commands[0] == "addowner":
2021-06-21 16:04:10 -05:00
if len(commands) == 2:
if user == self.admin:
2021-06-22 23:04:09 -05:00
self.admin.append(commands[1])
await self.msg(user, "admin added: {}".format(commands[1]), user)
2021-06-22 23:04:09 -05:00
return
else:
await self.msg(user, "error: you must be an admin!", user)
2021-06-22 23:04:09 -05:00
return
else:
await self.msg(user, "two arguments required", user)
2021-06-22 23:04:09 -05:00
return
elif commands[0] == "delowner":
if len(commands) == 2:
if user == self.admin:
try: self.admin.remove(commands[1])
except:
await self.msg(user, "problem with removing admin", user)
2021-06-22 23:04:09 -05:00
return
await self.msg(user, "admin deleted: {}".format(commands[1]), user)
2021-06-21 16:04:10 -05:00
return
else:
await self.msg(user, "error: you must be an admin!", user)
2021-06-21 16:04:10 -05:00
return
else:
await self.msg(user, "two arguments required", user)
2021-06-21 16:04:10 -05:00
return
2021-06-22 23:33:12 -05:00
elif commands[0] == "amowner":
if user == self.admin:
await self.msg(user, "you're an admin", user)
2021-06-22 23:33:12 -05:00
else:
await self.msg(user, "you're not an admin", user)
elif commands[0] == "ping": await self.msg(user, "pong", user)
2021-06-22 23:33:12 -05:00
elif commands[0] == "eval":
if user == self.admin:
text = ' '.join(line.params[-1][1:].split(' ')[1:])
try:
result = await _aexec(self, text)
except Exception as e:
await self.msg(user, "segfault: {}".format(repr(e)), user)
2021-06-22 23:33:12 -05:00
return
await self.msg(user, "{}".format(result), user)
2021-06-22 23:33:12 -05:00
else:
await self.msg(user, "must have admin to execute", user)
2021-06-22 23:33:12 -05:00
2021-06-21 16:04:10 -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("botanybot", "tilde.chat", 6697, True)
await bot.add_server("tilde", params)
await bot.run()
if __name__ == "__main__":
asyncio.run(main())