This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.py
175 lines (116 loc) · 4.67 KB
/
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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from models import *
from pointtcl import create_database, check_lines
from envparse import env
import inspect
__all__ = [
'HelpCommand',
'SubwayStatusCommand',
'TramStatusCommand',
'BusStatusCommand',
'FunicularStatusCommand',
'ResetDatabaseCommand',
'CheckNowCommand'
]
subway_names = ['métro', 'metro']
tram_names = ['tram']
bus_names = ['bus']
funicular_names = ['funiculaire', 'funi']
class Command:
names = []
bot = None
user = None
channel = None
def get_names(self):
if not self.names:
raise ValueError('self.names must contain at least one name')
return self.names
def get_params(self):
return inspect.signature(self.run).parameters.keys()
def run(self):
raise NotImplementedError('Must be implemented')
class AdminCommand(Command):
def _is_user_allowed(self):
admins = env.list('BOT_ADMINS', default=[])
if self.user not in admins:
self.bot.say('<@{user}> Vous n\'avez pas autorité sur moi. Et bim.'.format(user=self.user), self.channel)
return False
return True
class CheckLineCommand(Command):
def _check_line(self, type, line, unknown, disrupted, ok):
line_object = TclLine.find_line_by_type(type, line.lower())
if not line_object:
self.bot.say_random(unknown, self.channel, user=self.user, line=line.upper())
return
if line_object.is_disrupted:
self.bot.say_random(disrupted, self.channel, line=line.upper(), since=line_object.latest_disruption_started_at.humanize(locale='FR'), reason='\n _' + line_object.latest_disruption_reason + '_' if line_object.latest_disruption_reason else '')
else:
self.bot.say_random(ok, self.channel, line=line.upper())
class HelpCommand(Command):
names = ['aide', 'help', 'comment', 'dafuq', 'wut', 'hein']
def run(self):
help_answer = """
Déjà : bonjour.
Comment ça marche : en me mentionnant, le premier mot désigne sur quel type de ligne vous souhaitez avoir des infos. Le deuxième le nom de la ligne.
Exemples :
> @pointtcl métro d
> @pointtcl tram t4
> @pointtcl bus 31 bordel
> @pointtcl funi f2
En retour je vous dis s'il y a un souci ou pas.
*Attention :* je ne suis disponible que sur le canal #transports.
Bien à vous,
"""
self.bot.say(help_answer, self.channel)
class NextPassesCommand(Command):
names = ['prochain', 'next', 'suivant']
def _get_next_passes(self):
# TODO
pass
def run(self, type, line):
if type in subway_names:
type = TclLineType.SUBWAY
elif type in tram_names:
type = TclLineType.TRAM
elif type in bus_names:
type = TclLineType.BUS
elif type in funicular_names:
type = TclLineType.FUNICULAR
else:
self.bot.say('Je connais pas ce type de ligne.', self.channel)
return
line_object = TclLine.find_line_by_type(type, line.lower())
if not line_object:
self.bot.say_random(unknown, self.channel, user=self.user, line=line.upper())
return
class SubwayStatusCommand(CheckLineCommand):
names = subway_names
def run(self, line):
self._check_line(TclLineType.SUBWAY, line, 'unknown_subway_line', 'subway_line_disrupted', 'subway_line_ok')
class TramStatusCommand(CheckLineCommand):
names = tram_names
def run(self, line):
self._check_line(TclLineType.TRAM, line, 'unknown_tram_line', 'tram_line_disrupted', 'tram_line_ok')
class BusStatusCommand(CheckLineCommand):
names = bus_names
def run(self, line):
self._check_line(TclLineType.BUS, line, 'unknown_bus_line', 'bus_line_disrupted', 'bus_line_ok')
class FunicularStatusCommand(CheckLineCommand):
names = funicular_names
def run(self, line):
self._check_line(TclLineType.FUNICULAR, line, 'unknown_funicular_line', 'funicular_line_disrupted', 'funicular_line_ok')
class ResetDatabaseCommand(AdminCommand):
names = ['resetbdd']
def run(self):
if not self._is_user_allowed():
return
self.bot.say('<@{user}> Réinitialisation de la base de données.'.format(user=self.user), self.channel)
create_database()
self.bot.say('<@{user}> Daune.'.format(user=self.user), self.channel)
class CheckNowCommand(AdminCommand):
names = ['verif', 'vérif']
def run(self):
if not self._is_user_allowed():
return
self.bot.say('<@{user}> Lancement de la vérification de toutes les lignes.'.format(user=self.user), self.channel)
check_lines()
self.bot.say('<@{user}> Daune.'.format(user=self.user), self.channel)