-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.py
216 lines (168 loc) · 5.84 KB
/
command.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import asyncio
import random
from db_object import DBItem
from core import Message
from media import YoutubeInfo
from actions import (
ReplyAction,
)
class Command(DBItem):
DB_TAG = 'database_command'
TYPE = 'base'
COMMAND = ['']
COMMAND_LOCATION = 'start'
def __init__(self, message_or_db):
super(Command, self).__init__(message_or_db)
if isinstance(message_or_db, Message):
self.parent_id = message_or_db.uid
else:
self.uid = message_or_db['uid']
def get_actions(self):
return []
@classmethod
def command_string(cls, args = None):
c_str = cls.COMMAND[0]
if len(cls.COMMAND) > 1:
c_str = '(alt: {}) {}'.format(', '.join(cls.COMMAND[1:]), c_str)
if args:
c_str = '{} {}'.format(c_str, args)
return c_str
@classmethod
def help_string(cls):
return None
def to_db_dict(self):
base = super(Command, self).to_db_dict()
base['parent_id'] = self.parent_id
return base
@classmethod
def is_this(cls, message):
low_message = message.lower()
matches = [0]
for alias in cls.COMMAND:
if message.lower().find(alias) == 0:
matches.append(len(alias))
return max(matches)
def __repr__(self):
return str(self)
def __str__(self):
return '{}({})'.format(self.DB_TAG, self.user)
class QueueCommand(Command):
DB_TAG = 'database_command_queue'
TYPE = 'queue'
COMMAND = ['!queue', '!q']
COMMAND_LOCATION = 'start'
def __init__(self, message_or_db):
super(QueueCommand, self).__init__(message_or_db)
if isinstance(message_or_db, Message):
self.youtube_info = YoutubeInfo.create_from_message(message_or_db)
else:
self.youtube_info = YoutubeInfo.from_db_dict(message_or_db)
def _generate_confirmation_text(self):
return 'Added to queue: {}'.format(self.youtube_info.display())
def get_actions(self):
if not self.youtube_info.prepared:
return [ReplyAction("Sorry, I could not find a youtube url in your command. I can't understand anything other than 'youtube.com/watch?v=...' links (including youtu.be).", self.parent_id)]
else:
return []
def to_db_dict(self):
return self.youtube_info.to_db_dict(super(QueueCommand, self).to_db_dict())
@asyncio.coroutine
def prepare(self):
yield from self.youtube_info.prepare()
def is_prepared(self):
return self.youtube_info.prepared
@classmethod
def help_string(cls):
return "{}: Add a song to the queue. Does not support any link shorterners (youtube's included). Songs will be played in order.".format(cls.command_string('<youtube url>'))
def __str__(self):
return '[QueueCommand] {}'.format(self.youtube_info)
class QueueFirstCommand(QueueCommand):
DB_TAG = 'database_command_queue_first'
COMMAND = ['!queuefirst', '!qf']
COMMAND_LOCATION = 'start'
@classmethod
def help_string(cls):
return "{}: Add a song at the start of the queue. Same restrictions as !queue.".format(cls.command_string('<youtube url>'))
def __str__(self):
return '[QueueFirstCommand] {}'.format(self.youtube_info)
class ClearQueueCommand(Command):
DB_TAG = 'database_command_clearqueue'
COMMAND = ['!clearqueue']
COMMAND_LOCATION = 'start'
class ListQueueCommand(Command):
DB_TAG = 'database_command_listqueue'
COMMAND = ['!list']
COMMAND_LOCATION = 'start'
def get_actions(self):
return []
@classmethod
def help_string(cls):
return "{}: List the songs currently in the queue.".format(cls.command_string())
class DumpQueueCommand(Command):
DB_TAG = 'database_command_dumpqueue'
COMMAND = ['!dumpqueue']
COMMAND_LOCATION = 'start'
def get_actions(self):
return []
@classmethod
def help_string(cls):
return "{}: List all queued songs with youtube URL included and then DELETES the queue. Useful for shutting down the bot.".format(cls.command_string())
class SkipCommand(Command):
DB_TAG = 'database_command_skip'
COMMAND = ['!skip']
COMMAND_LOCATION = 'start'
def get_actions(self):
return []
@classmethod
def help_string(cls):
return "{}: Skip a song. Please note: one song will be skipped per command! Use carefully.".format(cls.command_string())
class DramaticSkipCommand(Command):
DB_TAG = 'database_command_dramatic_skip'
COMMAND = ['!dramaticskip']
COMMAND_LOCATION = 'start'
class TestSkipCommand(Command):
DB_TAG = 'database_command_testskip'
COMMAND = ['!testskip']
COMMAND_LOCATION = 'start'
def get_actions(self):
return []
class NeonLightShowCommand(Command):
DB_TAG = 'database_command_neonlightshow'
COMMAND = ['!neonlightshow']
COMMAND_LOCATION = 'start'
def get_actions(self):
images = [
'http://i.imgur.com/eBZO67G.gif', 'http://i.imgur.com/0bprD6k.gif', 'http://i.imgur.com/van2j15.gif', 'http://i.imgur.com/sYjX7Qv.gif',
'http://i.imgur.com/sNm4j9n.gif', 'http://i.imgur.com/uXSlR5b.gif', 'http://i.imgur.com/hGSXbsa.gif', 'http://i.imgur.com/UlpqRbK.gif',
'http://i.imgur.com/Wmm7EZg.gif', 'http://i.imgur.com/QdYSbbA.gif', 'http://i.imgur.com/Zy5heqF.gif', 'http://i.imgur.com/H4vsVkh.gif'
]
return [ReplyAction(random.choice(images), str(self.parent_id))]
class HelpCommand(Command):
DB_TAG = 'database_command_help'
COMMAND = ['!help']
COMMAND_LOCATION = 'start'
def set_commands(self, commands):
self.commands = commands
def get_help_lines(self):
lines = ['Commands with help text:']
for command in self.commands:
if hasattr(command, 'help_string') and command.help_string():
lines.append(command.help_string())
if len(lines):
return '\n'.join(lines)
else:
return None
def get_actions(self):
if self.get_help_lines():
return [ReplyAction(self.get_help_lines(), str(self.parent_id))]
DBItem.SUBCLASSES.update({
QueueCommand.DB_TAG: QueueCommand,
QueueFirstCommand.DB_TAG: QueueFirstCommand,
SkipCommand.DB_TAG: SkipCommand,
ClearQueueCommand.DB_TAG: ClearQueueCommand,
ListQueueCommand.DB_TAG: ListQueueCommand,
TestSkipCommand.DB_TAG: TestSkipCommand,
NeonLightShowCommand.DB_TAG: NeonLightShowCommand,
HelpCommand.DB_TAG: HelpCommand,
DumpQueueCommand.DB_TAG: DumpQueueCommand,
})