PyBot/rawbot.py
2021-10-15 23:18:03 -05:00

56 lines
1.3 KiB
Python
Executable File

import socket
server = '127.0.0.1'
port = 6667
channel = '#lobby'
botnick = 'RawBot'
def join(chan):
irc.send(bytes('JOIN :'+ chan +'\r\n', 'UTF-8'))
def logoff(msg):
irc.send(bytes('QUIT :'+ msg +'\r\n', 'UTF-8'))
# Program begins
print('Connecting to ' + server + ':' + str(port) + '...')
print()
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
irc.connect((server, port))
irc.send(bytes('NICK '+ botnick +'\n', 'UTF-8'))
irc.send(bytes('USER '+ botnick +' '+ botnick +' '+ botnick +' :Python IRC bot\r\n', 'UTF-8'))
join(channel)
while True:
data = str(irc.recv(4096))
if data == "b''": # Detect connection loss
break
data = data[2:len(data) - 5].split('\\r\\n:')
for i in data:
print(i)
if i.startswith('PING'): # Respond to server pings
irc.send(bytes('PONG ' + i.split() [ 1 ] + '\r\n', 'UTF-8'))
# Auto-Rejoin when kicked
if i.find('KICK #') == i.find(' ') + 1:
source = i[i.find('#'):len(i)]
source = source[0:source.find(' ')]
irc.send(bytes('JOIN '+ source +'\r\n', 'UTF-8'))
# Shutdown command
if i.lower().find('!quit') != -1:
logoff('Shutdown')
# End of program
print()
print('Connection terminated.')