Repository: Rapptz/discord.py
File: discord/gateway.py (Python)

Code before:
                if data['transition_id'] == 0:
                    await state._execute_transition(data['transition_id'])
                else:
                    if data['protocol_version'] == 0 and state.dave_session:
                        state.dave_session.set_passthrough_mode(True, 120)

                    await self.send_transition_ready(data['transition_id'])
            elif op == self.DAVE_EXECUTE_TRANSITION:
                _log.debug('Executing DAVE transition id %d', data['transition_id'])
                await state._execute_transition(data['transition_id'])
            elif op == self.DAVE_PREPARE_EPOCH:
                _log.debug('Preparing for DAVE epoch %d', data['epoch'])
                # When the epoch ID is equal to 1, this message indicates that a new MLS group is to be created for the given protocol version.
                if data['epoch'] == 1:
                    state.dave_protocol_version = data['protocol_version']
                    await state.reinit_dave_session()

        await self._hook(self, msg)

    async def recieved_binary_message(self, msg: bytes) -> None:
        self.seq_ack = struct.unpack_from('>H', msg, 0)[0]
        op = msg[2]
        _log.debug('Voice websocket binary frame received: %d bytes; seq=%s op=%s', len(msg), self.seq_ack, op)
        state = self._connection

        if state.dave_session:
            if op == self.MLS_EXTERNAL_SENDER:
                state.dave_session.set_external_sender(msg[3:])
                _log.debug('Set MLS external sender')
            elif op == self.MLS_PROPOSALS:
                optype = msg[3]
                result = state.dave_session.process_proposals(
                    davey.ProposalsOperationType.append if optype == 0 else davey.ProposalsOperationType.revoke, msg[4:]
                )
                if isinstance(result, davey.CommitWelcome):
                    await self.send_binary(
                        DiscordVoiceWebSocket.MLS_COMMIT_WELCOME,
                        result.commit + result.welcome if result.welcome else result.commit,
                    )
                _log.debug('MLS proposals processed')
            elif op == self.MLS_ANNOUNCE_COMMIT_TRANSITION:
                transition_id = struct.unpack_from('>H', msg, 3)[0]
                try:
                    state.dave_session.process_commit(msg[5:])
                    if transition_id != 0:
                        state.dave_pending_transitions[transition_id] = state.dave_protocol_version
                        await self.send_transition_ready(transition_id)
                    _log.debug('MLS commit processed for transition id %d', transition_id)
                except Exception:
                    await state._recover_from_invalid_commit(transition_id)
            elif op == self.MLS_WELCOME:

Review comment (suggestion):
Prefer early return here to prevent excessive nesting, i.e. `if state.dave_session is None:` followed by a `return`.

Code after:
                if data['transition_id'] == 0:
                    await state._execute_transition(data['transition_id'])
                else:
                    if data['protocol_version'] == 0 and state.dave_session:
                        state.dave_session.set_passthrough_mode(True, 120)

                    await self.send_transition_ready(data['transition_id'])
            elif op == self.DAVE_EXECUTE_TRANSITION:
                _log.debug('Executing DAVE transition id %d', data['transition_id'])
                await state._execute_transition(data['transition_id'])
            elif op == self.DAVE_PREPARE_EPOCH:
                _log.debug('Preparing for DAVE epoch %d', data['epoch'])
                # When the epoch ID is equal to 1, this message indicates that a new MLS group is to be created for the given protocol version.
                if data['epoch'] == 1:
                    state.dave_protocol_version = data['protocol_version']
                    await state.reinit_dave_session()

        await self._hook(self, msg)

    async def received_binary_message(self, msg: bytes) -> None:
        self.seq_ack = struct.unpack_from('>H', msg, 0)[0]
        op = msg[2]
        _log.debug('Voice websocket binary frame received: %d bytes; seq=%s op=%s', len(msg), self.seq_ack, op)
        state = self._connection

        if state.dave_session is None:
            return

        if op == self.MLS_EXTERNAL_SENDER:
            state.dave_session.set_external_sender(msg[3:])
            _log.debug('Set MLS external sender')
        elif op == self.MLS_PROPOSALS:
            optype = msg[3]
            result = state.dave_session.process_proposals(
                davey.ProposalsOperationType.append if optype == 0 else davey.ProposalsOperationType.revoke, msg[4:]
            )
            if isinstance(result, davey.CommitWelcome):
                await self.send_binary(
                    DiscordVoiceWebSocket.MLS_COMMIT_WELCOME,
                    result.commit + result.welcome if result.welcome else result.commit,
                )
            _log.debug('MLS proposals processed')
        elif op == self.MLS_ANNOUNCE_COMMIT_TRANSITION:
            transition_id = struct.unpack_from('>H', msg, 3)[0]
            try:
                state.dave_session.process_commit(msg[5:])
                if transition_id != 0:
                    state.dave_pending_transitions[transition_id] = state.dave_protocol_version
                    await self.send_transition_ready(transition_id)
                _log.debug('MLS commit processed for transition id %d', transition_id)
            except Exception: