forked from Just-Some-Bots/MusicBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructs.py
More file actions
460 lines (389 loc) · 15.4 KB
/
constructs.py
File metadata and controls
460 lines (389 loc) · 15.4 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import asyncio
import inspect
import json
import logging
from pathlib import Path
import pydoc
from collections import defaultdict
from typing import (
TYPE_CHECKING,
Any,
Callable,
DefaultDict,
Dict,
List,
Optional,
Set,
Type,
Union,
)
import discord
from .constants import DATA_GUILD_FILE_OPTIONS
from .json import Json
from .utils import _get_variable
log = logging.getLogger(__name__)
if TYPE_CHECKING:
from .autoplaylist import AutoPlaylist
from .bot import MusicBot
from .config import Config
class GuildAsyncEvent(asyncio.Event):
"""
Simple extension of asyncio.Event() to provide a boolean flag for activity.
"""
def __init__(self) -> None:
"""
Create an event with an activity flag.
"""
super().__init__()
self._event_active: bool = False
def is_active(self) -> bool:
"""Reports activity state"""
return self._event_active
def activate(self) -> None:
"""Sets the event's active flag."""
self._event_active = True
def deactivate(self) -> None:
"""Unset the event's active flag."""
self._event_active = False
class GuildSpecificData:
"""
A typed collection of data specific to each guild/discord server.
"""
def __init__(self, bot: "MusicBot") -> None:
"""
Initialize a managed server specific data set.
"""
# Members for internal use only.
self._ssd: DefaultDict[int, "GuildSpecificData"] = bot.server_data
self._bot_config: "Config" = bot.config
self._bot: "MusicBot" = bot
self._guild_id: int = 0
self._guild_name: str = ""
self._command_prefix: str = ""
self._prefix_history: Set[str] = set()
self._events: DefaultDict[str, GuildAsyncEvent] = defaultdict(GuildAsyncEvent)
self._file_lock: asyncio.Lock = asyncio.Lock()
self._loading_lock: asyncio.Lock = asyncio.Lock()
self._is_file_loaded: bool = False
# Members below are available for public use.
self.last_np_msg: Optional["discord.Message"] = None
self.last_played_song_subject: str = ""
self.follow_user: Optional["discord.Member"] = None
self.auto_join_channel: Optional[
Union["discord.VoiceChannel", "discord.StageChannel"]
] = None
self.autoplaylist: "AutoPlaylist" = self._bot.playlist_mgr.get_default()
self.current_playing_url: str = ""
# create a task to load any persistent guild options.
# in theory, this should work out fine.
bot.create_task(self.load_guild_options_file(), name="MB_LoadGuildOptions")
bot.create_task(self.autoplaylist.load(), name="MB_LoadAPL")
def is_ready(self) -> bool:
"""A status indicator for fully loaded server data."""
return self._is_file_loaded and self._guild_id != 0
def _lookup_guild_id(self) -> int:
"""
Looks up guild.id used to create this instance of GuildSpecificData
Will return 0 if for some reason lookup fails.
"""
for key, val in self._ssd.items():
if val == self:
guild = discord.utils.find(
lambda m: m.id == key, # pylint: disable=cell-var-from-loop
self._bot.guilds,
)
if guild:
self._guild_name = guild.name
return key
return 0
async def get_played_history(self) -> Optional["AutoPlaylist"]:
"""Get the history playlist for this guild, if enabled."""
if not self._bot.config.enable_queue_history_guilds:
return None
if not self.is_ready():
return None
pl = self._bot.playlist_mgr.get_playlist(f"history-{self._guild_id}.txt")
pl.create_file()
if not pl.loaded:
await pl.load()
return pl
@property
def command_prefix(self) -> str:
"""
If per-server prefix is enabled, and the server has a specific
command prefix, it will be returned.
Otherwise the default command prefix is returned from MusicBot config.
"""
if self._bot_config.enable_options_per_guild:
if self._command_prefix:
return self._command_prefix
return self._bot_config.command_prefix
@command_prefix.setter
def command_prefix(self, value: str) -> None:
"""Set the value of command_prefix"""
if not value:
raise ValueError("Cannot set an empty prefix.")
# update prefix history
if not self._command_prefix:
self._prefix_history.add(self._bot_config.command_prefix)
else:
self._prefix_history.add(self._command_prefix)
# set prefix value
self._command_prefix = value
# clean up history buffer if needed.
if len(self._prefix_history) > 3:
self._prefix_history.pop()
@property
def command_prefix_list(self) -> List[str]:
"""
Get the prefix list for this guild.
It includes a history of prefix changes since last restart as well.
"""
history = list(self._prefix_history)
# add self mention to invoke list.
if self._bot_config.commands_via_mention and self._bot.user:
history.append(f"<@{self._bot.user.id}>")
# Add current prefix to list.
if self._command_prefix:
history = [self._command_prefix] + history
else:
history = [self._bot_config.command_prefix] + history
return history
def get_event(self, name: str) -> GuildAsyncEvent:
"""
Gets an event by the given `name` or otherwise creates and stores one.
"""
return self._events[name]
async def load_guild_options_file(self) -> None:
"""
Load a JSON file from the server's data directory that contains
server-specific options intended to persist through shutdowns.
This method only supports per-server command prefix currently.
"""
if self._loading_lock.locked():
return
async with self._loading_lock:
if self._guild_id == 0:
self._guild_id = self._lookup_guild_id()
if self._guild_id == 0:
log.error(
"Cannot load data for guild with ID 0. This is likely a bug in the code!"
)
return
opt_file = self._bot_config.data_path.joinpath(
str(self._guild_id), DATA_GUILD_FILE_OPTIONS
)
if not opt_file.is_file():
log.debug("No file for guild %s/%s", self._guild_id, self._guild_name)
self._is_file_loaded = True
return
async with self._file_lock:
try:
log.debug(
"Loading guild data for guild with ID: %s/%s",
self._guild_id,
self._guild_name,
)
options = Json(opt_file)
self._is_file_loaded = True
except OSError:
log.exception(
"An OS error prevented reading guild data file: %s",
opt_file,
)
return
guild_prefix = options.get("command_prefix", None)
if guild_prefix:
self._command_prefix = guild_prefix
log.info(
"Guild %s/%s has custom command prefix: %s",
self._guild_id,
self._guild_name,
self._command_prefix,
)
guild_playlist = options.get("auto_playlist", None)
if guild_playlist:
self.autoplaylist = self._bot.playlist_mgr.get_playlist(guild_playlist)
await self.autoplaylist.load()
async def save_guild_options_file(self) -> None:
"""
Save server-specific options, like the command prefix, to a JSON
file in the server's data directory.
"""
if self._guild_id == 0:
log.error(
"Cannot save data for guild with ID 0. This is likely a bug in the code!"
)
return
opt_file = self._bot_config.data_path.joinpath(
str(self._guild_id), DATA_GUILD_FILE_OPTIONS
)
auto_playlist = None
if self.autoplaylist is not None:
auto_playlist = self.autoplaylist.filename
# Prepare a dictionary to store our options.
opt_dict = {
"command_prefix": self._command_prefix,
"auto_playlist": auto_playlist,
}
async with self._file_lock:
try:
with open(opt_file, "w", encoding="utf8") as fh:
fh.write(json.dumps(opt_dict))
except OSError:
log.exception("Could not save guild specific data due to OS Error.")
except (TypeError, ValueError):
log.exception(
"Failed to serialize guild specific data due to invalid data."
)
class SkipState:
__slots__ = ["skippers", "skip_msgs"]
def __init__(self) -> None:
"""
Manage voters and their ballots for fair MusicBot track skipping.
This creates a set of discord.Message and a set of member IDs to
enable counting votes for skipping a song.
"""
self.skippers: Set[int] = set()
self.skip_msgs: Set["discord.Message"] = set()
@property
def skip_count(self) -> int:
"""
Get the number of authors who requested skip.
"""
return len(self.skippers)
def reset(self) -> None:
"""
Clear the vote counting sets.
"""
self.skippers.clear()
self.skip_msgs.clear()
def add_skipper(self, skipper_id: int, msg: "discord.Message") -> int:
"""
Add a message and the author's ID to the skip vote.
"""
self.skippers.add(skipper_id)
self.skip_msgs.add(msg)
return self.skip_count
class Response:
__slots__ = ["_content", "reply", "delete_after", "codeblock", "_codeblock"]
def __init__(
self,
content: Union[str, "discord.Embed"],
reply: bool = False,
delete_after: int = 0,
codeblock: str = "",
) -> None:
"""
Helper class intended to be used by command functions in MusicBot.
Simple commands should return a Response rather than calling to send
messages on their own.
:param: content: the text message or an Embed object to be sent.
:param: reply: if this response should reply to the original author.
:param: delete_after: how long to wait before deleting the message created by this Response.
Set to 0 to never delete.
:param: codeblock: format a code block with this value as the language used for syntax highlights.
"""
self._content = content
self.reply = reply
self.delete_after = delete_after
self.codeblock = codeblock
self._codeblock = f"```{codeblock}\n{{}}\n```"
@property
def content(self) -> Union[str, "discord.Embed"]:
"""
Get the Response content, but quietly format a code block if needed.
"""
if self.codeblock:
return self._codeblock.format(self._content)
return self._content
class Serializer(json.JSONEncoder):
def default(self, o: "Serializable") -> Any:
"""
Default method used by JSONEncoder to return serializable data for
the given object or Serializable in `o`
"""
if hasattr(o, "__json__"):
return o.__json__()
# Convert Path objects to string for JSON serialization
if isinstance(o, Path):
return str(o)
return super().default(o)
@classmethod
def deserialize(cls, data: Dict[str, Any]) -> Any:
"""
Read a simple JSON dict for a valid class signature, and pass the
simple dict on to a _deserialize function in the signed class.
"""
if all(x in data for x in Serializable.CLASS_SIGNATURE):
# log.debug("Deserialization requested for %s", data)
factory = pydoc.locate(data["__module__"] + "." + data["__class__"])
# log.debug("Found object %s", factory)
if factory and issubclass(factory, Serializable): # type: ignore[arg-type]
# log.debug("Deserializing %s object", factory)
return factory._deserialize( # type: ignore[attr-defined]
data["data"], **cls._get_vars(factory._deserialize) # type: ignore[attr-defined]
)
return data
@classmethod
def _get_vars(cls, func: Callable[..., Any]) -> Dict[str, Any]:
"""
Inspect argument specification for given callable `func` and attempt
to inject it's named parameters by inspecting the calling frames for
locals which match the parameter names.
"""
# log.debug("Getting vars for %s", func)
params = inspect.signature(func).parameters.copy()
args = {}
# log.debug("Got %s", params)
for name, param in params.items():
# log.debug("Checking arg %s, type %s", name, param.kind)
if param.kind is param.POSITIONAL_OR_KEYWORD and param.default is None:
# log.debug("Using var %s", name)
args[name] = _get_variable(name)
# log.debug("Collected var for arg '%s': %s", name, args[name])
return args
class Serializable:
CLASS_SIGNATURE = ("__class__", "__module__", "data")
def _enclose_json(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""
Helper used by child instances of Serializable that includes class signature
for the Serializable object.
Intended to be called from __json__ methods of child instances.
"""
return {
"__class__": self.__class__.__qualname__,
"__module__": self.__module__,
"data": data,
}
# Perhaps convert this into some sort of decorator
@staticmethod
def _bad(arg: str) -> None:
"""
Wrapper used by assertions in Serializable classes to enforce required arguments.
:param: arg: the parameter name being enforced.
:raises: TypeError when given `arg` is None in calling frame.
"""
raise TypeError(f"Argument '{arg}' must not be None")
def serialize(self, *, cls: Type[Serializer] = Serializer, **kwargs: Any) -> str:
"""
Simple wrapper for json.dumps with Serializer instance support.
"""
return json.dumps(self, cls=cls, **kwargs)
def __json__(self) -> Optional[Dict[str, Any]]:
"""
Serialization method to be implemented by derived classes.
Should return a simple dictionary representing the Serializable
class and its data/state, using only built-in types.
"""
raise NotImplementedError
@classmethod
def _deserialize(
cls: Type["Serializable"], raw_json: Dict[str, Any], **kwargs: Any
) -> Any:
"""
Deserialization handler, to be implemented by derived classes.
Should construct and return a valid Serializable child instance or None.
:param: raw_json: data from json.loads() using built-in types only.
"""
raise NotImplementedError