pincer.middleware.voice_state_update   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 23
dl 0
loc 57
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A export() 0 2 1
A voice_state_update_middleware() 0 31 5
1
# Copyright Pincer 2021-Present
2
# Full MIT License can be found in `LICENSE` at the project root.
3
4
"""
5
sent when a user's voice state changes in a subscribed voice channel
6
(mute, volume, etc.)
7
"""
8
from __future__ import annotations
9
10
from typing import TYPE_CHECKING
11
12
from ..objects.user.voice_state import VoiceState
13
14
if TYPE_CHECKING:
15
    from typing import List, Tuple
16
17
    from ..client import Client
18
    from ..core.gateway import Gateway
19
    from ..core.gateway import GatewayDispatch
20
21
22
async def voice_state_update_middleware(
23
    self: Client, gateway: Gateway, payload: GatewayDispatch
0 ignored issues
show
Unused Code introduced by
The argument gateway seems to be unused.
Loading history...
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
24
) -> Tuple[str, List[VoiceState]]:
25
    """|coro|
26
    Middleware for the ``on_voice_state_update`` event.
27
28
    Parameters
29
    ----------
30
    payload : :class:`~pincer.core.gateway.GatewayDispatch`
31
        The data received from the voice state update event.
32
    gateway : :class:`~pincer.core.gateway.Gateway`
33
        The gateway for the current shard.
34
35
    Returns
36
    -------
37
    Tuple[:class:`str`, :class:`~pincer.objects.user.voice_state.VoiceState`]
38
        ``on_voice_state_update`` and a ``VoiceState``
39
    """  # noqa: E501
40
41
    voice_state = VoiceState.from_dict(payload.data)
42
    guild = self.guilds.get(voice_state.guild_id)
43
44
    if guild:
45
        for index, state in enumerate(guild.voice_states):
46
            if state.user_id == voice_state.user_id:
47
                guild.voice_states[index] = voice_state
48
                break
49
        else:
50
            guild.voice_states.append(voice_state)
51
52
    return "on_voice_state_update", voice_state
53
54
55
def export():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
56
    return voice_state_update_middleware
57