Passed
Push — main ( d0b3ac...149404 )
by
unknown
01:45
created

pincer.middleware.interaction_create   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 30
dl 0
loc 75
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B interaction_create_middleware() 0 44 7
1
# -*- coding: utf-8 -*-
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# MIT License
3
#
4
# Copyright (c) 2021 Pincer
5
#
6
# Permission is hereby granted, free of charge, to any person obtaining
7
# a copy of this software and associated documentation files
8
# (the "Software"), to deal in the Software without restriction,
9
# including without limitation the rights to use, copy, modify, merge,
10
# publish, distribute, sublicense, and/or sell copies of the Software,
11
# and to permit persons to whom the Software is furnished to do so,
12
# subject to the following conditions:
13
#
14
# The above copyright notice and this permission notice shall be
15
# included in all copies or substantial portions of the Software.
16
#
17
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
from pincer.commands import ChatCommandHandler
0 ignored issues
show
Bug introduced by
The name commands does not seem to exist in module pincer.
Loading history...
introduced by
Cannot import 'pincer.commands' due to syntax error 'invalid syntax (<unknown>, line 89)'
Loading history...
25
from pincer.core.dispatch import GatewayDispatch
26
from pincer.objects import Interaction, Embed, Message, InteractionFlags
27
from pincer.utils import MISSING, should_pass_cls
28
from pincer.utils.extraction import get_params
29
30
31
async def interaction_create_middleware(self, payload: GatewayDispatch):
32
    """
33
    Middleware for ``on_interaction``, which handles command
34
    execution.
35
36
    :param self:
37
        The current client.
38
39
    :param payload:
40
        The data received from the interaction event.
41
    """
42
    interaction: Interaction = Interaction.from_dict(payload.data)
43
    command = ChatCommandHandler.register.get(interaction.data.name)
44
45
    if command:
46
        defaults = {param: None for param in get_params(command.call)}
47
        params = {}
48
49
        if interaction.data.options is not MISSING:
50
            params = {
51
                opt.name: opt.value for opt in interaction.data.options
52
            }
53
54
        kwargs = {**defaults, **params}
55
56
        if should_pass_cls(command.call):
57
            kwargs["self"] = self
58
59
        message = await command.call(**kwargs)
60
61
        if isinstance(message, Embed):
62
            message = Message(embeds=[message])
63
        elif not isinstance(message, Message):
64
            message = Message(message) if message else Message(
65
                self.__received,
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like __received was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
66
                flags=InteractionFlags.EPHEMERAL
67
            )
68
69
        await self.http.post(
70
            f"interactions/{interaction.id}/{interaction.token}/callback",
71
            message.to_dict()
72
        )
73
74
    return "on_interaction_create", [interaction]
75