First draft of generate command
This commit is contained in:
parent
672e0ec5d8
commit
dd9a4b68a7
|
@ -0,0 +1,37 @@
|
|||
import plugin, utils, tracery, titlecase
|
||||
from tracery.modifiers import base_english
|
||||
|
||||
NAME = re.compile(r'\$([^\s]+)')
|
||||
SUB = r"#\1#"
|
||||
replace_name = lambda x: NAME.sub(SUB,x)
|
||||
|
||||
GENERATORS = [
|
||||
"book_fantasy",
|
||||
"book_horror",
|
||||
"book_hp",
|
||||
"book",
|
||||
"book_rom",
|
||||
"book_sf",
|
||||
"char",
|
||||
"idea",
|
||||
"land",
|
||||
"place"
|
||||
]
|
||||
GENERATOR_URL = "https://github.com/cwarwicker/discord-WriterBot/raw/master/assets/json/en/gen_{}.json"
|
||||
def get_generator(name):
|
||||
generator = utils.get(GENERATOR_URL.format(name))
|
||||
out = dict()
|
||||
out.update(generator["names"])
|
||||
out["origin"] = [replace_name(x) for x in generator["formats"]]
|
||||
ret = tracery.Grammar(out)
|
||||
ret.add_modifiers(base_english)
|
||||
return (lambda: ret.flatten("#origin#"))
|
||||
|
||||
GENERATORS = utils.LazyLoadedDict(get_generator)
|
||||
|
||||
@plugin.command("generate")
|
||||
def generate(bot,channel,nick,generator):
|
||||
res = GENERATORS[generator]()
|
||||
if generator!="idea":
|
||||
res = titlecase.titlecase(res)
|
||||
bot.say(channel,"{}: {}".format(nick,res))
|
12
utils.py
12
utils.py
|
@ -1,4 +1,4 @@
|
|||
import plugin, subprocess, requests
|
||||
import plugin, subprocess, requests, collections
|
||||
|
||||
def register_command_call(command,cli,use_args=False,verify_args=lambda args: args):
|
||||
@plugin.command(command)
|
||||
|
@ -12,3 +12,13 @@ def get(*args,**kwargs):
|
|||
r = requests.get(*args,**kwargs)
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
|
||||
class LazyLoadedDict(collections.UserDict):
|
||||
def __init__(self,func,initialdata=dict()):
|
||||
super().__init__(initialdata)
|
||||
self.func = func
|
||||
def __getitem__(self,key):
|
||||
if key not in self.data:
|
||||
self.data[key]=self.func(key)
|
||||
return self.data[key]
|
||||
return self.data[key]
|
||||
|
|
Loading…
Reference in New Issue