-
Notifications
You must be signed in to change notification settings - Fork 2
/
moves.py
104 lines (68 loc) · 2.76 KB
/
moves.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import re
from playbooks import get_playbook_names, get_playbook_list
from language_handler import get_translation, LANG_LIST
from utils import get_moves as get_moves_json_array
from config_interactions import get_raw_lang
COMMA_SEPARATOR = ', '
PLUS = '+'
def compare_move_to_playbook(move, playbook_name):
if 'playbook' in move:
return move['playbook'] == playbook_name
return False
def join_with_commas(array_to_join, field):
result = ''
index = 1
for joinable in array_to_join:
result = result + joinable[field]
if index != len(array_to_join):
result = result + COMMA_SEPARATOR
index = index + 1
return result
def join_with_detail(array_to_join, lang):
response = get_translation(lang, 'moves.moves_plus.response_header')
for p in array_to_join:
response = response + p['capital'].capitalize() + " - " + p['description'] + \
COMMA_SEPARATOR + p['shortName'] + \
COMMA_SEPARATOR + p['label'] + "\n "
return response
def get_playbook_field(command, lang):
playbook_list = get_playbook_list(lang)
if command in playbook_list:
return command
moves = get_translation(lang, 'moves.moves')
if command.startswith(moves):
return 'basic'
adult = get_translation(lang, 'moves.adult')
if command.startswith(adult):
return 'adult'
return ''
def parse_command(command):
raw_command = command[1:]
has_plus = PLUS in raw_command
if has_plus:
command_name = raw_command.split(PLUS)[0]
return command_name, True
return raw_command, False
def get_unknown_playbook_response(lang):
response = get_translation(lang, 'moves.non_existent_playbook_intro')
for playbook in get_playbook_names(lang):
response = response + playbook
response = response + get_translation(lang, 'moves.non_existent_playbook_end')
# return response #changing this as we need to be polite to other bots, there's a lot of them
return 0
def get_moves(message):
content = message.content
for lang in LANG_LIST:
moves_array = get_moves_json_array(lang)
type_of_command, show_detail = parse_command(content)
playbook_field = get_playbook_field(type_of_command, lang)
if playbook_field:
raw_lang = get_raw_lang(message)
moves_by_playbook = list(filter(lambda move_dict: compare_move_to_playbook(
move_dict, playbook_field), moves_array['moves']))
if not len(moves_by_playbook):
return get_unknown_playbook_response(raw_lang)
if show_detail:
return join_with_detail(moves_by_playbook, raw_lang)
return join_with_commas(moves_by_playbook, 'shortName')
return None