Compare commits

...

4 Commits
test ... main

1 changed files with 44 additions and 29 deletions

73
bot.py
View File

@ -2,6 +2,7 @@ import asyncio
import os import os
import random import random
import time import time
import subprocess
from irctokens import build, Line from irctokens import build, Line
from ircrobots import Bot as BaseBot from ircrobots import Bot as BaseBot
@ -17,6 +18,7 @@ channels = [
"#bots", "#bots",
"#club", "#club",
"###", "###",
"#forero",
] ]
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." 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."
@ -49,9 +51,10 @@ class Server(BaseServer):
self.drunkentime = 0 self.drunkentime = 0
if line.command == "PRIVMSG": if line.command == "PRIVMSG":
user = line.hostmask.nickname user = line.hostmask.nickname
channel = line.params[0]
if line.params[-1] == "!rollcall": if line.params[-1] == "!rollcall":
await self.msg(channel, helpmessage, user) await self.msg(user, helpmessage, user)
if line.params[-1] == "!botlist":
await self.msg(user, helpmessage, user)
if line.params[-1][0] == '%': if line.params[-1][0] == '%':
commands = line.params[-1][1:].split(' ') commands = line.params[-1][1:].split(' ')
if commands[0] == "score": if commands[0] == "score":
@ -62,97 +65,109 @@ class Server(BaseServer):
while b.getInfo() == []: while b.getInfo() == []:
b = Botany(userchooser(commands[1])) b = Botany(userchooser(commands[1]))
i = b.getInfo() i = b.getInfo()
await self.msg(channel, "{}'s score: {}".format(b.user, str(int(b.score()))), user) 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)
elif commands[0] == "pct": elif commands[0] == "pct":
if len(commands) == 1: commands.append(user) if len(commands) == 1: commands.append(user)
b = Botany(commands[1]) b = Botany(commands[1])
i = b.getInfo() i = b.getInfo()
await self.msg(user, "warning: the %pct command is experimental and could possibly not work. you have been warned.", user)
if len(i) > 1: if len(i) > 1:
pct = (1 - ((time.time() - i['last_watered'])/86400)) * 100 pct = max((1 - ((time.time() - i['last_watered'])/86400)) * 100, 0)
await self.msg(channel, "plant percentage for {}: {}%".format(b.user, int(pct)), user) await self.msg(user, "plant percentage for {}: {}%".format(b.user, int(pct)), user)
else: else:
await self.msg(channel, "couldn't find plant for {}".format(commands[1]), user) await self.msg(user, "couldn't find plant for {}".format(commands[1]), user)
elif commands[0] == "vodka": elif commands[0] == "vodka":
if self.isDrunk(): if self.isDrunk():
self.drunkentime = int(time.time()) self.drunkentime = int(time.time())
await self.msg(channel, "glug glug glug", user) await self.msg(user, "glug glug glug", user)
else: else:
await self.msg(channel, "vodka? what's vodka? *burp*", user) await self.msg(user, "vodka? what's vodka? *burp*", user)
elif commands[0] == "desc": elif commands[0] == "desc":
if len(commands) == 1: commands.append(user) if len(commands) == 1: commands.append(user)
if self.isDrunk(): if self.isDrunk():
b = Botany(commands[1]) b = Botany(commands[1])
else: else:
b = Botany(userchooser(commands[1])) b = Botany(userchooser(commands[1]))
await self.msg(channel, b.plantDescription(), user) await self.msg(user, b.plantDescription(), user)
elif commands[0] == "water": elif commands[0] == "water":
if len(commands) == 1: commands.append(user) if len(commands) == 1: commands.append(user)
if self.isDrunk(): if self.isDrunk():
b = Botany(commands[1]) b = Botany(commands[1])
if b.water("{} (via IRC)".format(user)): if b.water("{} (via IRC)".format(user)):
await self.msg(channel, b.watered(), user) await self.msg(user, b.watered(), user)
else: else:
await self.msg(channel, b.cantWater(), user) await self.msg(user, b.cantWater(), user)
else: else:
b = Botany(userchooser(commands[1])) b = Botany(userchooser(commands[1]))
while not b.water("{} (via IRC)".format(user)): while not b.water("{} (via IRC)".format(user)):
b = Botany(userchooser(commands[1])) b = Botany(userchooser(commands[1]))
await self.msg(channel, b.watered(), user) await self.msg(user, b.watered(), user)
elif commands[0] == "help": elif commands[0] == "help":
await self.msg(channel, helpmessage, user) await self.msg(user, helpmessage, user)
elif commands[0] == "join": elif commands[0] == "join":
if len(commands) == 2: if len(commands) == 2:
if user == self.admin: if user == self.admin:
await self.send(build("JOIN", [commands[1]])) await self.send(build("JOIN", [commands[1]]))
await self.msg(channel, "joined the channel {}".format(commands[1]), user) await self.msg(user, "joined the user {}".format(commands[1]), user)
else: else:
await self.msg(channel, "you're not an admin", user) await self.msg(user, "you're not an admin", user)
else: else:
await self.msg(channel, "specify the channel", user) await self.msg(user, "specify the user", user)
elif commands[0] == "addowner": elif commands[0] == "addowner":
if len(commands) == 2: if len(commands) == 2:
if user == self.admin: if user == self.admin:
self.admin.append(commands[1]) self.admin.append(commands[1])
await self.msg(channel, "admin added: {}".format(commands[1]), user) await self.msg(user, "admin added: {}".format(commands[1]), user)
return return
else: else:
await self.msg(channel, "error: you must be an admin!", user) await self.msg(user, "error: you must be an admin!", user)
return return
else: else:
await self.msg(channel, "two arguments required", user) await self.msg(user, "two arguments required", user)
return return
elif commands[0] == "delowner": elif commands[0] == "delowner":
if len(commands) == 2: if len(commands) == 2:
if user == self.admin: if user == self.admin:
try: self.admin.remove(commands[1]) try: self.admin.remove(commands[1])
except: except:
await self.msg(channel, "problem with removing admin", user) await self.msg(user, "problem with removing admin", user)
return return
await self.msg(channel, "admin deleted: {}".format(commands[1]), user) await self.msg(user, "admin deleted: {}".format(commands[1]), user)
return return
else: else:
await self.msg(channel, "error: you must be an admin!", user) await self.msg(user, "error: you must be an admin!", user)
return return
else: else:
await self.msg(channel, "two arguments required", user) await self.msg(user, "two arguments required", user)
return return
elif commands[0] == "amowner": elif commands[0] == "amowner":
if user == self.admin: if user == self.admin:
await self.msg(channel, "you're an admin", user) await self.msg(user, "you're an admin", user)
else: else:
await self.msg(channel, "you're not an admin", user) await self.msg(user, "you're not an admin", user)
elif commands[0] == "ping": await self.msg(channel, "pong", user) elif commands[0] == "ping": await self.msg(user, "pong", user)
elif commands[0] == "eval": elif commands[0] == "eval":
if user == self.admin: if user == self.admin:
text = ' '.join(line.params[-1][1:].split(' ')[1:]) text = ' '.join(line.params[-1][1:].split(' ')[1:])
try: try:
result = await _aexec(self, text) result = await _aexec(self, text)
except Exception as e: except Exception as e:
await self.msg(channel, "segfault: {}".format(repr(e)), user) await self.msg(user, "segfault: {}".format(repr(e)), user)
return return
await self.msg(channel, "{}".format(result), user) await self.msg(user, "{}".format(result), user)
else: else:
await self.msg(channel, "must have admin to execute", user) await self.msg(user, "must have admin to execute", user)
async def line_send(self, line: Line): async def line_send(self, line: Line):