This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/namegen.py

61 lines
1.7 KiB
Python
Raw Normal View History

# Plugin by Lukeroge
from util import hook, text, textgen
2013-11-12 07:06:06 +01:00
import json
import os
2013-09-04 12:30:04 +02:00
2012-10-11 06:47:27 +02:00
GEN_DIR = "./plugins/data/name_files/"
2012-10-11 22:35:05 +02:00
2012-10-11 06:47:27 +02:00
def get_generator(_json):
data = json.loads(_json)
return textgen.TextGenerator(data["templates"],
2013-11-12 07:06:06 +01:00
data["parts"], default_templates=data["default_templates"])
2011-11-22 02:36:12 +01:00
2012-04-02 18:17:55 +02:00
2012-04-23 11:38:21 +02:00
@hook.command(autohelp=False)
def namegen(inp, notice=None):
"namegen [generator] -- Generates some names using the chosen generator. " \
"'namegen list' will display a list of all generators."
2011-11-22 02:36:12 +01:00
2012-10-11 22:35:05 +02:00
# clean up the input
inp = inp.strip().lower()
2012-10-11 06:47:27 +02:00
# get a list of available name generators
files = os.listdir(GEN_DIR)
all_modules = []
for i in files:
2012-10-11 22:35:05 +02:00
if os.path.splitext(i)[1] == ".json":
all_modules.append(os.path.splitext(i)[0])
all_modules.sort()
2011-11-22 02:36:12 +01:00
2012-10-11 06:47:27 +02:00
# command to return a list of all available generators
2012-10-11 22:35:05 +02:00
if inp == "list":
2012-10-11 06:47:27 +02:00
message = "Available generators: "
message += text.get_text_list(all_modules, 'and')
notice(message)
2011-11-22 02:45:30 +01:00
return
2012-02-02 14:05:11 +01:00
2012-04-23 11:38:21 +02:00
if inp:
2012-10-11 22:35:05 +02:00
selected_module = inp.split()[0]
2012-04-23 11:38:21 +02:00
else:
# make some generic fantasy names
2012-10-11 22:35:05 +02:00
selected_module = "fantasy"
2012-10-11 06:47:27 +02:00
# check if the selected module is valid
if not selected_module in all_modules:
return "Invalid name generator :("
2011-11-22 02:36:12 +01:00
2012-10-11 06:47:27 +02:00
# load the name generator
with open(os.path.join(GEN_DIR, "{}.json".format(selected_module))) as f:
2012-10-11 22:35:05 +02:00
try:
generator = get_generator(f.read())
except ValueError as error:
return "Unable to read name file: {}".format(error)
2011-11-22 02:36:12 +01:00
2012-10-11 06:47:27 +02:00
# time to generate some names
name_list = generator.generate_strings(10)
2012-04-23 11:38:21 +02:00
# and finally return the final message :D
return "Some names to ponder: {}.".format(text.get_text_list(name_list, 'and'))