Skip to content

Commit

Permalink
2024.12.12
Browse files Browse the repository at this point in the history
- disco.gateway.client: optional `isal` import speed/resource optimization;
- disco.voice.client: log voice state changes and negotiated encryption mode on `info` channel instead of `debug`;
- disco.state: Optimize state listeners, add `message_create` config toggle for state listeners;
  • Loading branch information
elderlabs committed Dec 13, 2024
1 parent 96d2e80 commit 3623409
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
6 changes: 5 additions & 1 deletion disco/gateway/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
from platform import system as platform_system
from time import time, perf_counter_ns as time_perf_counter_ns
from websocket import ABNF, WebSocketConnectionClosedException, WebSocketTimeoutException
from zlib import decompress as zlib_decompress, decompressobj as zlib_decompressobj

try:
from isal.isal_zlib import decompress as zlib_decompress, decompressobj as zlib_decompressobj
except ImportError:
from zlib import decompress as zlib_decompress, decompressobj as zlib_decompressobj

from disco.gateway.packets import OPCode, RECV, SEND
from disco.gateway.events import GatewayEvent
Expand Down
47 changes: 36 additions & 11 deletions disco/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class StateConfig(Config):
to batch requests using the underlying `GatewayClient.request_guild_members`
interface.
"""
message_create = True
track_messages = False
track_messages_size = 100

Expand Down Expand Up @@ -101,16 +102,7 @@ class State:
messages : Optional[dict(snowflake, deque)]
Mapping of channel ids to dequeue containing `StackMessage` objects.
"""
EVENTS = [
'Ready', 'UserUpdate', 'MessageCreate', 'ChannelCreate', 'ChannelDelete', 'ChannelTopicUpdate', 'ChannelUpdate',
'GuildCreate', 'GuildDelete', 'GuildUpdate', 'GuildEmojisUpdate', 'GuildMemberAdd', 'GuildMemberRemove',
'GuildMembersChunk', 'GuildMemberUpdate', 'GuildRoleCreate', 'GuildRoleDelete', 'GuildRoleUpdate',
'GuildScheduledEventCreate', 'GuildScheduledEventDelete', 'GuildScheduledEventUpdate',
'GuildSoundboardSoundCreate', 'GuildSoundboardSoundDelete', 'GuildSoundboardSoundUpdate', 'GuildStickersUpdate',
'PresenceUpdate', 'StageInstanceCreate', 'StageInstanceDelete', 'StageInstanceUpdate', 'ThreadCreate',
'ThreadDelete', 'ThreadListSync', 'ThreadUpdate', 'VoiceChannelStatusUpdate', 'VoiceServerUpdate',
'VoiceStateUpdate',
]
EVENTS = ['Ready', 'UserUpdate', 'GuildCreate', 'GuildDelete', 'GuildUpdate', 'VoiceServerUpdate']

def __init__(self, client, config):
self.client = client
Expand All @@ -134,7 +126,40 @@ def __init__(self, client, config):
# If message tracking is enabled, listen to those events
if self.config.track_messages:
self.messages = DefaultHashMap(lambda: deque(maxlen=self.config.track_messages_size))
self.EVENTS += ['MessageDelete', 'MessageDeleteBulk']
self.EVENTS += ['MessageDelete', 'MessageDeleteBulk',]

if self.config.message_create:
self.EVENTS += ['MessageCreate',]

if self.config.sync_guild_members:
self.EVENTS += ['GuildMemberAdd', 'GuildMemberRemove', 'GuildMembersChunk', 'GuildMemberUpdate', 'PresenceUpdate',]

if self.config.cache_channels or self.config.cache_dm_channels:
self.EVENTS += ['ChannelCreate', 'ChannelDelete', 'ChannelTopicUpdate', 'ChannelUpdate', 'VoiceChannelStatusUpdate',]

if self.config.cache_threads:
self.EVENTS += ['ThreadCreate', 'ThreadDelete', 'ThreadListSync', 'ThreadUpdate',]

if self.config.cache_roles:
self.EVENTS += ['GuildRoleCreate', 'GuildRoleDelete', 'GuildRoleUpdate',]

if self.config.cache_voice_states:
self.EVENTS += ['VoiceStateUpdate',]

if self.config.cache_emojis:
self.EVENTS += ['GuildEmojisUpdate',]

if self.config.cache_stickers:
self.EVENTS += ['GuildStickersUpdate',]

if self.config.cache_soundboard:
self.EVENTS += ['GuildSoundboardSoundCreate', 'GuildSoundboardSoundDelete', 'GuildSoundboardSoundUpdate',]

if self.config.cache_scheduled_events:
self.EVENTS += ['GuildScheduledEventCreate', 'GuildScheduledEventDelete', 'GuildScheduledEventUpdate',]

if self.config.cache_stage_instances:
self.EVENTS += ['StageInstanceCreate', 'StageInstanceDelete', 'StageInstanceUpdate',]

# The bound listener objects
self.listeners = []
Expand Down
4 changes: 2 additions & 2 deletions disco/voice/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def ssrc_rtcp(self):
return self.ssrc + 3

def set_state(self, state):
self.log.debug('[{}] state {} -> {}'.format(self.channel_id or '-', self.state, state))
self.log.info('[{}] state {} -> {}'.format(self.channel_id or '-', self.state, state))
prev_state = self.state
self.state = state
self.state_emitter.emit(state, prev_state)
Expand Down Expand Up @@ -333,7 +333,7 @@ def on_voice_ready(self, data):
for mode in self.enc_modes:
if mode in self.SUPPORTED_MODES:
self.mode = mode
self.log.debug('[{}] Selected mode {}'.format(self.channel_id, mode))
self.log.info('[{}] Selected mode {}'.format(self.channel_id, mode))
break
else:
raise Exception('Failed to find a supported voice mode')
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
gevent==24.10.2
gevent==24.11.1
requests==2.32.3
websocket-client==1.8.0

0 comments on commit 3623409

Please sign in to comment.