Skip to content

Instantly share code, notes, and snippets.

@jixunmoe
Last active September 28, 2015 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jixunmoe/368137900ff8072e41fb to your computer and use it in GitHub Desktop.
Save jixunmoe/368137900ff8072e41fb to your computer and use it in GitHub Desktop.
An IRC bot that does nothing but a simple 1A2B game.
import socket
import random
import sys
import re
server = "irc.freenode.net" # Server
channel = "#cs-york-dev2" # Channel
botnick = "game_1A2B" # Your bots nick
# Convert string to bytes
def _buff(_str):
return bytes(_str, 'UTF-8')
# Join an IRC channel
def bot_join(x):
ircsock.send(_buff("JOIN %s \n" % x))
# In case of multi channel
game_dict = {}
# Generate 4 digits non-repeat
def gen_digits(count):
return ''.join(str(x) for x in (random.sample(range(0,9), count)))
# Check if the 4 digits are unique
def is_unique(text):
for i in range(0, 4):
if text.find(text[i]) != i:
return False
pass
return True
# Rank guess and result
def _rank(a, b):
r = [0, 0]
for i in range(0, 4):
if a[i] == b[i]:
r[0] += 1
elif b.find(a[i]) != -1:
r[1] += 1
pass
return r
# Reply to where it come from
def bot_reply(m, content):
# _msg = "PRIVMSG " + m.group(4) + m.group(5) + " :" + content + "\n"
_msg = "NOTICE %s%s :%s\n" % (m.group(4), m.group(5), content)
print('Issue a %s' % _msg)
ircsock.send(_buff(_msg))
# Start a new game
def bot_newgame(m):
target = m.group(5)
if target in game_dict:
del game_dict[target]
game_dict[target] = gen_digits(4)
print('The answer is %s' % game_dict[target])
bot_reply(m, '1A2B has started! reply with a non-repeat 4 digit number(e.g. 1234) to play.')
def bot_do_game(m):
guess = m.group(7)
target = m.group(5)
whom = m.group(1)
if not is_unique(guess):
# Ignore it
return
r = _rank(guess, game_dict[target])
if r[0] == 4:
# Guess correct, end game
del game_dict[target]
bot_reply(m, 'You have guessed correctly [%s]! Reply "1A2B" to start a new game.' % (guess))
else:
bot_reply(m, 'You guessed %s -> %dA%dB' % (guess, r[0], r[1]))
pass
r1A2B = re.compile(r'\d{4}')
# :Nick!~User@ip PRIVMSG #CHAN :Message
rCommand = re.compile(r':(\w+?)!~(\w+?)@([\S]+) PRIVMSG (#?)([\w-]+) :(!?)(.+)')
def proc_msg(msg):
m = rCommand.match(msg)
if m is not None:
# For now, only work with group chats.
if m.group(4) != '':
if m.group(6) == '!':
# This is bad but it works
if m.group(7) == 'exit':
sys.exit()
else:
# Game 1A2B
target = m.group(5)
if target not in game_dict:
if m.group(7) == '1A2B':
# then init it
bot_newgame(m)
elif r1A2B.match(m.group(7)):
bot_do_game(m)
pass # the ball
pass # out
pass # word
pass # idk
# Reply to prevent timeout
if msg.find("PING :") == 0:
ircsock.send("PONG :pingis\n")
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Login
ircsock.connect((server, 6667))
ircsock.send(_buff("USER %s %s_1 %s_2 :Jixun \nNICK %s\n" % (botnick, botnick, botnick, botnick)))
# Join
bot_join(channel)
while 1: # Be careful with these! it might send you to an infinite loop
ircmsg = ircsock.recv(2048) # receive data from the server
ircmsg = ircmsg.decode('UTF-8').strip('\n\r') # removing any unnecessary linebreaks.
for line in ircmsg.split('\n'):
proc_msg(line)
print(ircmsg) # Here we print what's coming from the server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment