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

109 lines
3.1 KiB
Python
Executable File

# PyBot v1.0 - Based on code by chevloschelios
# (http://ubuntuforums.org/showthread.php?t=1493702)
# Modified by Ikewise with help from Adrian Malacoda
import socket
server = '127.0.0.1'
port = 6667
channel = '#lobby'
botnick = 'PyBot'
def say(target, msg):
irc.send(bytes('PRIVMSG '+ target +' :'+ msg +'\r\n', 'UTF-8'))
# Differentiate between channel and private messages
if target[0] != '#':
print('DIRECT (' + target + '): <' + botnick + '> ' + msg)
else:
print(target + ': <' + botnick + '> ' + msg)
def join(chan):
irc.send(bytes('JOIN :'+ chan +'\r\n', 'UTF-8'))
print('* Now talking in ' + chan)
say(channel, 'Hello '+ chan +'!')
def part(chan):
irc.send(bytes('PART '+ 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))
data = data.lstrip('b\':')
data = data.lstrip('b":')
data = data[0:len(data)-5]
#print(data)
if data == '': # Detect connection loss
break
if data.find('PING') != -1: # Respond to server pings
irc.send(bytes('PONG ' + data.split() [ 1 ] + '\r\n', 'UTF-8'))
# Auto-Rejoin when kicked
if data.find('KICK #') == data.find(' ') + 1:
source = data[data.find('#'):len(data)]
source = source[0:source.find(' ')]
irc.send(bytes('JOIN '+ source +'\r\n', 'UTF-8'))
# Check for standard messages
if data.find('PRIVMSG') == data.find(' ') + 1:
sender = data[0:(data.find('!'))] # Set name of sender
source = data[(data.find('PRIVMSG')+8):(data.find(':')-1)] # The channel the data came from (or you, if PM)
msgdata = data[data.find(':') + 1:len(data)] # Set actual message content
lowdata = msgdata.lower() # Case-sensitivity is an asshole
# Differentiate between channel and private messages
if source[0] != '#':
source = sender
print('DIRECT (' + sender + '): <' + sender + '> ' + msgdata)
else:
print(source + ': <' + sender + '> ' + msgdata)
# PyBot main commands begin here
# Use "== 0:" for line start, "!= -1:" for anywhere
if lowdata.find('!quit') == 0:
logoff('Shutdown command from ' + sender)
if lowdata.find('chk') == 0:
say(source, sender + ': ack')
if lowdata.find('hi '+ botnick.lower()) == 0:
say(source, 'Hello, ' + sender + '!')
if lowdata.find('hello '+ botnick.lower()) == 0:
say(source, 'Greetings, ' + sender + '!')
if lowdata.find('slaps '+ botnick.lower()) != -1:
say(source, 'This is the Trout Protection Agency. Please put the Trout Down and walk away with your hands in the air.')
# End of program
print()
print('Connection terminated.')