浏览代码

Better task cancellation interface

TheHamkerCat 5 年之前
父节点
当前提交
20007aade0
共有 6 个文件被更改,包括 95 次插入 和 26 次删除
  1. 2 2
      wbb/core/keyboard.py
  2. 27 19
      wbb/core/tasks.py
  3. 7 0
      wbb/modules/inline.py
  4. 0 3
      wbb/modules/userbot.py
  5. 1 1
      wbb/utils/functions.py
  6. 58 1
      wbb/utils/inlinefuncs.py

+ 2 - 2
wbb/core/keyboard.py

@@ -37,9 +37,9 @@ def keyboard(buttons_list, row_width: int = 2):
     buttons = InlineKeyboard(row_width=row_width)
     data = [
         (
-            Ikb(text=i[0], callback_data=i[1])
+            Ikb(text=str(i[0]), callback_data=str(i[1]))
             if not is_url(i[1])
-            else Ikb(text=i[0], url=i[1])
+            else Ikb(text=str(i[0]), url=str(i[1]))
         )
         for i in buttons_list
     ]

+ 27 - 19
wbb/core/tasks.py

@@ -4,12 +4,12 @@ from time import time
 from pyrogram import filters
 from pyrogram.types import Message
 
-from wbb import SUDOERS, USERBOT_PREFIX, app2, eor
+from wbb import BOT_ID, SUDOERS, USERBOT_PREFIX, app, app2, eor
 from wbb.core.sections import bold, section, w
 
 tasks = {}
 TASKS_LOCK = Lock()
-arrow = lambda x: x.text + "\n`→`"
+arrow = lambda x: (x.text if x else "") + "\n`→`"
 
 
 def all_tasks():
@@ -73,22 +73,10 @@ async def task_cancel(_, message: Message):
     await eor(message, text=f"{arrow(m)} Task cancelled")
 
 
-@app2.on_message(
-    filters.user(SUDOERS)
-    & ~filters.forwarded
-    & ~filters.via_bot
-    & filters.command("lsTasks", prefixes=USERBOT_PREFIX)
-)
-async def task_list(_, message: Message):
+async def _get_tasks_text():
     await rm_task()  # Clean completed tasks
-
-    tasks = all_tasks()
-
     if not tasks:
-        return await eor(
-            message,
-            text=f"{arrow(message)} No tasks pending",
-        )
+        return f"{arrow('')} No pending task"
 
     text = bold("Tasks") + "\n"
 
@@ -100,7 +88,6 @@ async def task_list(_, message: Message):
         info = t._repr_info()
 
         id = task[0]
-
         text += section(
             f"{indent}Task {i}",
             body={
@@ -110,7 +97,28 @@ async def task_list(_, message: Message):
                 "Origin": info[2].split("/")[-1].replace(">", ""),
                 "Running since": f"{elapsed}s",
             },
-            indent=6,
+            indent=8,
         )
+    return text
+
 
-    await eor(message, text=text)
+@app2.on_message(
+    filters.user(SUDOERS)
+    & ~filters.forwarded
+    & ~filters.via_bot
+    & filters.command("lsTasks", prefixes=USERBOT_PREFIX)
+)
+async def task_list(_, message: Message):
+    if message.from_user.is_self:
+        await message.delete()
+
+    results = await app2.get_inline_bot_results(
+            BOT_ID,
+            f"tasks",
+    )
+    await app2.send_inline_bot_result(
+        message.chat.id,
+        results.query_id,
+        results.results[0].id,
+        hide_via=True,
+    )

+ 7 - 0
wbb/modules/inline.py

@@ -273,6 +273,13 @@ async def inline_query_handler(client, query):
 
         elif text.split()[0] == "exec":
             await execute_code(query)
+    
+        elif text.strip() == "tasks":
+            user_id = query.from_user.id
+            answerss = await task_inline_func(user_id)
+            await client.answer_inline_query(
+                query.id, results=answerss, cache_time=1
+            )
 
     except Exception as e:
         e = traceback.format_exc()

+ 0 - 3
wbb/modules/userbot.py

@@ -122,9 +122,6 @@ async def executor(client, message: Message):
         evaluation = stderr
     elif stdout:
         evaluation = stdout
-        # Save the last stdout in globals,
-        # Can use the it in next calls
-        globals()["lstdout"] = stdout
     else:
         evaluation = "Success"
 

+ 1 - 1
wbb/utils/functions.py

@@ -168,7 +168,7 @@ def get_urls_from_text(text: str) -> bool:
                 [.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(
                 \([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\
                 ()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))""".strip()
-    return [x[0] for x in findall(regex, text)]
+    return [x[0] for x in findall(regex, str(text))]
 
 
 async def time_converter(message: Message, time_value: str) -> int:

+ 58 - 1
wbb/utils/inlinefuncs.py

@@ -35,7 +35,7 @@ from pykeyboard import InlineKeyboard
 from pyrogram import __version__ as pyrover
 from pyrogram import filters
 from pyrogram.raw.functions import Ping
-from pyrogram.types import (InlineKeyboardButton,
+from pyrogram.types import (CallbackQuery, InlineKeyboardButton,
                             InlineQueryResultArticle,
                             InlineQueryResultPhoto,
                             InputTextMessageContent)
@@ -43,6 +43,8 @@ from search_engine_parser import GoogleSearch
 
 from wbb import (BOT_USERNAME, MESSAGE_DUMP_CHAT, SUDOERS, USERBOT_ID,
                  USERBOT_NAME, USERBOT_USERNAME, app, app2, arq)
+from wbb.core.keyboard import ikb
+from wbb.core.tasks import _get_tasks_text, all_tasks, rm_task
 from wbb.core.types import InlineQueryResultCachedDocument
 from wbb.modules.info import get_chat_info, get_user_info
 from wbb.modules.music import download_youtube_audio
@@ -862,3 +864,58 @@ async def execute_code(query):
                 )
             )
     await query.answer(results=answers, cache_time=1)
+
+
+async def task_inline_func(user_id):
+    if user_id not in SUDOERS:
+        return
+
+    tasks = all_tasks()
+    text = await _get_tasks_text()
+    keyb = None
+
+    if tasks:
+        keyb = ikb(
+            {i: f"cancel_task_{i}" for i in list(tasks.keys())}
+        )
+
+    return [
+        InlineQueryResultArticle(
+            title="Tasks",
+            reply_markup=keyb,
+            input_message_content=InputTextMessageContent(
+                text,
+            ),
+        )
+    ]
+
+
+@app.on_callback_query(filters.regex("^cancel_task_"))
+async def cancel_task_button(_, query: CallbackQuery):
+    user_id = query.from_user.id
+
+    if user_id not in SUDOERS:
+        return await query.answer("This is not for you.")
+
+    task_id = int(query.data.split("_")[-1])
+    await rm_task(task_id)
+
+    tasks = all_tasks()
+    text = await _get_tasks_text()
+    keyb = None
+
+    if tasks:
+        keyb = ikb(
+            {i: f"cancel_task_{i}" for i in list(tasks.keys())}
+        )
+
+    await app.edit_inline_text(
+        query.inline_message_id,
+        text,
+    )
+
+    if keyb:
+        await app.edit_inline_reply_markup(
+            query.inline_message_id,
+            keyb,
+        )