modular-bot-framework-for-t.../modules/shuffle/index.py

108 lines
2.8 KiB
Python

import random
module_path = ""
def extract_separators(text):
sequence_buf = ""
seps = []
for i in text:
if i in ["\n", " "]:
sequence_buf += i
elif sequence_buf:
seps.append(sequence_buf)
sequence_buf = ""
return seps
def process(message, path):
if not message.text.lower().startswith("!shuf"):
return "", None
global module_path
module_path = path
cmd = message.text.split()
print(cmd)
l = len(cmd)
# settings
split_by = "word"
shuf_individual_words = False
data_source = "reply"
only_spaces = False
shuf_data = []
i = 1
# parsing arguments
while i < l:
print("C", i, cmd[i])
if cmd[i][0] != "-" or cmd[i] == "--":
break
elif cmd[i] == "-i":
shuf_individual_words = True
elif cmd[i] == "-c":
split_by = "char"
elif cmd[i] == "-o":
only_spaces = True
i += 1
# parsing text (if any)
while i < l:
print("T", i, cmd[i])
data_source = "internal"
if split_by == "char":
for c in cmd[i]:
shuf_data.append(c)
shuf_data.append(" ")
elif split_by == "word":
if shuf_individual_words:
shuf_data.append(list(cmd[i]))
else:
shuf_data.append(cmd[i])
i += 1
if data_source == "reply":
seps = extract_separators(message['reply_to_message'].text)
else:
seps = extract_separators(message.text)
if data_source == "reply":
for w in message['reply_to_message'].text.split():
if split_by == "char":
for c in w:
shuf_data.append(c)
elif split_by == "word":
if shuf_individual_words:
shuf_data.append(list(w))
else:
shuf_data.append(w)
if split_by == "word":
if shuf_individual_words:
for w in shuf_data:
random.shuffle(w)
if only_spaces:
return " ".join(["".join(w) for w in shuf_data]), "HTML"
else:
return "".join([i[0] + i[1] for i in zip(["".join(w) for w in shuf_data], seps)]) + "".join(shuf_data[-1]), "HTML"
else:
random.shuffle(shuf_data)
if only_spaces:
return " ".join(shuf_data), "HTML"
else:
return "".join([i[0] + i[1] for i in zip(shuf_data, seps)]) + shuf_data[-1], "HTML"
elif split_by == "char":
if only_spaces:
shuf_data += [" " for i in seps]
else:
shuf_data += seps
random.shuffle(shuf_data)
return "".join(shuf_data), "HTML"