Add a special exception for required privileged intents
This commit is contained in:
parent
4c56e6da9c
commit
6546f63ad7
@ -570,6 +570,8 @@ class Client:
|
|||||||
# sometimes, discord sends us 1000 for unknown reasons so we should reconnect
|
# sometimes, discord sends us 1000 for unknown reasons so we should reconnect
|
||||||
# regardless and rely on is_closed instead
|
# regardless and rely on is_closed instead
|
||||||
if isinstance(exc, ConnectionClosed):
|
if isinstance(exc, ConnectionClosed):
|
||||||
|
if exc.code == 4014:
|
||||||
|
raise PrivilegedIntentsRequired(exc.shard_id) from None
|
||||||
if exc.code != 1000:
|
if exc.code != 1000:
|
||||||
await self.close()
|
await self.close()
|
||||||
raise
|
raise
|
||||||
|
@ -175,3 +175,27 @@ class ConnectionClosed(ClientException):
|
|||||||
self.reason = ''
|
self.reason = ''
|
||||||
self.shard_id = shard_id
|
self.shard_id = shard_id
|
||||||
super().__init__('Shard ID %s WebSocket closed with %s' % (self.shard_id, self.code))
|
super().__init__('Shard ID %s WebSocket closed with %s' % (self.shard_id, self.code))
|
||||||
|
|
||||||
|
class PrivilegedIntentsRequired(ClientException):
|
||||||
|
"""Exception that's thrown when the gateway is requesting privileged intents
|
||||||
|
but they're not ticked in the developer page yet.
|
||||||
|
|
||||||
|
Go to https://discord.com/developers/applications/ and enable the intents
|
||||||
|
that are required. Currently these are as follows:
|
||||||
|
|
||||||
|
- :attr:`Intents.members`
|
||||||
|
- :attr:`Intents.presences`
|
||||||
|
|
||||||
|
Attributes
|
||||||
|
-----------
|
||||||
|
shard_id: Optional[:class:`int`]
|
||||||
|
The shard ID that got closed if applicable.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, shard_id):
|
||||||
|
self.shard_id = shard_id
|
||||||
|
msg = 'Shard ID %s is requesting privileged intents that have not been explicitly enabled in the ' \
|
||||||
|
'developer portal. It is recommended to go to https://discord.com/developers/applications/ ' \
|
||||||
|
'and explicitly enable the privileged intents within your application\'s page. If this is not ' \
|
||||||
|
'possible, then consider disabling the privileged intents instead.'
|
||||||
|
super().__init__(msg % shard_id)
|
||||||
|
@ -34,7 +34,15 @@ from .state import AutoShardedConnectionState
|
|||||||
from .client import Client
|
from .client import Client
|
||||||
from .backoff import ExponentialBackoff
|
from .backoff import ExponentialBackoff
|
||||||
from .gateway import *
|
from .gateway import *
|
||||||
from .errors import ClientException, InvalidArgument, HTTPException, GatewayNotFound, ConnectionClosed
|
from .errors import (
|
||||||
|
ClientException,
|
||||||
|
InvalidArgument,
|
||||||
|
HTTPException,
|
||||||
|
GatewayNotFound,
|
||||||
|
ConnectionClosed,
|
||||||
|
PrivilegedIntentsRequired,
|
||||||
|
)
|
||||||
|
|
||||||
from . import utils
|
from . import utils
|
||||||
from .enums import Status
|
from .enums import Status
|
||||||
|
|
||||||
@ -125,6 +133,9 @@ class Shard:
|
|||||||
return
|
return
|
||||||
|
|
||||||
if isinstance(e, ConnectionClosed):
|
if isinstance(e, ConnectionClosed):
|
||||||
|
if e.code == 4014:
|
||||||
|
self._queue_put(EventItem(EventType.terminate, self, PrivilegedIntentsRequired(self.id)))
|
||||||
|
return
|
||||||
if e.code != 1000:
|
if e.code != 1000:
|
||||||
self._queue_put(EventItem(EventType.close, self, e))
|
self._queue_put(EventItem(EventType.close, self, e))
|
||||||
return
|
return
|
||||||
@ -407,8 +418,11 @@ class AutoShardedClient(Client):
|
|||||||
item = await self.__queue.get()
|
item = await self.__queue.get()
|
||||||
if item.type == EventType.close:
|
if item.type == EventType.close:
|
||||||
await self.close()
|
await self.close()
|
||||||
if isinstance(item.error, ConnectionClosed) and item.error.code != 1000:
|
if isinstance(item.error, ConnectionClosed):
|
||||||
|
if item.error.code != 1000:
|
||||||
raise item.error
|
raise item.error
|
||||||
|
if item.error.code == 4014:
|
||||||
|
raise PrivilegedIntentsRequired(item.shard.id) from None
|
||||||
return
|
return
|
||||||
elif item.type in (EventType.identify, EventType.resume):
|
elif item.type in (EventType.identify, EventType.resume):
|
||||||
await item.shard.reidentify(item.error)
|
await item.shard.reidentify(item.error)
|
||||||
|
@ -2924,6 +2924,8 @@ The following exceptions are thrown by the library.
|
|||||||
|
|
||||||
.. autoexception:: ConnectionClosed
|
.. autoexception:: ConnectionClosed
|
||||||
|
|
||||||
|
.. autoexception:: PrivilegedIntentsRequired
|
||||||
|
|
||||||
.. autoexception:: discord.opus.OpusError
|
.. autoexception:: discord.opus.OpusError
|
||||||
|
|
||||||
.. autoexception:: discord.opus.OpusNotLoaded
|
.. autoexception:: discord.opus.OpusNotLoaded
|
||||||
@ -2940,6 +2942,7 @@ Exception Hierarchy
|
|||||||
- :exc:`InvalidArgument`
|
- :exc:`InvalidArgument`
|
||||||
- :exc:`LoginFailure`
|
- :exc:`LoginFailure`
|
||||||
- :exc:`ConnectionClosed`
|
- :exc:`ConnectionClosed`
|
||||||
|
- :exc:`PrivilegedIntentsRequired`
|
||||||
- :exc:`NoMoreItems`
|
- :exc:`NoMoreItems`
|
||||||
- :exc:`GatewayNotFound`
|
- :exc:`GatewayNotFound`
|
||||||
- :exc:`HTTPException`
|
- :exc:`HTTPException`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user