-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwitch_commands.py
64 lines (49 loc) · 1.87 KB
/
twitch_commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import functools
from twitchio.ext import commands
__all__ = ["twitch_command_aliased"]
def translate_message(message: str):
_my_eng_symbols = (
r"`qwertyuiop[]\asdfghjkl;'zxcvbnm,./"
+ r'~QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?'
+ "@#$^&"
)
_my_ru_symbols = (
r"ёйцукенгшщзхъ\фывапролджэячсмитьбю."
+ r"ЁЙЦУКЕНГШЩЗХЪ/ФЫВАПРОЛДЖЭЯЧСМИТЬБЮ,"
+ '"№;:?'
)
@functools.cache
def translator_to_ru():
return str.maketrans(_my_eng_symbols, _my_ru_symbols)
@functools.cache
def translator_to_eng():
return str.maketrans(_my_ru_symbols, _my_eng_symbols)
@functools.cache
def get_only_eng():
return "".join([i for i in _my_eng_symbols if i not in _my_ru_symbols])
@functools.cache
def get_only_ru():
return "".join([i for i in _my_ru_symbols if i not in _my_eng_symbols])
translated = []
for word in message.split(" "):
eng_count = len(list(filter(lambda x: x in get_only_eng(), word)))
ru_count = len(list(filter(lambda x: x in get_only_ru(), word)))
word_translator = (
translator_to_eng() if ru_count >= eng_count else translator_to_ru()
)
translated.append(str.translate(word, word_translator))
return " ".join(translated)
def twitch_command_aliased(
name: str, *args, aliases: list[str] | tuple = None, **kwargs
):
if aliases is None:
aliases = []
def decorator(function):
all_commands = [name, *aliases]
new_aliases = list(map(translate_message, all_commands))
total_aliases = sum([list(aliases), list(new_aliases)], start=[])
actual_decorator = commands.command(
name=name, aliases=total_aliases, *args, **kwargs
)
return actual_decorator(function)
return decorator