forked from lexande/trainbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrainbotrun.py
More file actions
87 lines (74 loc) · 2.79 KB
/
trainbotrun.py
File metadata and controls
87 lines (74 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# by lexande - BSD licensed
#!/usr/bin/env python
import irc.client
import irc.bot
import ib3.auth
import ib3.connection
import sys
import time
from importlib import reload
from threading import Thread
import trainbotbrain
from trainbotpass import password, ownernick, botnicks
channel = sys.argv[1]
irc.client.ServerConnection.buffer_class.errors = 'replace'
class reloader(ib3.auth.SASL, ib3.connection.SSL, irc.bot.SingleServerIRCBot):
def __init__(self, serverspec, nick, bots):
irc.bot.SingleServerIRCBot.__init__(self, serverspec, nick, nick)
ib3.auth.SASL.__init__(self, serverspec, nick, nick, password, [channel])
self.nick = nick
self.bots = bots
self.botclassname = "tra"+str(botnicks.index(nick)+1)+"nbot"
self.brain = getattr(trainbotbrain, self.botclassname)()
self.broken = False
def on_welcome(self, c, event):
print(c.nickname , "is now online")
def on_pubmsg(self, c, event):
if not self.broken:
try:
self.brain.on_pubmsg(c, event)
except Exception as thisbroke:
self.errorhandle(thisbroke, c)
def on_privmsg(self, c, event):
if event.source.nick == ownernick and event.arguments[0] == "lern":
try:
reload(trainbotbrain)
self.brain = getattr(trainbotbrain, self.botclassname)()
self.broken = False
except Exception as thisbroke:
self.errorhandle(thisbroke, c)
elif event.source.nick == ownernick and event.arguments[0].split(' ')[0] == "reset":
nicktoreset = event.arguments[0].split(' ')[1]
self.bots[nicktoreset].stop_bot()
newbot = run_trainbot("irc.libera.chat", 6697, nicktoreset, bots)
self.bots[nicktoreset] = newbot
newbot.start()
elif not self.broken:
try:
self.brain.on_privmsg(c, event)
except Exception as thisbroke:
self.errorhandle(thisbroke, c)
def errorhandle(self, thisbroke, c):
c.privmsg(ownernick, "halp")
print ("%s had an error of type %s: %s" % (self.nick, type(thisbroke), thisbroke))
self.broken = True
class run_trainbot(Thread):
def __init__(self, server, port, nick, bots):
self.serverspec = [(server, port)]
self.nick = nick
self.bots = bots
Thread.__init__(self)
def run(self):
self.bot = reloader(self.serverspec, self.nick, self.bots)
self.bot.start()
def stop_bot(self):
self.bot.disconnect()
self.bot.connection.close()
bots = {}
for nick in botnicks:
bot = run_trainbot("irc.libera.chat", 6697, nick, bots)
bot.daemon = True
bots[nick] = bot
bot.start()
while True:
time.sleep(1000)