Skip to content

Commit b27b438

Browse files
committed
merge upstream
2 parents 56caff5 + 0ea2077 commit b27b438

File tree

10 files changed

+169
-63
lines changed

10 files changed

+169
-63
lines changed

discord/gateway.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from __future__ import annotations
2727

2828
import asyncio
29-
from collections.abc import Callable
3029
import concurrent.futures
3130
import logging
3231
import struct
@@ -36,6 +35,7 @@
3635
import traceback
3736
import zlib
3837
from collections import deque
38+
from collections.abc import Callable
3939
from typing import TYPE_CHECKING, Any, NamedTuple
4040

4141
import aiohttp
@@ -324,7 +324,12 @@ class DiscordWebSocket:
324324
shard_count: int | None
325325
_max_heartbeat_timeout: float
326326

327-
def __init__(self, socket: aiohttp.ClientWebSocketResponse, *, loop: asyncio.AbstractEventLoop) -> None:
327+
def __init__(
328+
self,
329+
socket: aiohttp.ClientWebSocketResponse,
330+
*,
331+
loop: asyncio.AbstractEventLoop,
332+
) -> None:
328333
self.socket: aiohttp.ClientWebSocketResponse = socket
329334
self.loop: asyncio.AbstractEventLoop = loop
330335

@@ -658,7 +663,7 @@ async def poll_event(self) -> None:
658663
elif msg.type is aiohttp.WSMsgType.BINARY:
659664
await self.received_message(msg.data)
660665
elif msg.type is aiohttp.WSMsgType.ERROR:
661-
_log.debug('Received an error %s', msg)
666+
_log.debug("Received an error %s", msg)
662667
elif msg.type in (
663668
aiohttp.WSMsgType.CLOSED,
664669
aiohttp.WSMsgType.CLOSING,

discord/raw_models.py

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@
2525

2626
from __future__ import annotations
2727

28-
from collections.abc import ItemsView, KeysView, ValuesView
2928
import datetime
29+
from collections.abc import ItemsView, KeysView, ValuesView
3030
from typing import TYPE_CHECKING, Any
3131

32+
from . import utils
3233
from .automod import AutoModAction, AutoModTriggerType
3334
from .enums import AuditLogAction, ChannelType, ReactionType, try_enum
34-
from . import utils
3535

3636
if TYPE_CHECKING:
37-
from .abc import MessageableChannel, GuildChannel
37+
from .abc import GuildChannel, MessageableChannel
3838
from .guild import Guild
3939
from .member import Member
4040
from .message import Message
@@ -94,7 +94,11 @@ class _RawReprMixin:
9494
__slots__: tuple[str, ...]
9595

9696
def __repr__(self) -> str:
97-
value = " ".join(f"{attr}={getattr(self, attr)!r}" for attr in self.__slots__ if not attr.startswith('_'))
97+
value = " ".join(
98+
f"{attr}={getattr(self, attr)!r}"
99+
for attr in self.__slots__
100+
if not attr.startswith("_")
101+
)
98102
return f"<{self.__class__.__name__} {value}>"
99103

100104

@@ -850,23 +854,24 @@ def __init__(self, data: MessagePollVoteEvent, added: bool) -> None:
850854
except KeyError:
851855
self.guild_id: int | None = None
852856

857+
853858
# this is for backwards compatibility because VoiceProtocol.on_voice_..._update
854859
# passed the raw payload instead of a raw object. Emit deprecation warning.
855860
class _PayloadLike(_RawReprMixin):
856861
_raw_data: dict[str, Any]
857862

858863
@utils.deprecated(
859-
'the attributes',
860-
'2.7',
861-
'3.0',
864+
"the attributes",
865+
"2.7",
866+
"3.0",
862867
)
863868
def __getitem__(self, key: str) -> Any:
864869
return self._raw_data[key]
865870

866871
@utils.deprecated(
867-
'the attributes',
868-
'2.7',
869-
'3.0',
872+
"the attributes",
873+
"2.7",
874+
"3.0",
870875
)
871876
def get(self, key: str, default: Any = None) -> Any:
872877
"""Gets an item from this raw event, and returns its value or ``default``.
@@ -877,9 +882,9 @@ def get(self, key: str, default: Any = None) -> Any:
877882
return self._raw_data.get(key, default)
878883

879884
@utils.deprecated(
880-
'the attributes',
881-
'2.7',
882-
'3.0',
885+
"the attributes",
886+
"2.7",
887+
"3.0",
883888
)
884889
def items(self) -> ItemsView:
885890
"""Returns the (key, value) pairs of this raw event.
@@ -890,9 +895,9 @@ def items(self) -> ItemsView:
890895
return self._raw_data.items()
891896

892897
@utils.deprecated(
893-
'the attributes',
894-
'2.7',
895-
'3.0',
898+
"the attributes",
899+
"2.7",
900+
"3.0",
896901
)
897902
def values(self) -> ValuesView:
898903
"""Returns the values of this raw event.
@@ -903,9 +908,9 @@ def values(self) -> ValuesView:
903908
return self._raw_data.values()
904909

905910
@utils.deprecated(
906-
'the attributes',
907-
'2.7',
908-
'3.0',
911+
"the attributes",
912+
"2.7",
913+
"3.0",
909914
)
910915
def keys(self) -> KeysView:
911916
"""Returns the keys of this raw event.
@@ -957,37 +962,37 @@ class RawVoiceStateUpdateEvent(_PayloadLike):
957962
"""
958963

959964
__slots__ = (
960-
'session_id',
961-
'mute',
962-
'deaf',
963-
'self_mute',
964-
'self_deaf',
965-
'self_stream',
966-
'self_video',
967-
'suppress',
968-
'requested_to_speak_at',
969-
'afk',
970-
'channel',
971-
'guild_id',
972-
'channel_id',
973-
'_state',
974-
'_raw_data',
965+
"session_id",
966+
"mute",
967+
"deaf",
968+
"self_mute",
969+
"self_deaf",
970+
"self_stream",
971+
"self_video",
972+
"suppress",
973+
"requested_to_speak_at",
974+
"afk",
975+
"channel",
976+
"guild_id",
977+
"channel_id",
978+
"_state",
979+
"_raw_data",
975980
)
976981

977982
def __init__(self, *, data: VoiceStateEvent, state: ConnectionState) -> None:
978-
self.session_id: str = data['session_id']
983+
self.session_id: str = data["session_id"]
979984
self._state: ConnectionState = state
980985

981-
self.self_mute: bool = data.get('self_mute', False)
982-
self.self_deaf: bool = data.get('self_deaf', False)
983-
self.mute: bool = data.get('mute', False)
984-
self.deaf: bool = data.get('deaf', False)
985-
self.suppress: bool = data.get('suppress', False)
986+
self.self_mute: bool = data.get("self_mute", False)
987+
self.self_deaf: bool = data.get("self_deaf", False)
988+
self.mute: bool = data.get("mute", False)
989+
self.deaf: bool = data.get("deaf", False)
990+
self.suppress: bool = data.get("suppress", False)
986991
self.requested_to_speak_at: datetime.datetime | None = utils.parse_time(
987-
data.get('request_to_speak_timestamp')
992+
data.get("request_to_speak_timestamp")
988993
)
989-
self.guild_id: int | None = utils._get_as_snowflake(data, 'guild_id')
990-
self.channel_id: int | None = utils._get_as_snowflake(data, 'channel_id')
994+
self.guild_id: int | None = utils._get_as_snowflake(data, "guild_id")
995+
self.channel_id: int | None = utils._get_as_snowflake(data, "channel_id")
991996
self._raw_data: VoiceStateEvent = data
992997

993998
@property
@@ -1017,18 +1022,18 @@ class RawVoiceServerUpdateEvent(_PayloadLike):
10171022
"""
10181023

10191024
__slots__ = (
1020-
'token',
1021-
'guild_id',
1022-
'endpoint',
1023-
'_raw_data',
1024-
'_state',
1025+
"token",
1026+
"guild_id",
1027+
"endpoint",
1028+
"_raw_data",
1029+
"_state",
10251030
)
10261031

10271032
def __init__(self, *, data: VoiceServerUpdateEvent, state: ConnectionState) -> None:
10281033
self._state: ConnectionState = state
1029-
self.guild_id: int = int(data['guild_id'])
1030-
self.token: str = data['token']
1031-
self.endpoint: str | None = data['endpoint']
1034+
self.guild_id: int = int(data["guild_id"])
1035+
self.token: str = data["token"]
1036+
self.endpoint: str | None = data["endpoint"]
10321037

10331038
@property
10341039
def guild(self) -> Guild | None:

discord/types/raw_models.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@
3333
from .snowflake import Snowflake
3434
from .threads import Thread, ThreadMember
3535
from .user import User
36-
from .voice import (
37-
VoiceState as VoiceStateEvent,
38-
VoiceServerUpdate as VoiceServerUpdateEvent,
39-
)
36+
from .voice import VoiceServerUpdate as VoiceServerUpdateEvent
37+
from .voice import VoiceState as VoiceStateEvent
4038

4139

4240
class _MessageEventOptional(TypedDict, total=False):

discord/voice/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
:license: MIT, see LICENSE for more details.
99
"""
1010

11-
from .client import VoiceClient
1211
from ._types import VoiceProtocol
12+
from .client import VoiceClient
1313

1414
__all__ = (
15-
'VoiceClient',
16-
'VoiceProtocol',
15+
"VoiceClient",
16+
"VoiceProtocol",
1717
)

discord/voice/_types.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2323
DEALINGS IN THE SOFTWARE.
2424
"""
25+
2526
from __future__ import annotations
2627

2728
from typing import TYPE_CHECKING, Generic, TypeVar
@@ -30,11 +31,11 @@
3031
from discord import abc
3132
from discord.client import Client
3233
from discord.raw_models import (
33-
RawVoiceStateUpdateEvent,
3434
RawVoiceServerUpdateEvent,
35+
RawVoiceStateUpdateEvent,
3536
)
3637

37-
ClientT = TypeVar('ClientT', bound='Client', covariant=True)
38+
ClientT = TypeVar("ClientT", bound="Client", covariant=True)
3839

3940

4041
class VoiceProtocol(Generic[ClientT]):

discord/voice/client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2323
DEALINGS IN THE SOFTWARE.
2424
"""
25+
2526
from __future__ import annotations
2627

2728
# rn this is for typing, will be moved here in some point in the future

discord/voice/errors.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2015-2021 Rapptz
5+
Copyright (c) 2021-present Pycord Development
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a
8+
copy of this software and associated documentation files (the "Software"),
9+
to deal in the Software without restriction, including without limitation
10+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
11+
and/or sell copies of the Software, and to permit persons to whom the
12+
Software is furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23+
DEALINGS IN THE SOFTWARE.
24+
"""
25+
26+
from __future__ import annotations
27+
28+
from aiohttp import ClientWebSocketResponse
29+
30+
from discord.errors import ClientException
31+
32+
33+
class VoiceConnectionClosed(ClientException):
34+
"""Exception that's raised when a voice websocket connection
35+
is closed for reasons that could not be handled internally.
36+
37+
Attributes
38+
----------
39+
code: :class:`int`
40+
The close code of the websocket.
41+
reason: :class:`str`
42+
The reason provided for the closure.
43+
guild_id: :class:`int`
44+
The guild ID the client was connected to.
45+
channel_id: :class:`int`
46+
The channel ID the client was connected to.
47+
"""
48+
49+
__slots__ = (
50+
"code",
51+
"reason",
52+
"channel_id",
53+
"guild_id",
54+
)
55+
56+
def __init__(
57+
self,
58+
socket: ClientWebSocketResponse,
59+
channel_id: int,
60+
guild_id: int,
61+
*,
62+
reason: str | None = None,
63+
code: int | None = None,
64+
) -> None:
65+
self.code: int = code or socket.close_code or -1
66+
self.reason: str = reason if reason is not None else ""
67+
self.channel_id: int = channel_id
68+
self.guild_id: int = guild_id
69+
super().__init__(
70+
f"The voice connection on {self.channel_id} (guild {self.guild_id}) was closed with {self.code}",
71+
)
72+
73+
74+
class VoiceGuildMismatch(ClientException):
75+
"""Exception that's raised when, while connecting to a voice channel, the data
76+
the library has differs from the one discord sends.
77+
78+
Attributes
79+
----------
80+
expected: :class:`int`
81+
The expected guild ID. This is the one the library has.
82+
received: :class:`int`
83+
The received guild ID. This is the one sent by discord.
84+
"""
85+
86+
__slots__ = (
87+
"expected",
88+
"received",
89+
)
90+
91+
def __init__(self, expt: int, recv: int) -> None:
92+
self.expected: int = expt
93+
self.received: int = recv

discord/voice/gateway.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2323
DEALINGS IN THE SOFTWARE.
2424
"""
25+
2526
from __future__ import annotations
2627

2728
import asyncio

discord/voice/state.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2323
DEALINGS IN THE SOFTWARE.
2424
"""
25+
2526
from __future__ import annotations
2627

2728
import asyncio

0 commit comments

Comments
 (0)