format strings and server:port connections

This commit is contained in:
Ben Harris committed 2018-10-09 02:50:11 -04:00
1 parent 58753c7234
commit 99cf1c5822
15 files changed
+241 -511

No files matched your search

+69 -148
View File
@@ -13,10 +13,6 @@ import subprocess
import time
import datetime
import formatter
import get_users
import mentions
import pretty_date
import inflect
from rhymesWith import getRhymes
from rhymesWith import rhymeZone
@@ -37,7 +33,7 @@ parser.add_option(
"-s",
"--server",
dest="server",
default="127.0.0.1",
default="127.0.0.1:6667",
help="the server to connect to",
metavar="SERVER",
)
@@ -63,20 +59,12 @@ parser.add_option(
p = inflect.engine()
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")
ircsock.send("JOIN " + chan + "\r\n")
def hello():
ircsock.send("PRIVMSG " + channel + " :Hello!\n")
util.sendmsg(ircsoc, channel, "Hello!")
def score_banter(channel, user, messageText):
@@ -109,42 +97,27 @@ def score_banter(channel, user, messageText):
msg = ""
if score > 100:
msg = (
"Truely "
+ random.choice(compliment).capitalize()
+ ", "
+ random.choice(names)
+ "! That was some #banter! You earned a "
+ str(score)
+ " for that!"
msg = "Truely {}, {}! That was some #banter! You earned a {} for that!".format(
random.choice(compliment).capitalize(), random.choice(names), score
)
elif score > 50:
msg = (
random.choice(compliment).capitalize()
+ " #banter! You get a "
+ str(score)
+ " from me!"
msg = "{} #banter! You get a {} from me!".format(
random.choice(compliment).capitalize(), score
)
elif score > 10:
msg = (
random.choice(["acceptible", "reasonable", "passable"]).capitalize()
+ " #banter. You get a "
+ str(score)
msg = "{} #banter. You get a {}".format(
random.choice(["acceptible", "reasonable", "passable"]).capitalize(), score
)
else:
msg = (
"That "
+ random.choice(
msg = "That {} #banter, {}. I'll give you a {}. Maybe try again?".format(
random.choice(
["was hardly", "was barely", "wasn't", "won't pass for", "was awful"]
)
+ " #banter"
+ random.choice([", lad", ", lah", ", boy", "", ""])
+ ". I'll give you a "
+ str(score)
+ ". Maybe try again?"
),
random.choice(["lad", "lah", "boy", ""]),
score,
)
ircsock.send("PRIVMSG " + channel + " :" + msg + "\n")
util.sendmsg(ircsock, channel, msg)
def get_new_banter(channel, user):
@@ -174,14 +147,8 @@ def get_new_banter(channel, user):
word = word[:end] + "t" + word[end:]
else: # replace the letter with 'b'
word = word[:end] + "t" + word[end + 1 :]
ircsock.send(
"PRIVMSG "
+ channel
+ " :"
+ user
+ ": Here, why don't you try '"
+ word
+ "'?\n"
util.sendmsg(
ircsock, channel, "{} : Here, why don't you try '{}'?".format(user, word)
)
@@ -194,26 +161,18 @@ def get_rhymes(channel, user, text):
word = random.choice(words.readlines()).strip("\n")
rhymes = rhymeZone(word)
if len(rhymes) == 0:
ircsock.send(
"PRIVMSG "
+ channel
+ " :"
+ user
+ ": Couldn't find anything that rhymes with '"
+ word
+ "' :(\n"
util.sendmsg(
ircsock,
channel,
"{}: Couldn't find anything that rhymes with '{}' :(".format(user, word),
)
else:
ircsock.send(
"PRIVMSG "
+ channel
+ " :"
+ user
+ ": Here, these words rhyme with '"
+ word
+ "': "
+ ", ".join(rhymes)
+ "\n"
util.sendmsg(
ircsock,
channel,
"{}: Here, these words rhyme with '{}': {}".format(
user, word, ", ".join(rhymes)
),
)
@@ -224,68 +183,48 @@ def define_word(channel, user, text):
word = text.split(" ")[1]
defs = defWord(word)
if len(defs) == 0:
ircsock.send(
"PRIVMSG "
+ channel
+ " :"
+ user
+ ": Couldn't find the definition of '"
+ word
+ "' :(\n"
util.sendmsg(
ircsock,
channel,
"{}: Couldn't find the definition of '{}' :(".format(user, word),
)
elif isinstance(defs, list):
for entry in defs:
ircsock.send(
"PRIVMSG "
+ channel
+ " :"
+ user
+ ": Define '"
+ word
+ "'"
+ entry[0:400]
+ "\n"
util.sendmsg(
ircsock, channel, "{} : Define '{}' {}".format(user, word, entry[0:400])
)
else:
ircsock.send(
"PRIVMSG "
+ channel
+ " :"
+ user
+ ": Define '"
+ word
+ "'"
+ defs[0:400]
+ "\n"
util.sendmsg(
ircsock, channel, "{} : Define '{}' {}".format(user, word, defs[0:400])
)
def make_rainbow(channel, user, text):
rbword = makeRainbow(text[9:])
ircsock.send("PRIVMSG " + channel + " :" + rbword + "\n")
util.sendmsg(ircsock, channel, rbword)
def get_welch(channel):
ircsock.send("PRIVMSG " + channel + " :" + welch.get_thing()[0:400] + "\n")
util.sendmsg(ircsock, channel, welch.get_thing()[0:400])
def get_evil(channel):
evilThing = evil.get_thing()
for line in [evilThing[i : i + 400] for i in range(0, len(evilThing), 400)]:
ircsock.send("PRIVMSG " + channel + " :" + line + "\n")
util.sendmsg(ircsock, channel, line)
def get_tumble(url, channel):
tumble = tumblr.tumble(url)
for line in [tumble[i : i + 400] for i in range(0, len(tumble), 400)]:
ircsock.send("PRIVMSG " + channel + " :" + line + "\n")
util.sendmsg(ircsock, channel, line)
def get_xkcd(channel, text):
links = xkcdApropos.xkcd(text[6:])
joined_links = ", ".join(links)
for line in [joined_links[i : i + 400] for i in range(0, len(joined_links), 400)]:
ircsock.send("PRIVMSG " + channel + " :" + line + "\n")
util.sendmsg(ircsock, channel, line)
# res = xkcdApropos.xkcd(text[6:])
# ircsock.send("PRIVMSG " + channel + " :" + res + "\n")
@@ -293,12 +232,8 @@ def get_xkcd(channel, text):
def get_wphilosophy(channel, text):
steps = wikiphilosophy.get_philosophy_lower(text[17:])
if not steps:
ircsock.send(
"PRIVMSG "
+ channel
+ " :Couldn't find a wikipedia entry for "
+ text
+ "\n"
util.sendmsg(
ircsock, channel, "Couldn't find a wikipedia entry for {}".format(text)
)
else:
joined_steps = " > ".join(steps)
@@ -307,24 +242,24 @@ def get_wphilosophy(channel, text):
for line in [
joined_steps[i : i + 400] for i in range(0, len(joined_steps), 400)
]:
ircsock.send("PRIVMSG " + channel + " :" + line + "\n")
util.sendmsg(ircsock, channel, line)
def figlet(channel, text):
if not text:
ircsock.send("PRIVMSG " + channel + " :No text given. :(\n")
util.sendmsg(ircsock, channel, "No text given. :(")
else:
lines = subprocess.Popen(
["figlet", "-w140"] + text.split(" "), shell=False, stdout=subprocess.PIPE
).stdout.read()
for line in lines.split("\n"):
ircsock.send("PRIVMSG " + channel + " :" + line + "\n")
util.sendmsg(ircsock, channel, line)
time.sleep(0.4) # to avoid channel throttle due to spamming
def toilet(channel, text):
if not text:
ircsock.send("PRIVMSG " + channel + " :No text given. :(\n")
util.sendmsg(ircsock, channel, "No text given. :(")
else:
lines = subprocess.Popen(
["toilet", "-w140", "--irc"] + text.split(" "),
@@ -332,24 +267,24 @@ def toilet(channel, text):
stdout=subprocess.PIPE,
).stdout.read()
for line in lines.split("\n"):
ircsock.send("PRIVMSG " + channel + " :" + line + "\n")
util.sendmsg(ircsock, channel, line)
time.sleep(0.4) # to avoid channel throttle due to spamming
def get_acronym(channel, text):
if not text:
ircsock.send("PRIVMSG " + channel + " :No text given :(\n")
util.sendmsg(ircsock, channel, "No text given :(")
else:
defs = acronymFinder.get_acros(text, True, True)
for d in defs[0:5]: # only the first five. they are already sorted by 'score'
ircsock.send("PRIVMSG " + channel + " :" + d.encode("utf-8") + "\n")
util.sendmsg(ircsock, channel, d.encode("utf-8"))
if len(defs) > 5:
ircsock.send("PRIVMSG " + channel + " :" + defs[-1] + "\n")
util.sendmsg(ircsock, channel, defs[-1])
def get_whosaid(channel, text):
if not text:
ircsock.send("PRIVMSG " + channel + " :No text given :(\n")
util.sendmsg(ircsock, channel, " :No text given :(")
else:
result = whoSaid(text)
date = datetime.date.fromtimestamp(result["timecutoff"])
@@ -368,46 +303,37 @@ def get_whosaid(channel, text):
result["data"][1][0],
result["data"][1][1],
)
ircsock.send("PRIVMSG " + channel + " :" + msg + ".\n")
util.sendmsg(ircsock, channel, msg)
def get_notice(user, channel):
ircsock.send("CNOTICE " + user + " " + channel + " :Notice me sempai!\n")
ircsock.send("CNOTICE " + user + " " + channel + " :Notice me senpai!\r\n")
def get_water(user, channel, msg, botnick):
if msg.find(botnick) == 0:
ircsock.send("PRIVMSG " + channel + " :Fight me, " + user + "!\n")
util.sendmsg(ircsock, channel, "Fight me, {}!".format(user))
def mug_off(channel):
ircsock.send("PRIVMSG " + channel + " :u want some of this, m8?\n")
util.sendmsg(ircsock, channel, "u want some of this, m8?")
def rollcall(channel):
ircsock.send(
"PRIVMSG "
+ channel
+ " :U wot m8? I score all the top drawer #banter and #bantz on this channel! \
Find new top-shelf banter with !newbanter, !rhymes, and !define. \
Look up things with !acronym and !whosaid. \
Make your chatter #legend with !rainbow, !toilet, and !figlet. \
Find interesting things with !xkcd and !wiki-philosophy. \
Get jokes with !welch and !evil\n"
util.sendmsg(
ircsock,
channel,
"""
U wot m8? I score all the top drawer #banter and #bantz on this channel!
Find new top-shelf banter with !newbanter, !rhymes, and !define.
Look up things with !acronym and !whosaid.
Make your chatter #legend with !rainbow, !toilet, and !figlet.
Find interesting things with !xkcd and !wiki-philosophy.
Get jokes with !welch and !evil
""",
)
def connect(server, channel, botnick):
ircsock.connect((server, 6667))
ircsock.send(
"USER " + botnick + " " + botnick + " " + botnick + " :krowbar\n"
) # user authentication
ircsock.send("NICK " + botnick + "\n")
joinchan(channel)
joinchan("#bots")
def get_user_from_message(msg):
try:
i1 = msg.index(":") + 1
@@ -433,12 +359,7 @@ def listen(botnick):
# print formatted
split = formatted.split("\t")
# time = split[0]
user = split[1]
command = split[2]
channel = split[3]
messageText = split[4]
time, user, command, channel, messageText = formatted.split("\t")
if ircmsg.find("#banter") != -1 or ircmsg.find("#bantz") != -1:
score_banter(channel, user, messageText)
@@ -497,12 +418,12 @@ def listen(botnick):
mug_off(channel)
if ircmsg[:4] == "PING":
ping(ircmsg.split(" ")[1])
util.ping(ircsock, msg)
sys.stdout.flush()
time.sleep(1)
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connect(options.server, options.channel, options.nick)
util.connect(ircsock, options)
listen(options.nick)