made slap use the new TextGenerator class

This commit is contained in:
Luke Rogers 2013-09-12 22:46:43 +12:00
parent 15e7825125
commit 2be0c46b89
6 changed files with 93 additions and 75 deletions

View file

@ -5,14 +5,6 @@ with open("plugins/data/larts.txt") as f:
larts = [line.strip() for line in f.readlines()
if not line.startswith("//")]
with open("plugins/data/slaps.txt") as f:
slaps = [line.strip() for line in f.readlines()
if not line.startswith("//")]
with open("plugins/data/slap_items.txt") as f:
items = [line.strip() for line in f.readlines()
if not line.startswith("//")]
with open("plugins/data/kills.txt") as f:
kills = [line.strip() for line in f.readlines()
if not line.startswith("//")]
@ -25,27 +17,6 @@ with open("plugins/data/flirts.txt") as f:
flirts = [line.strip() for line in f.readlines()
if not line.startswith("//")]
@hook.command
def slap(inp, me=None, nick=None, conn=None, notice=None):
"""slap <user> -- Makes the bot slap <user>."""
target = inp.strip()
if " " in target:
notice("Invalid username!")
return
# if the user is trying to make the bot slap itself, slap them
if target.lower() == conn.nick.lower() or target.lower() == "itself":
target = nick
values = {"item": random.choice(items), "user": target}
phrase = random.choice(slaps)
# act out the message
me(phrase.format(**values))
@hook.command
def lart(inp, me=None, nick=None, conn=None, notice=None):
"""lart <user> -- LARTs <user>."""

View file

@ -1,29 +0,0 @@
cast iron skillet
large trout
baseball bat
wooden cane
nail
printer
shovel
pair of trousers
CRT monitor
diamond sword
baguette
physics textbook
television
mau5head
five ton truck
roll of duct tape
book
laptop
old television
sack of rocks
rainbow trout
cobblestone block
lava bucket
rubber chicken
spiked bat
gold block
fire extinguisher
heavy rock
chunk of dirt

51
plugins/data/slaps.json Normal file
View file

@ -0,0 +1,51 @@
{
"templates":[
"slaps {user} with a {item}.",
"slaps {user} around a bit with a {item}.",
"throws a {item} at {user}.",
"chucks a few {item}s at {user}.",
"grabs a {item} and throws it in {user}'s face.",
"launches a {item} in {user}'s general direction.",
"sits on {user}'s face while slamming a {item} into their crotch.",
"starts slapping {user} silly with a {item}.",
"holds {user} down and repeatedly whacks them with a {item}.",
"prods {user} with a flaming {item}.",
"picks up a {item} and whacks {user} with it.",
"ties {user} to a chair and throws a {item} at them.",
"hits {user} on the head with a {item}.",
"ties {user} to a pole and whips them with a {item}."
],
"parts":{
"item":[
"cast iron skillet",
"large trout",
"baseball bat",
"wooden cane",
"nail",
"printer",
"shovel",
"pair of trousers",
"CRT monitor",
"diamond sword",
"baguette",
"physics textbook",
"television",
"mau5head",
"five ton truck",
"roll of duct tape",
"book",
"laptop",
"old television",
"sack of rocks",
"rainbow trout",
"cobblestone block",
"lava bucket",
"rubber chicken",
"spiked bat",
"gold block",
"fire extinguisher",
"heavy rock",
"chunk of dirt"
]
}
}

View file

@ -1,14 +0,0 @@
slaps {user} with a {item}.
slaps {user} around a bit with a {item}.
throws a {item} at {user}.
chucks a few {item}s at {user}.
grabs a {item} and throws it in {user}'s face.
launches a {item} in {user}'s general direction.
sits on {user}'s face while slamming a {item} into their crotch.
starts slapping {user} silly with a {item}.
holds {user} down and repeatedly whacks them with a {item}.
prods {user} with a flaming {item}.
picks up a {item} and whacks {user} with it.
ties {user} to a chair and throws a {item} at them.
hits {user} on the head with a {item}.
ties {user} to a pole and whips them with a {item}.

32
plugins/slap.py Normal file
View file

@ -0,0 +1,32 @@
from util import hook, text, textgen
import json, os
def get_generator(_json, variables):
data = json.loads(_json)
return textgen.TextGenerator(data["templates"],
data["parts"], variables=variables)
@hook.command
def slap(inp, me=None, nick=None, conn=None, notice=None):
"""slap <user> -- Makes the bot slap <user>."""
target = inp.strip()
if " " in target:
notice("Invalid username!")
return
# if the user is trying to make the bot slap itself, slap them
if target.lower() == conn.nick.lower() or target.lower() == "itself":
target = nick
variables = {
"user": target
}
with open("plugins/data/slaps.json") as f:
generator = get_generator(f.read(), variables)
# act out the message
me(generator.generate_string())

View file

@ -5,19 +5,26 @@ TEMPLATE_RE = re.compile(r"\{(.+?)\}")
class TextGenerator(object):
def __init__(self, name, templates, default_templates, parts, variables=None):
self.name = name
def __init__(self, templates, parts, default_templates=None, variables=None):
self.templates = templates
self.default_templates = default_templates
self.parts = parts
self.variables = variables
print self.variables
def generate_string(self, template=None):
"""
Generates one string using the specified templates.
If no templates are specified, use a random template from the default_templates list.
"""
text = self.templates[template or random.choice(self.default_templates)]
if self.default_templates:
text = self.templates[template or random.choice(self.default_templates)]
else:
text = random.choice(self.templates)
if self.variables:
for key, value in self.variables.items():
text = text.replace("{%s}" % key, value)
# get a list of all text parts we need
required_parts = TEMPLATE_RE.findall(text)