Make bot autojoin channel and move nick and username to server config
This commit is contained in:
parent
3a2fb42538
commit
f4077cdd3e
2 changed files with 32 additions and 7 deletions
21
ircbot.py
21
ircbot.py
|
@ -9,7 +9,7 @@ from constants import logmessage_types, internal_submessage_types, controlmessag
|
|||
|
||||
import line_handling
|
||||
|
||||
Server = namedtuple('Server', ['host', 'port'])
|
||||
Server = namedtuple('Server', ['host', 'port', 'nick', 'realname', 'channels'])
|
||||
|
||||
# ServerThread(server, control_socket)
|
||||
# Creates a new server main loop thread
|
||||
|
@ -21,6 +21,12 @@ class ServerThread(threading.Thread):
|
|||
|
||||
self.server_socket_write_lock = threading.Lock()
|
||||
|
||||
self.nick = None
|
||||
self.nick_lock = threading.Lock()
|
||||
|
||||
self.channels = set()
|
||||
self.channels_lock = threading.Lock()
|
||||
|
||||
threading.Thread.__init__(self)
|
||||
|
||||
def send_line_raw(self, line):
|
||||
|
@ -105,9 +111,13 @@ class ServerThread(threading.Thread):
|
|||
self.api = line_handling.API(self)
|
||||
|
||||
# Run initialization
|
||||
# TODO: read nick/username/etc. from a config
|
||||
self.send_line_raw(b'NICK HynneFlip')
|
||||
self.send_line_raw(b'USER HynneFlip a a :HynneFlip IRC bot')
|
||||
self.send_line_raw(b'USER HynneFlip a a :' + self.server.realname.encode('utf-8'))
|
||||
|
||||
# Set up nick and channels
|
||||
self.api.nick(self.server.nick.encode('utf-8'))
|
||||
|
||||
for channel in self.server.channels:
|
||||
self.api.join(channel.encode('utf-8'))
|
||||
|
||||
# Run mainloop
|
||||
self.mainloop()
|
||||
|
@ -129,7 +139,8 @@ def spawn_serverthread(server):
|
|||
return (control_channel, logging_channel)
|
||||
|
||||
if __name__ == '__main__':
|
||||
control_channel, logging_channel = spawn_serverthread(Server('irc.freenode.net', 6667))
|
||||
server = Server(host = 'irc.freenode.net', port = 6667, nick = 'HynneFlip', realname = 'HynneFlip IRC bot', channels = ['##ingsoc'])
|
||||
control_channel, logging_channel = spawn_serverthread(server)
|
||||
|
||||
while True:
|
||||
cmd = input(': ')
|
||||
|
|
|
@ -9,14 +9,28 @@ class API:
|
|||
# We need to access the internal functions of the ServerThread object in order to send lines etc.
|
||||
self.serverthread_object = serverthread_object
|
||||
|
||||
def send(self, line):
|
||||
def send_raw(self, line):
|
||||
self.serverthread_object.send_line_raw(line)
|
||||
|
||||
def msg(self, recipient, message):
|
||||
"""Make sending PRIVMSGs much nicer"""
|
||||
line = 'PRIVMSG ' + recipient + ' :' + message
|
||||
line = b'PRIVMSG ' + recipient + b' :' + message
|
||||
self.serverthread_object.send_line_raw(line)
|
||||
|
||||
def nick(self, nick):
|
||||
# Send a NICK command and update the internal nick tracking state
|
||||
with self.serverthread_object.nick_lock:
|
||||
line = b'NICK ' + nick
|
||||
self.serverthread_object.send_line_raw(line)
|
||||
self.serverthread_object.nick = nick
|
||||
|
||||
def join(self, channel):
|
||||
# Send a JOIN command and update the internal channel tracking state
|
||||
with self.serverthread_object.channels_lock:
|
||||
line = b'JOIN ' + channel
|
||||
self.serverthread_object.send_line_raw(line)
|
||||
self.serverthread_object.channels.add(channel)
|
||||
|
||||
def error(self, message):
|
||||
self.serverthread_object.logging_channel.send((constants.logmessage_types.internal, constants.internal_submessage_types.error, message))
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue