mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 19:44:20 +00:00
Tweak a bit the ship command
This commit is contained in:
parent
55ba97e89a
commit
269d86e89c
3 changed files with 24 additions and 7 deletions
|
@ -1,6 +1,5 @@
|
|||
import re
|
||||
from ..utils import Command, Call
|
||||
|
||||
from ..utils import Command, Call, safeformat
|
||||
|
||||
SHIP_RESULT = "💕 {one} + {two} = <b>{result}</b>"
|
||||
|
||||
|
@ -17,17 +16,21 @@ class ShipCommand(Command):
|
|||
name_two = args[2]
|
||||
name_one = name_one.lower()
|
||||
name_two = name_two.lower()
|
||||
# Get all letters until the first vowel, included
|
||||
match_one = re.search(r"^[A-Za-z][^aeiouAEIOU]*[aeiouAEIOU]?", name_one)
|
||||
if match_one is None:
|
||||
part_one = name_one[:int(len(name_one) / 2)]
|
||||
else:
|
||||
part_one = match_one.group(0)
|
||||
# Get all letters from the second to last vowel, excluded
|
||||
match_two = re.search(r"[^aeiouAEIOU]*[aeiouAEIOU]?[A-Za-z]$", name_two)
|
||||
if match_two is None:
|
||||
part_two = name_two[int(len(name_two) / 2):]
|
||||
else:
|
||||
part_two = match_two.group(0)
|
||||
mixed = part_one + part_two # TODO: find out what exceptions this could possibly raise
|
||||
await call.reply(SHIP_RESULT.format(one=name_one.capitalize(),
|
||||
two=name_two.capitalize(),
|
||||
result=mixed.capitalize()))
|
||||
# Combine the two name parts
|
||||
mixed = part_one + part_two
|
||||
await call.reply(safeformat(SHIP_RESULT,
|
||||
one=name_one.capitalize(),
|
||||
two=name_two.capitalize(),
|
||||
result=mixed.capitalize()))
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from .asyncify import asyncify
|
||||
from .call import Call
|
||||
from .command import Command
|
||||
from .safeformat import safeformat
|
||||
|
||||
__all__ = ["asyncify", "Call", "Command"]
|
||||
__all__ = ["asyncify", "Call", "Command", "safeformat"]
|
||||
|
|
13
royalnet/utils/safeformat.py
Normal file
13
royalnet/utils/safeformat.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
class SafeDict(dict):
|
||||
def __missing__(self, key):
|
||||
return "{" + key + "}"
|
||||
|
||||
|
||||
def safeformat(string: str, ignore_escaping: bool = False, **words: str) -> str:
|
||||
if ignore_escaping:
|
||||
escaped = words
|
||||
else:
|
||||
escaped = {}
|
||||
for key in words:
|
||||
escaped[key] = str(words[key]).replace("<", "<").replace(">", ">")
|
||||
return string.format_map(SafeDict(**escaped))
|
Loading…
Reference in a new issue