mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-06 11:57:17 +00:00
Reformat state.py
This commit is contained in:
parent
d5033b04a2
commit
311eac97b0
@ -80,8 +80,16 @@ if TYPE_CHECKING:
|
||||
CS = TypeVar('CS', bound='ConnectionState')
|
||||
Channel = Union[GuildChannel, VocalGuildChannel, PrivateChannel, PartialMessageable]
|
||||
|
||||
|
||||
class ChunkRequest:
|
||||
def __init__(self, guild_id: int, loop: asyncio.AbstractEventLoop, resolver: Callable[[int], Any], *, cache: bool = True) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
guild_id: int,
|
||||
loop: asyncio.AbstractEventLoop,
|
||||
resolver: Callable[[int], Any],
|
||||
*,
|
||||
cache: bool = True,
|
||||
) -> None:
|
||||
self.guild_id: int = guild_id
|
||||
self.resolver: Callable[[int], Any] = resolver
|
||||
self.loop: asyncio.AbstractEventLoop = loop
|
||||
@ -120,21 +128,33 @@ class ChunkRequest:
|
||||
if not future.done():
|
||||
future.set_result(self.buffer)
|
||||
|
||||
|
||||
log: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def logging_coroutine(coroutine: Coroutine[Any, Any, T], *, info: str) -> Optional[T]:
|
||||
try:
|
||||
await coroutine
|
||||
except Exception:
|
||||
log.exception('Exception occurred during %s', info)
|
||||
|
||||
|
||||
class ConnectionState:
|
||||
if TYPE_CHECKING:
|
||||
_get_websocket: Callable[..., DiscordWebSocket]
|
||||
_get_client: Callable[..., Client]
|
||||
_parsers: Dict[str, Callable[[Dict[str, Any]], None]]
|
||||
|
||||
def __init__(self, *, dispatch: Callable, handlers: Dict[str, Callable], hooks: Dict[str, Callable], http: HTTPClient, loop: asyncio.AbstractEventLoop, **options: Any) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
dispatch: Callable,
|
||||
handlers: Dict[str, Callable],
|
||||
hooks: Dict[str, Callable],
|
||||
http: HTTPClient,
|
||||
loop: asyncio.AbstractEventLoop,
|
||||
**options: Any,
|
||||
) -> None:
|
||||
self.loop: asyncio.AbstractEventLoop = loop
|
||||
self.http: HTTPClient = http
|
||||
self.max_messages: Optional[int] = options.get('max_messages', 1000)
|
||||
@ -454,7 +474,9 @@ class ConnectionState:
|
||||
|
||||
return channel or PartialMessageable(state=self, id=channel_id), guild
|
||||
|
||||
async def chunker(self, guild_id: int, query: str = '', limit: int = 0, presences: bool = False, *, nonce: Optional[str] = None) -> None:
|
||||
async def chunker(
|
||||
self, guild_id: int, query: str = '', limit: int = 0, presences: bool = False, *, nonce: Optional[str] = None
|
||||
) -> None:
|
||||
ws = self._get_websocket(guild_id) # This is ignored upstream
|
||||
await ws.request_chunks(guild_id, query=query, limit=limit, presences=presences, nonce=nonce)
|
||||
|
||||
@ -469,7 +491,9 @@ class ConnectionState:
|
||||
|
||||
try:
|
||||
# start the query operation
|
||||
await ws.request_chunks(guild_id, query=query, limit=limit, user_ids=user_ids, presences=presences, nonce=request.nonce)
|
||||
await ws.request_chunks(
|
||||
guild_id, query=query, limit=limit, user_ids=user_ids, presences=presences, nonce=request.nonce
|
||||
)
|
||||
return await asyncio.wait_for(request.wait(), timeout=30.0)
|
||||
except asyncio.TimeoutError:
|
||||
log.warning('Timed out waiting for chunks with query %r and limit %d for guild_id %d', query, limit, guild_id)
|
||||
@ -853,10 +877,7 @@ class ConnectionState:
|
||||
else:
|
||||
previous_threads = guild._filter_threads(channel_ids)
|
||||
|
||||
threads = {
|
||||
d['id']: guild._store_thread(d)
|
||||
for d in data.get('threads', [])
|
||||
}
|
||||
threads = {d['id']: guild._store_thread(d) for d in data.get('threads', [])}
|
||||
|
||||
for member in data.get('members', []):
|
||||
try:
|
||||
@ -1101,7 +1122,9 @@ class ConnectionState:
|
||||
|
||||
# do a cleanup of the messages cache
|
||||
if self._messages is not None:
|
||||
self._messages: Optional[Deque[Message]] = deque((msg for msg in self._messages if msg.guild != guild), maxlen=self.max_messages)
|
||||
self._messages: Optional[Deque[Message]] = deque(
|
||||
(msg for msg in self._messages if msg.guild != guild), maxlen=self.max_messages
|
||||
)
|
||||
|
||||
self._remove_guild(guild)
|
||||
self.dispatch('guild_remove', guild)
|
||||
@ -1368,9 +1391,12 @@ class ConnectionState:
|
||||
if channel is not None:
|
||||
return channel
|
||||
|
||||
def create_message(self, *, channel: Union[TextChannel, Thread, DMChannel, GroupChannel, PartialMessageable], data: MessagePayload) -> Message:
|
||||
def create_message(
|
||||
self, *, channel: Union[TextChannel, Thread, DMChannel, GroupChannel, PartialMessageable], data: MessagePayload
|
||||
) -> Message:
|
||||
return Message(state=self, channel=channel, data=data)
|
||||
|
||||
|
||||
class AutoShardedConnectionState(ConnectionState):
|
||||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||
super().__init__(*args, **kwargs)
|
||||
@ -1390,7 +1416,16 @@ class AutoShardedConnectionState(ConnectionState):
|
||||
# channel will either be a TextChannel, Thread or Object
|
||||
msg._rebind_cached_references(new_guild, channel) # type: ignore
|
||||
|
||||
async def chunker(self, guild_id: int, query: str = '', limit: int = 0, presences: bool = False, *, shard_id: Optional[int] = None, nonce: Optional[str] = None) -> None:
|
||||
async def chunker(
|
||||
self,
|
||||
guild_id: int,
|
||||
query: str = '',
|
||||
limit: int = 0,
|
||||
presences: bool = False,
|
||||
*,
|
||||
shard_id: Optional[int] = None,
|
||||
nonce: Optional[str] = None,
|
||||
) -> None:
|
||||
ws = self._get_websocket(guild_id, shard_id=shard_id)
|
||||
await ws.request_chunks(guild_id, query=query, limit=limit, presences=presences, nonce=nonce)
|
||||
|
||||
@ -1435,9 +1470,9 @@ class AutoShardedConnectionState(ConnectionState):
|
||||
try:
|
||||
await utils.sane_wait_for(futures, timeout=timeout)
|
||||
except asyncio.TimeoutError:
|
||||
log.warning('Shard ID %s failed to wait for chunks (timeout=%.2f) for %d guilds', shard_id,
|
||||
timeout,
|
||||
len(guilds))
|
||||
log.warning(
|
||||
'Shard ID %s failed to wait for chunks (timeout=%.2f) for %d guilds', shard_id, timeout, len(guilds)
|
||||
)
|
||||
for guild in children:
|
||||
if guild.unavailable is False:
|
||||
self.dispatch('guild_available', guild)
|
||||
|
Loading…
x
Reference in New Issue
Block a user