From 72f355bb64e05f6e9cc848445c026bfbbafca3ae Mon Sep 17 00:00:00 2001 From: Rapptz Date: Tue, 8 Dec 2015 19:37:34 -0500 Subject: [PATCH] Add OpusNotLoaded exception and opus.is_loaded utility function. --- discord/client.py | 2 ++ discord/opus.py | 31 +++++++++++++++++++++++++++++-- docs/api.rst | 6 +++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/discord/client.py b/discord/client.py index 172634710..ff5ec16fa 100644 --- a/discord/client.py +++ b/discord/client.py @@ -1879,6 +1879,8 @@ class Client: Could not connect to the voice channel in time. ClientException You are already connected to a voice channel. + OpusNotLoaded + The opus library has not been loaded. Returns ------- diff --git a/discord/opus.py b/discord/opus.py index 68c6a3ec4..2f1e2d1d3 100644 --- a/discord/opus.py +++ b/discord/opus.py @@ -111,13 +111,37 @@ def load_opus(name): global _lib _lib = libopus_loader(name) +def is_loaded(): + """Function to check if opus lib is successfully loaded either + via the ``ctypes.util.find_library`` call of :func:`load_opus`. + + This must return ``True`` for voice to work. + + Returns + ------- + bool + Indicates if the opus library has been loaded. + """ + global _lib + return _lib is not None + class OpusError(DiscordException): - """An exception that is thrown for libopus related errors.""" + """An exception that is thrown for libopus related errors. + + Attributes + ---------- + code : int + The error code returned. + """ def __init__(self, code): self.code = code msg = _lib.opus_strerror(self.code).decode('utf-8') log.info('"{}" has happened'.format(msg)) - super(DiscordException, self).__init__(msg) + super().__init__(msg) + +class OpusNotLoaded(DiscordException): + """An exception that is thrown for when libopus is not loaded.""" + pass # Some constants... @@ -137,6 +161,9 @@ class Encoder: self.samples_per_frame = int(self.sampling_rate / 1000 * self.frame_length) self.frame_size = self.samples_per_frame * self.sample_size + if not is_loaded(): + raise OpusNotLoaded() + self._state = self._create_state() def __del__(self): diff --git a/docs/api.rst b/docs/api.rst index e503e703c..05d84c318 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -33,6 +33,8 @@ Opus Library .. autofunction:: opus.load_opus +.. autofunction:: opus.is_loaded + .. _discord-api-events: Event Reference @@ -457,4 +459,6 @@ The following exceptions are thrown by the library. .. autoexception:: GatewayNotFound -.. autoexception:: OpusError +.. autoexception:: opus.OpusError + +.. autoexception:: opus.OpusNotLoaded