-
-
Save rubenwardy/8863c20a75e4faad3562 to your computer and use it in GitHub Desktop.
IRC bot for Python Noob Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Based on http://linuxclues.blogspot.co.uk/2011/03/simple-python-irc-bot.html | |
# Requires https://gist.github.com/rubenwardy/4cb29fedb7952e5a4cdf | |
import socket | |
import re | |
import os | |
from noob_gen import * | |
import time | |
class Bot(): | |
# Change into values that suit you. | |
NETWORK = 'irc.freenode.org' | |
PORT = 6667 | |
nick = 'rubenBOT' | |
channel = '#rubenBOT' | |
owner = 'rubenwardy' | |
def send(self, input): | |
self.irc.send(bytes(input, 'UTF-8')) | |
def __init__(self): | |
self.quit_bot = False | |
self.command_list = [] # initialy we have not received any command. | |
self.data_buffer = '' # no data received yet. | |
self.rexp_general = re.compile(r'^(:[^ ]+)?[ ]*([^ ]+)[ ]+([^ ].*)?$') | |
def connect_to_server(self): | |
self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self.irc.connect((self.NETWORK, self.PORT)) | |
self.send('NICK ' + self.nick + '\r\n') | |
self.send('USER ' + self.nick + ' Ztbot Ztbot :Python IRC Bot\r\n') | |
self.send('JOIN ' + self.channel + '\r\n') | |
def say_hello(self): | |
# Say hello! | |
self.send('PRIVMSG #rubenBOT :Bot connected to channel correctly, I hope.\r\n') | |
def parse_command(self, data): | |
p = self.rexp_general | |
r = p.search(data) | |
if r is None: | |
# command does not match. | |
print ("command does not match.") | |
return | |
g = r.groups() | |
print (g) | |
sender = g[0] | |
cmd = g[1] | |
params = g[2] | |
if cmd == 'PING': | |
self.send('PONG ' + params + '\r\n') | |
return | |
elif cmd == 'KICK': | |
self.send('JOIN ' + self.channel + '\r\n') | |
return | |
elif cmd == 'PRIVMSG': | |
quit_rex = r'%s:[ ]+(quit|QUIT)' % self.nick | |
if re.search(quit_rex, params) is not None: | |
s_expr = 'PRIVMSG %s :OK, I am quitting now!\r\n' % self.owner | |
self.send(s_expr) | |
self.send('QUIT\r\n') | |
self.quit_bot = True | |
return | |
hi_rex = r'(hi|hello|hola)[ ]+%s' % self.nick | |
if re.search(hi_rex, params) is not None: | |
msg = 'Hi, I am a pythonic irc bot!\r\n' | |
self.send('PRIVMSG %s :%s\r\n' % (self.channel, msg)) | |
return | |
def get_command(self): | |
if len(self.command_list) > 0: | |
result = self.command_list.pop(0) | |
return result | |
# There is no command available, we read more bytes. | |
chunk = self.irc.recv(4096) | |
self.data_buffer = ''.join([self.data_buffer, chunk]) | |
self.command_list = self.data_buffer.split('\r\n') | |
self.data_buffer = self.command_list.pop() | |
if len(self.command_list) == 0: | |
return None | |
result = self.command_list.pop(0) | |
return result | |
def main(self): | |
self.connect_to_server() | |
self.say_hello() | |
time.sleep(20) | |
while (1): | |
time.sleep(5) | |
result = getrand(cons) + " " + gensentence() | |
result += " " + getrand(cons) + " " + gensentence() | |
result += " " + getrand(cons) + " " + gensentence() | |
self.send("PRIVMSG #rubenBOT :"+result+"\r\n") | |
if __name__ == '__main__': | |
myBot = Bot() | |
myBot.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment