forked from IsHimanshu/aioaria2-mirror-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.py
executable file
·153 lines (118 loc) · 3.81 KB
/
command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from typing import (
TYPE_CHECKING,
Any,
Callable,
Coroutine,
Optional,
Sequence,
Union,
)
import pyrogram
from pyrogram.filters import Filter
if TYPE_CHECKING:
from .core import Bot
CommandFunc = Union[Callable[..., Coroutine[Any, Any, None]],
Callable[..., Coroutine[Any, Any, Optional[str]]]]
Decorator = Callable[[CommandFunc], CommandFunc]
def desc(_desc: str) -> Decorator:
"""Sets description on a command function."""
def desc_decorator(func: CommandFunc) -> CommandFunc:
setattr(func, "_cmd_description", _desc)
return func
return desc_decorator
def usage(_usage: str, optional: bool = False) -> Decorator:
"""Sets argument usage help on a command function."""
def usage_decorator(func: CommandFunc) -> CommandFunc:
setattr(func, "_cmd_usage", _usage)
setattr(func, "_cmd_usage_optional", optional)
return func
return usage_decorator
def alias(*aliases: str) -> Decorator:
"""Sets aliases on a command function."""
def alias_decorator(func: CommandFunc) -> CommandFunc:
setattr(func, "_cmd_aliases", aliases)
return func
return alias_decorator
def filters(_filters: Filter) -> Decorator:
"""Sets filters on a command function."""
def filter_decorator(func: CommandFunc) -> CommandFunc:
setattr(func, "_cmd_filters", _filters)
return func
return filter_decorator
class Command:
name: str
desc: Optional[str]
usage: Optional[str]
usage_optional: bool
aliases: Sequence[str]
filters: Optional[Filter]
plugin: Any
func: CommandFunc
def __init__(self, name: str, plugin: Any, func: CommandFunc) -> None:
self.name = name
self.desc = getattr(func, "_cmd_description", None)
self.usage = getattr(func, "_cmd_usage", None)
self.usage_optional = getattr(func, "_cmd_usage_optional", False)
self.aliases = getattr(func, "_cmd_aliases", [])
self.filters = getattr(func, "_cmd_filters", None)
self.plugin = plugin
self.func = func
# Command invocation context
class Context:
bot: "Bot"
msg: pyrogram.types.Message
cmd_len: int
response: pyrogram.types.Message
input: str
args: Sequence[str]
segments: Sequence[str]
invoker: str
def __init__(
self,
bot: "Bot",
msg: pyrogram.types.Message,
cmd_len: int,
) -> None:
self.bot = bot
self.msg = msg
self.cmd_len = cmd_len
# Response message to be filled later
self.response = None # type: ignore
# Single argument string
username = self.bot.user.username
if username in self.msg.text:
self.input = self.msg.text[self.cmd_len + 1 + len(username) :]
else:
self.input = self.msg.text[self.cmd_len :]
self.segments = self.msg.command
self.invoker = self.segments[0]
# Lazily resolve expensive fields
def __getattr__(self, name: str) -> Any:
if name == "args":
return self._get_args()
raise AttributeError(
f"'{type(self).__name__}' object has no attribute '{name}'"
)
# Argument segments
def _get_args(self) -> Sequence[str]:
self.args = self.segments[1:]
return self.args
# Wrapper for Bot.respond()
async def respond(
self,
text: str,
*,
mode: str = "edit",
redact: bool = True,
msg: Optional[pyrogram.types.Message] = None,
**kwargs: Any,
) -> Optional[pyrogram.types.Message]:
self.response = await self.bot.respond(
msg or self.msg,
text,
mode=mode,
redact=redact,
response=self.response,
**kwargs,
)
return self.response