I'm building an IRC bot in Python for fun. It is supposed to accept commands prefixed with '!' and act on them. The function below is used to parse commands received in an IRC message.
def parse_cmd(self, sender):
#Admin Commands
if sender == self.owner:
if self.command == 'quit':
send_bufr = "QUIT %s\r\n" %(self.channel)
self.sock.send(bytearray(send_bufr, "utf-8"))
self.sock.close()
sys.exit(1)
if self.command == 'hi':
print("Run: Hi")
send_bufr = "PRIVMSG %s :Hello %s" %(self.channel, sender)
print(send_bufr)
self.sock.send(bytearray(send_bufr, "utf-8"))
return
else:
return
else:
return
The exclamation points are parsed earlier and the function uses self.command as the command which is also set earlier. The following code is used to set the USER, NICK, and to join a channel and self.sock.send works fine here:
#Send NICK self.nick to set NICK
send_bufr = ("NICK %s \r\n") %(self.nick)
self.sock.send(bytearray(send_bufr, "utf-8"))
print("Set Nick to %s" %(self.nick))
#Send USER to set USER
send_bufr = ("USER %s 8 * :S0lder \r\n") %(self.nick)
self.sock.send(bytearray(send_bufr, "utf-8"))
print("Set USER to %s 8 :S0lder" %(self.nick))
#JOIN self.channel
send_bufr = ("JOIN %s \r\n") %(self.channel)
self.sock.send(bytearray(send_bufr, "utf-8"))
print("Joined %s" %(self.channel))
time.sleep(5)
However In the function earlier and any instances of self.sock.send() after the initial connection are not sent until the '!quit' command is given, at which point all of the messages that were supposed to be sent earlier are sent. Why is this? Am I misunderstanding the proper way to use sockets?
Edit: I'm connected to the same channel with an IRC client and the messages appear in the channel only after I give the !quit command.