PagerMaid-Discord/pagermaid/utils.py

71 lines
2.3 KiB
Python
Raw Normal View History

2021-07-25 06:00:08 +00:00
""" Libraries for python modules. """
from emoji import get_emoji_regexp
from random import choice
from json import load as load_json
from re import sub, IGNORECASE
from asyncio import create_subprocess_shell
from asyncio.subprocess import PIPE
from pagermaid import module_dir
# def lang(text: str) -> str:
# """ i18n """
# result = lang_dict.get(text, text)
# return result
async def execute(command, pass_error=True):
""" Executes command and returns output, with the option of enabling stderr. """
executor = await create_subprocess_shell(
command,
stdout=PIPE,
stderr=PIPE
)
try:
stdout, stderr = await executor.communicate()
except:
return 'error'
if pass_error:
result = str(stdout.decode().strip()) \
+ str(stderr.decode().strip())
else:
result = str(stdout.decode().strip())
return result
def owoify(text):
""" Converts your text to OwO """
smileys = [';;w;;', '^w^', '>w<', 'UwU', '(・`ω´・)', '(´・ω・`)']
with open(f"{module_dir}/assets/replacements.json") as fp:
replacements = load_json(fp)
for expression in replacements:
replacement = replacements[expression]
text = sub(expression, replacement, text, flags=IGNORECASE)
words = text.split()
first_letter = words[0][0]
letter_stutter = f"{first_letter}-{first_letter.lower()}-{first_letter.lower()}"
if len(words[0]) > 1:
words[0] = letter_stutter + words[0][1:]
else:
words[0] = letter_stutter
text = " ".join(words)
text = text.replace('L', 'W').replace('l', 'w')
text = text.replace('R', 'W').replace('r', 'w')
text = '! {}'.format(choice(smileys)).join(text.rsplit('!', 1))
text = '? OwO'.join(text.rsplit('?', 1))
text = '. {}'.format(choice(smileys)).join(text.rsplit('.', 1))
text = f"{text} desu"
for v in ['a', 'o', 'u', 'A', 'O', 'U']:
if 'n{}'.format(v) in text:
text = text.replace('n{}'.format(v), 'ny{}'.format(v))
if 'N{}'.format(v) in text:
text = text.replace('N{}'.format(v), 'N{}{}'.format('Y' if v.isupper() else 'y', v))
return text
def clear_emojis(target):
""" Removes all Emojis from provided string """
return get_emoji_regexp().sub(u'', target)