This commit is contained in:
Ben Harris 2018-10-09 02:50:06 -04:00
parent e5c499d86e
commit a8a5de36f2
Signed by untrusted user: ben
GPG Key ID: 4E0AF802FFF7960C
4 changed files with 168 additions and 30 deletions

View File

@ -28,6 +28,7 @@ import tumblr
import xkcdApropos
import wikiphilosophy
import acronymFinder
import util
from whosaid import whoSaid
parser = OptionParser()
@ -266,7 +267,7 @@ def listen(botnick):
if ircmsg[:4] == "PING":
ping(ircmsg.split(" ")[1])
formatted = formatter.format_message(ircmsg)
formatted = util.format_message(ircmsg)
if "" == formatted:
continue

View File

@ -12,7 +12,7 @@ import time
import re
import operator
import msgformatter
from .. import util
import madlib
class State:
@ -215,7 +215,7 @@ def listen(botnick):
if ircmsg[:4] == "PING":
ping(ircmsg.split(" ")[1])
formatted = msgformatter.format_message(ircmsg)
formatted = util.format_message(ircmsg)
if "" == formatted:
continue
# print formatted

View File

@ -17,6 +17,7 @@ import mentions
import pretty_date
import inflect
import puzzle
import util
parser = OptionParser()
@ -36,23 +37,11 @@ JACKPOT_FILE = "tildejackpot.txt"
JACKPOT_MIN = 3
DEBUG = False
def ping(pong):
ircsock.send("PONG {}\n".format(pong))
def sendmsg(chan , msg):
ircsock.send("PRIVMSG "+ chan +" :"+ msg +"\n")
def joinchan(chan):
ircsock.send("JOIN "+ chan +"\n")
def hello():
ircsock.send("PRIVMSG "+ channel +" :Hello!\n")
util.sendmsg(ircsock, channel, "Hello!")
def too_recent(time1, time2):
if(int(time1) - int(time2) < 60*60):
return True
else:
return False
return int(time1) - int(time2) < 60*60
def get_positive():
return random.choice(['Yes', 'Yep', 'Yeppers', 'Correct','You got it', 'Yeah', 'Right on', 'Uh-huh', 'Positive', 'Totally right', 'Close enough', 'That\'s it'])
@ -98,7 +87,7 @@ def get_prize(name, isHuman, bonus=0):
def show_jackpot(channel):
with open(JACKPOT_FILE, "r") as jackpotfile:
jackpot = int(jackpotfile.readline().strip("\n"))
ircsock.send("PRiVMSG " + channel + " :The jackpot is currently " + p.number_to_words(jackpot) + " tildes!\n")
util.sendmsg(ircsock, channel, "The jackpot is currently {} tildes!".format(p.number_to_words(jackpot)))
def give_tilde(channel, user, name, time, human, bonus=0):
found = False
@ -111,15 +100,15 @@ def give_tilde(channel, user, name, time, human, bonus=0):
if(person[0] == user):
found = True
if(too_recent(time, person[2]) and not DEBUG):
ircsock.send("PRIVMSG " + channel + " :" + name + " asked for a tilde too recently and " + get_bad_thing() + ". Try again later.\n")
util.sendmsg(ircsock, channel, "{} asked for a tilde too recently and {}. Try again later.".format(name, get_bad_thing()))
else:
prize = get_prize(name, human, bonus)
score = person[0] + "&^%" + str(int(person[1]) + prize[0]) + "&^%" + time + "\n"
ircsock.send("PRIVMSG " + channel + " :" + prize[1] + "\n")
util.sendmsg(ircsock, channel, prize[1])
scorefile.write(score)
if(not found):
prize = get_prize(name, True, bonus)
ircsock.send("PRIVMSG " + channel + " :Welcome to the tilde game! Here's " + p.number_to_words(prize[0]+1) + " free tilde(s) to start you off.\n")
util.sendmsg(ircsock, channel, "Welcome to the tilde game! Here's {} free tilde(s) to start you off.".format(p.number_to_words(prize[0]+1)))
scorefile.write(user + "&^%" + str(prize[0]+1) + "&^%" + time + "\n")
def show_tildescore(channel, user, name):
@ -127,19 +116,19 @@ def show_tildescore(channel, user, name):
for idx,score in enumerate(scorefile):
person = score.strip("\n").split("&^%")
if(person[0] == user):
ircsock.send("PRIVMSG " + channel + " :" + name + " has " + p.number_to_words(person[1]) + " tildes!\n")
util.sendmsg(ircsock, channel, "{} has {} tildes!".format(name, p.number_to_words(person[1])))
return
#person has not played yet
ircsock.send("PRIVMSG " + channel + " :" + name + " has no tildes yet!\n")
util.sendmsg(ircsock, channel, "{} has no tildes yet!".format(name))
def challenge(channel, user, name, time):
if(channel != "#bots" and not DEBUG):
ircsock.send("PRIVMSG " + channel + " :" + name + " is a meanie and gets no tildes. **Tildebot now only gives out tildes in the #bots channel.**\n")
util.sendmsg(ircsock, channel, "{} is a meanie and gets no tildes. **Tildebot now only gives out tildes in the #bots channel.**".format(name))
return
global challenges;
challenge = puzzle.make_puzzle();
challenges[user] = challenge[1:]; #challenges[USER] = [ANSWER, BONUS]
ircsock.send("PRIVMSG " + channel + " :" + name + ": " + challenge[0] + "\n")
util.sendmsg(ircsock, channel, "{}: {}".format(name, challenge[1]))
def challenge_response(channel, user, name, time, msg):
global challenges
@ -155,12 +144,13 @@ def challenge_response(channel, user, name, time, msg):
def rollcall(channel):
ircsock.send("PRIVMSG "+ channel +" :tildebot reporting! I respond to !tilde !tildescore\n")
util.sendmsg(ircsock, channel, "tildebot reporting! I respond to !tilde !tildescore")
def connect(server, channel, botnick):
ircsock.connect((server, 6667))
ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :krowbar\n") # user authentication
ircsock.send("NICK "+ botnick +"\n")
ircsock.send("USER {0} {0} {0} :krowbar\r\n".format(botnick)) # user authentication
ircsock.send("NICK {}\r\n".format(botnick))
ircsock.send("MODE +B {}\r\n".format(botnick))
joinchan(channel)
if(not DEBUG):
@ -182,7 +172,7 @@ def listen():
msg = msg.strip('\n\r')
if msg[:4] == "PING":
ping(msg.split(" ")[1])
util.ping(ircsock, msg)
formatted = formatter.format_message(msg)
@ -214,7 +204,7 @@ def listen():
rollcall(channel)
if msg[:4] == "PING":
ping(msg.split(" ")[1])
util.ping(ircsock, msg)
sys.stdout.flush()
time.sleep(1)

147
Code/irc/util.py Normal file
View File

@ -0,0 +1,147 @@
import time
import re
def ping(pong):
ircsock.send("PONG {}\n".format(pong.split(" ")[1]))
def sendmsg(ircsock, chan , msg):
ircsock.send("PRIVMSG {} :{}\r\n".format(chan, msg))
def joinchan(ircsock, chan):
ircsock.send("JOIN {}\n".format(chan))
# integer number to english word conversion
# can be used for numbers as large as 999 vigintillion
# (vigintillion --> 10 to the power 60)
# tested with Python24 vegaseat 07dec2006
def int2word(n):
"""
convert an integer number n into a string of english words
"""
# break the number into groups of 3 digits using slicing
# each group representing hundred, thousand, million, billion, ...
n3 = []
r1 = ""
# create numeric string
ns = str(n)
for k in range(3, 33, 3):
r = ns[-k:]
q = len(ns) - k
# break if end of ns has been reached
if q < -2:
break
else:
if q >= 0:
n3.append(int(r[:3]))
elif q >= -1:
n3.append(int(r[:2]))
elif q >= -2:
n3.append(int(r[:1]))
r1 = r
#print n3 # test
# break each group of 3 digits into
# ones, tens/twenties, hundreds
# and form a string
nw = ""
for i, x in enumerate(n3):
b1 = x % 10
b2 = (x % 100)//10
b3 = (x % 1000)//100
#print b1, b2, b3 # test
if x == 0:
continue # skip
else:
t = thousands[i]
if b2 == 0:
nw = ones[b1] + t + nw
elif b2 == 1:
nw = tens[b1] + t + nw
elif b2 > 1:
nw = twenties[b2] + ones[b1] + t + nw
if b3 > 0:
nw = ones[b3] + "hundred " + nw
return nw
############# globals ################
ones = ["", "one ","two ","three ","four ", "five ",
"six ","seven ","eight ","nine "]
tens = ["ten ","eleven ","twelve ","thirteen ", "fourteen ",
"fifteen ","sixteen ","seventeen ","eighteen ","nineteen "]
twenties = ["","","twenty ","thirty ","forty ",
"fifty ","sixty ","seventy ","eighty ","ninety "]
thousands = ["","thousand ","million ", "billion ", "trillion ",
"quadrillion ", "quintillion ", "sextillion ", "septillion ","octillion ",
"nonillion ", "decillion ", "undecillion ", "duodecillion ", "tredecillion ",
"quattuordecillion ", "sexdecillion ", "septendecillion ", "octodecillion ",
"novemdecillion ", "vigintillion "]
def format_message(message):
pattern = r'^:.*\!~(.*)@.* (.*) (.*) :(.*)'
now = int(time.time())
matches = re.match(pattern, message)
if not matches:
return ''
nick = matches.group(1).strip()
command = matches.group(2).strip()
channel = matches.group(3).strip()
message = matches.group(4).strip()
return "%s\t%s\t%s\t%s\t%s" % (now, nick, command, channel, message)
def get_users():
# thanks, ~dan!
users = []
with open("/etc/passwd", "r") as f:
for line in f:
if "/bin/bash" in line:
u = line.split(":")[0] # Grab all text before first ':'
users.append(u)
return users
def pretty_date(time=False):
"""
Get a datetime object or a int() Epoch timestamp and return a
pretty string like 'an hour ago', 'Yesterday', '3 months ago',
'just now', etc
"""
from datetime import datetime
now = datetime.now()
if type(time) is int:
diff = now - datetime.fromtimestamp(time)
elif isinstance(time,datetime):
diff = now - time
elif not time:
diff = now - now
second_diff = diff.seconds
day_diff = diff.days
if day_diff < 0:
return ''
if day_diff == 0:
if second_diff < 10:
return "just now"
if second_diff < 60:
return str(second_diff) + " seconds ago"
if second_diff < 120:
return "a minute ago"
if second_diff < 3600:
return str(second_diff / 60) + " minutes ago"
if second_diff < 7200:
return "an hour ago"
if second_diff < 86400:
return str(second_diff / 3600) + " hours ago"
if day_diff == 1:
return "Yesterday"
if day_diff < 7:
return str(day_diff) + " days ago"
if day_diff < 31:
return str(day_diff / 7) + " weeks ago"
if day_diff < 365:
return str(day_diff / 30) + " months ago"
return str(day_diff / 365) + " years ago"