1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
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 |
|
|
|
|
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, |
|
|
|
|
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
|
|
|
|