60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
import irc.bot
|
|
import irc.strings
|
|
import jaraco.logging
|
|
import hashlib
|
|
import toml
|
|
import ssl
|
|
import string
|
|
import random
|
|
|
|
conf = toml.load(open("config.toml"))
|
|
|
|
class Bot(irc.bot.SingleServerIRCBot):
|
|
def __init__(self, servers, nick, channel):
|
|
super().__init__(servers, nick, nick, **{"ipv6": True})
|
|
print(dir(self))
|
|
self.channel = channel
|
|
|
|
def on_nicknameinuse(self, c, e):
|
|
c.nick("".join(random.choices(string.ascii_lowercase ,k=8)))
|
|
print("nick in use, changing")
|
|
|
|
def on_welcome(self, c, e):
|
|
c.join(self.channel)
|
|
print("joined", self.channel)
|
|
|
|
def on_privmsg(self, c, e):
|
|
self.do_command(e, e.arguments[0])
|
|
|
|
def on_pubmsg(self, c, e):
|
|
a = e.arguments[0].split(":", 1)
|
|
if len(a) > 1 and irc.strings.lower(a[0]) == irc.strings.lower(self.connection.get_nickname()):
|
|
self.do_command(e, a[1].strip())
|
|
return
|
|
|
|
def do_command(self, e, cmd):
|
|
nick = e.source.nick
|
|
c = self.connection
|
|
|
|
print("exec", nick, cmd)
|
|
|
|
if cmd == "stats":
|
|
for chname, chobj in self.channels.items():
|
|
c.notice(nick, "--- Channel statistics ---")
|
|
c.notice(nick, "Channel: " + chname)
|
|
users = sorted(chobj.users())
|
|
c.notice(nick, "Users: " + ", ".join(users))
|
|
opers = sorted(chobj.opers())
|
|
c.notice(nick, "Opers: " + ", ".join(opers))
|
|
voiced = sorted(chobj.voiced())
|
|
c.notice(nick, "Voiced: " + ", ".join(voiced))
|
|
elif cmd == "starch":
|
|
c.notice(nick, "starch: it exists.")
|
|
else:
|
|
c.notice(nick, "Not understood: " + cmd)
|
|
|
|
if __name__ == "__main__":
|
|
s = conf["server"]
|
|
bot = Bot([(s["host"], s["port"])], conf["nickname"], "#a")
|
|
|
|
bot.start() |