randydev commited on
Commit
51a244b
·
verified ·
1 Parent(s): d30c25d

Update Akeno/utils/scripts.py

Browse files
Files changed (1) hide show
  1. Akeno/utils/scripts.py +73 -0
Akeno/utils/scripts.py CHANGED
@@ -37,6 +37,79 @@ from pyrogram.types import Message
37
  META_COMMENTS = re.compile(r"^ *# *meta +(\S+) *: *(.*?)\s*$", re.MULTILINE)
38
  interact_with_to_delete = []
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  async def shell_exec(
42
  command: str,
 
37
  META_COMMENTS = re.compile(r"^ *# *meta +(\S+) *: *(.*?)\s*$", re.MULTILINE)
38
  interact_with_to_delete = []
39
 
40
+ def with_args(text: str):
41
+ def decorator(func):
42
+ async def wrapped(client: Client, message: Message):
43
+ if message.text and len(message.text.split()) == 1:
44
+ await message.edit(text)
45
+ else:
46
+ return await func(client, message)
47
+
48
+ return wrapped
49
+
50
+ return decorator
51
+
52
+ def get_args(
53
+ message: Union[Message, str], use_reply: bool = None
54
+ ) -> Tuple[List[str], Dict[str, str]]:
55
+ """Returns list of common args and a dictionary with named args.
56
+
57
+ Args:
58
+ message (Union[Message, str]): Message or text.
59
+
60
+ use_reply (bool, optional): Try to get args from reply message if no args in message. Defaults to None.
61
+
62
+ Returns:
63
+ List[str]: List of args.
64
+ """
65
+ raw_args = get_args_raw(message, use_reply)
66
+
67
+ try:
68
+ args = list(filter(lambda x: len(x) > 0, shlex.split(raw_args)))
69
+ except ValueError:
70
+ return [raw_args], {}
71
+
72
+ common_args = []
73
+ named_args = {}
74
+
75
+ i = 0
76
+ while i < len(args):
77
+ arg = args[i]
78
+ if arg.startswith("-"):
79
+ if i + 1 < len(args) and (
80
+ not args[i + 1].startswith("-") or len(args[i + 1].split()) > 1
81
+ ):
82
+ named_args[arg] = args[i + 1]
83
+ i += 2
84
+ else:
85
+ i += 1
86
+ else:
87
+ i += 1
88
+ common_args.append(arg)
89
+ return common_args, named_args
90
+
91
+ def get_args_raw(message: Union[Message, str], use_reply: bool = None) -> str:
92
+ """Returns text after command.
93
+
94
+ Args:
95
+ message (Union[Message, str]): Message or text.
96
+
97
+ use_reply (bool, optional): Try to get args from reply message if no args in message. Defaults to None.
98
+
99
+ Returns:
100
+ str: Text after command or empty string.
101
+ """
102
+ if isinstance(message, Message):
103
+ text = message.text or message.caption
104
+ args = text.split(maxsplit=1)[1] if len(text.split()) > 1 else ""
105
+
106
+ if use_reply and not args:
107
+ args = message.reply_to_message.text or message.reply_to_message.caption
108
+
109
+ elif not isinstance(message, str):
110
+ return ""
111
+
112
+ return args or ""
113
 
114
  async def shell_exec(
115
  command: str,