瀏覽代碼

Errors not go brr

TheHamkerCat 5 年之前
父節點
當前提交
83513ac734

+ 10 - 0
wbb/modules/admin.py

@@ -3,6 +3,7 @@ from pyrogram.types import Message, ChatPermissions
 from wbb import OWNER_ID, SUDO_USER_ID, app
 from wbb.utils import cust_filter
 from wbb.utils.botinfo import BOT_ID
+from wbb.utils.errors import capture_err
 
 __MODULE__ = "Admin"
 __HELP__ = '''/ban - Ban A User
@@ -40,6 +41,7 @@ async def list_members(group_id):
 
 
 @app.on_message(cust_filter.command(commands=("purge")) & ~filters.edited)
+@capture_err
 async def purge(client, message: Message):
     message_ids = []
     if message.chat.type not in ("supergroup", "channel"):
@@ -90,6 +92,7 @@ async def purge(client, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=("kick")) & ~filters.edited)
+@capture_err
 async def kick(_, message: Message):
     try:
         if (await app.get_chat_member(
@@ -131,6 +134,7 @@ async def kick(_, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=("ban")) & ~filters.edited)
+@capture_err
 async def ban(_, message: Message):
     try:
         if (await app.get_chat_member(
@@ -171,6 +175,7 @@ async def ban(_, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=("unban")) & ~filters.edited)
+@capture_err
 async def unban(_, message: Message):
     try:
         if (await app.get_chat_member(
@@ -205,6 +210,7 @@ async def unban(_, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=("del")))
+@capture_err
 async def delete(_, message: Message):
     try:
         admins = await list_admins(message.chat.id)
@@ -230,6 +236,7 @@ async def delete(_, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=("promote")) & ~filters.edited)
+@capture_err
 async def promote(_, message: Message):
     try:
         admins = await list_admins(message.chat.id)
@@ -280,6 +287,7 @@ async def promote(_, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=("pin")) & ~filters.edited)
+@capture_err
 async def pin(_, message: Message):
     try:
         admins = await list_admins(message.chat.id)
@@ -305,6 +313,7 @@ async def pin(_, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=("mute")) & ~filters.edited)
+@capture_err
 async def mute(_, message: Message):
     try:
         chat_id = message.chat.id
@@ -331,6 +340,7 @@ async def mute(_, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=("unmute")) & ~filters.edited)
+@capture_err
 async def unmute(_, message: Message):
     try:
         chat_id = message.chat.id

+ 1 - 1
wbb/modules/eval.py

@@ -59,7 +59,7 @@ async def executor(client, message):
         evaluation = stdout
     else:
         evaluation = "Success"
-    final_output = (f"**OUTPUT**:\n```{evaluation.strip()}``")
+    final_output = (f"**OUTPUT**:\n```{evaluation.strip()}```")
     if len(final_output) > 4096:
         filename = "output.txt"
         with open(filename, "w+", encoding="utf8") as out_file:

+ 3 - 0
wbb/modules/f.py

@@ -2,6 +2,8 @@ from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
 from wbb import app
 from pyrogram import filters
 from wbb.modules.admin import list_admins
+from wbb.utils.errors import capture_err
+
 i = 0
 m = None
 pressers = []
@@ -16,6 +18,7 @@ keyboard = InlineKeyboardMarkup(
 
 
 @app.on_message(filters.command("f"))
+@capture_err
 async def f(_, message):
     global i, m, pressers
     i = 0

+ 3 - 0
wbb/modules/github.py

@@ -1,12 +1,15 @@
 import aiohttp
 from pyrogram import filters
 from wbb import app
+from wbb.utils.errors import capture_err
+
 
 __MODULE__ = 'Github'
 __HELP__ = "Get information about a GitHub user.\n /github [username]"
 
 
 @app.on_message(filters.command('github'))
+@capture_err
 async def github(_, message):
     if len(message.command) != 2:
         await message.reply_text("/git Username")

+ 5 - 7
wbb/modules/greetings.py

@@ -13,24 +13,21 @@ import asyncio
 from datetime import datetime
 from pyrogram import filters
 from wbb import app, WELCOME_DELAY_KICK_SEC
+from wbb.utils.errors import capture_err
 from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
 from pyrogram.types import ChatPermissions
 from pyrogram.errors.exceptions.bad_request_400 import UserNotParticipant, ChatAdminRequired
-
+from pyrogram.errors.exceptions.forbidden_403 import 
 
 @app.on_message(filters.new_chat_members)
+@capture_err
 async def welcome(_, message: Message):
     """Mute new member and send message with button"""
     new_members = [f"{u.mention}" for u in message.new_chat_members]
     text = (f"Welcome, {', '.join(new_members)}\n**Are you human?**\n"
             "You will be removed from this chat if you are not verified "
             f"in {WELCOME_DELAY_KICK_SEC} seconds")
-    try:
-        await message.chat.restrict_member(message.from_user.id, ChatPermissions())
-    except ChatAdminRequired:
-        msg = "Yeah, I Can't Verify Newly Joined Members, Make Sure I'm Admin."
-        await message.reply_text(msg)
-        return
+    await message.chat.restrict_member(message.from_user.id, ChatPermissions())
 
     button_message = await message.reply(
         text,
@@ -82,6 +79,7 @@ async def kick_restricted_after_delay(delay, button_message: Message):
 
 
 @app.on_message(filters.left_chat_member)
+@capture_err
 async def left_chat_member(_, message: Message):
     """When a restricted member left the chat, ban him for a while"""
     group_chat = message.chat

+ 3 - 0
wbb/modules/images.py

@@ -7,6 +7,7 @@ from pyrogram import filters
 from pyrogram.types import Message
 from wbb.utils import cust_filter
 from wbb import app, WALL_API_KEY
+from wbb.utils.errors import capture_err
 
 __MODULE__ = "Images"
 __HELP__ = '''/cat  - Get Cute Cats Images
@@ -19,6 +20,7 @@ async def delete_message_with_delay(delay, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=("cat")) & ~filters.edited)
+@capture_err
 async def cat(_, message: Message):
     with urllib.request.urlopen(
         "https://api.thecatapi.com/v1/images/search"
@@ -29,6 +31,7 @@ async def cat(_, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=("wall")) & ~filters.edited)
+@capture_err
 async def wall(_, message: Message):
     if len(message.command) < 2:
         await message.reply_text("/wall needs an argument")

+ 12 - 0
wbb/modules/misc.py

@@ -7,6 +7,7 @@ from googletrans import Translator
 from cryptography.fernet import Fernet
 from wbb import app, FERNET_ENCRYPTION_KEY
 from wbb.utils import cust_filter, random_line
+from wbb.utils.errors import capture_err
 
 __MODULE__ = "Misc"
 __HELP__ = '''/commit - Generate Funny Commit Messages
@@ -25,10 +26,12 @@ __HELP__ = '''/commit - Generate Funny Commit Messages
 
 
 @app.on_message(cust_filter.command(commands=("commit")) & ~filters.edited)
+@capture_err
 async def commit(_, message: Message):
     await message.reply_text((await random_line('wbb/utils/commit.txt')))
 
 @app.on_message(filters.command("RTFM", "#"))
+@capture_err
 async def rtfm(_, message):
     await message.delete()
     if not message.reply_to_message:
@@ -38,11 +41,13 @@ async def rtfm(_, message):
 
 
 @app.on_message(cust_filter.command(commands=("runs")) & ~filters.edited)
+@capture_err
 async def runs(_, message: Message):
     await message.reply_text((await random_line('wbb/utils/runs.txt')))
 
 
 @app.on_message(cust_filter.command(commands=("id")) & ~filters.edited)
+@capture_err
 async def get_id(_, message: Message):
     if len(message.command) != 1:
         username = message.text.split(None, 1)[1]
@@ -65,6 +70,7 @@ async def get_id(_, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=('random')) & ~filters.edited)
+@capture_err
 async def random(_, message: Message):
     if len(message.command) != 2:
         await message.reply_text('"/random" Needs An Argurment.'
@@ -88,6 +94,7 @@ async def random(_, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=('encrypt')) & ~filters.edited)
+@capture_err
 async def encrypt(_, message: Message):
     if not message.reply_to_message:
         await message.reply_text('Reply To A Message To Encrypt It.')
@@ -103,6 +110,7 @@ async def encrypt(_, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=('decrypt')) & ~filters.edited)
+@capture_err
 async def decrypt(_, message: Message):
     if not message.reply_to_message:
         await message.reply_text('Reply To A Message To Decrypt It.')
@@ -119,6 +127,7 @@ async def decrypt(_, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=("cheat")) & ~filters.edited)
+@capture_err
 async def cheat(_, message: Message):
     text = message.text.split(None, 1)[1]
     ftext = text.split()
@@ -140,6 +149,7 @@ async def cheat(_, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=("weather")) & ~filters.edited)
+@capture_err
 async def weather(_, message: Message):
     city = message.text.split(None, 1)[1]
     if len(message.command) != 2:
@@ -153,6 +163,7 @@ async def weather(_, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=("tr")) & ~filters.edited)
+@capture_err
 async def tr(_, message: Message):
     lang = message.text.split(None, 1)[1]
     if not message.reply_to_message or lang == "":
@@ -169,6 +180,7 @@ async def tr(_, message: Message):
 
 
 @app.on_message(filters.command('bun'))
+@capture_err
 async def bunn(_, message):
     if message.reply_to_message:
         await message.reply_to_message.reply_sticker('CAACAgUAAx0CWIlO9AABARyRYBhyjKXFATVhu7AGQwip3TzSFiMAAuMBAAJ7usBUIu2xBtXTmuweBA')

+ 2 - 0
wbb/modules/music.py

@@ -9,6 +9,7 @@ from pyrogram import filters
 from pyrogram.types import Message
 from wbb.utils import cust_filter
 from wbb import app, OWNER_ID, SUDO_USER_ID
+from wbb.utils.errors import capture_err
 
 SUDOERS = [OWNER_ID, SUDO_USER_ID]
 
@@ -27,6 +28,7 @@ ydl_opts = {
 
 
 @app.on_message(cust_filter.command(commands=("music")) & ~filters.edited & filters.user(SUDOERS))
+@capture_err
 async def music(_, message: Message):
     if len(message.command) != 2:
         await message.reply_text("`/music` needs a link as argument")

+ 2 - 0
wbb/modules/paste.py

@@ -2,6 +2,7 @@ import os
 from pyrogram import filters
 from pyrogram.types import Message
 from wbb.utils import cust_filter, nekobin
+from wbb.utils.errors import capture_err
 from wbb import app
 
 __MODULE__ = "Paste"
@@ -9,6 +10,7 @@ __HELP__ = "/paste - To Paste Replied Text Or Document To Neokobin"
 
 
 @app.on_message(cust_filter.command(commands=("paste")) & ~filters.edited)
+@capture_err
 async def paste(_, message: Message):
     if bool(message.reply_to_message) is True:
         app.set_parse_mode("markdown")

+ 2 - 1
wbb/modules/ping.py

@@ -4,13 +4,14 @@ from pyrogram.types import Message
 from wbb import app
 from wbb.utils import cust_filter
 from wbb.utils.botinfo import BOT_DC_ID
-
+from wbb.utils.errors import capture_err
 
 __MODULE__ = "Ping"
 __HELP__ = " /ping - To Get Response Time From All TG Datacenters"
 
 
 @app.on_message(cust_filter.command(commands=("ping")) & ~filters.edited)
+@capture_err
 async def ping(_, message: Message):
     datacenter = (f"https://cdn{BOT_DC_ID}.telesco.pe")
     ping1 = round(requests.head(datacenter).elapsed.total_seconds() * 1000)

+ 2 - 0
wbb/modules/repo.py

@@ -2,6 +2,7 @@ from pyrogram import filters
 from pyrogram.types import Message
 from wbb import app
 from wbb.utils import cust_filter
+from wbb.utils.errors import capture_err
 
 __MODULE__ = "Repo"
 __HELP__ = "/repo - To Get My Github Repository Link " \
@@ -9,6 +10,7 @@ __HELP__ = "/repo - To Get My Github Repository Link " \
 
 
 @app.on_message(cust_filter.command(commands=("repo")) & ~filters.edited)
+@capture_err
 async def repo(_, message: Message):
     await message.reply_text(
         "[Github](https://github.com/thehamkercat/WilliamButcherBot)"

+ 2 - 0
wbb/modules/rice.py

@@ -2,6 +2,7 @@ from pyrogram import filters
 from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
 from pyrogram.types import InputMediaPhoto, InputMediaVideo
 from wbb import app
+from wbb.utils.errors import capture_err
 
 RICE_GROUP = "DE_WM"
 RICE_CHANNEL = "RiceGallery"
@@ -12,6 +13,7 @@ RICE_CHANNEL = "RiceGallery"
                 & filters.regex(r"^\[RICE\] ")
                 & ~filters.forwarded
                 & ~filters.edited)
+@capture_err
 async def rice(_, message: Message):
     """Forward media and media_group messages which has caption starts
     with [RICE] with space and description in RICE_GROUP to RICE_CHANNEL

+ 6 - 0
wbb/modules/search.py

@@ -5,6 +5,7 @@ from pyrogram import filters
 from requests import get
 from wbb import app
 from wbb.utils import cust_filter
+from wbb.utils.errors import capture_err
 
 __MODULE__ = "Search"
 __HELP__ = '''/ud - Search For Something In Urban Dictionary
@@ -15,6 +16,7 @@ __HELP__ = '''/ud - Search For Something In Urban Dictionary
 
 
 @app.on_message(cust_filter.command(commands=("ud")) & ~filters.edited)
+@capture_err
 async def urbandict(_, message: Message):
     if len(message.command) < 2:
         await message.reply_text('"/ud" Needs An Argument.')
@@ -40,6 +42,7 @@ async def urbandict(_, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=("google")) & ~filters.edited)
+@capture_err
 async def google(_, message: Message):
     try:
         if len(message.command) < 2:
@@ -66,6 +69,7 @@ async def google(_, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=("so")) & ~filters.edited)
+@capture_err
 async def stack(_, message: Message):
     try:
         if len(message.command) < 2:
@@ -93,6 +97,7 @@ async def stack(_, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=("gh")) & ~filters.edited)
+@capture_err
 async def github(_, message: Message):
     try:
         if len(message.command) < 2:
@@ -120,6 +125,7 @@ async def github(_, message: Message):
 
 
 @app.on_message(cust_filter.command(commands=("yt")) & ~filters.edited)
+@capture_err
 async def ytsearch(_, message: Message):
     try:
         if len(message.command) < 2:

+ 5 - 1
wbb/modules/succ.py

@@ -3,9 +3,11 @@ from pyrogram.types import Message
 from pyrogram import filters
 from wbb import app
 from wbb.utils import cust_filter
+from wbb.utils.errors import capture_err
 
 __MODULE__ = "Succ"
-__HELP__ = "/succ - Sends Succ Image For A Given Argument"
+__HELP__ = """/succ - Sends Succ Image For A Given Argument
+/reddit [query] - results something from reddit"""
 
 hack = [
     "https://i.ibb.co/YLLhCtt/i.png",
@@ -43,6 +45,7 @@ def suck(text):
 
 
 @app.on_message(cust_filter.command(commands=("succ")) & ~filters.edited)
+@capture_err
 def succ(_, message: Message):
     if len(message.command) != 2:
         message.reply_text(
@@ -63,6 +66,7 @@ phijiks, welth, smrt`"""
 
 
 @app.on_message(cust_filter.command(commands=("reddit")) & ~filters.edited)
+@capture_err
 async def reddit(_, message: Message):
     app.set_parse_mode("html")
     if len(message.command) != 2:

+ 5 - 0
wbb/modules/sudoers.py

@@ -8,6 +8,7 @@ from pyrogram.types import Message
 import speedtest
 import psutil
 from wbb.utils import cust_filter, nekobin, formatter
+from wbb.utils.errors import capture_err
 from wbb import app, OWNER_ID, SUDO_USER_ID, bot_start_time, NEOFETCH, JSMAPI
 
 __MODULE__ = "Sudoers"
@@ -23,6 +24,7 @@ SUDOERS = [OWNER_ID, SUDO_USER_ID]
 
 
 @app.on_message(filters.user(SUDOERS) & cust_filter.command("log"))
+@capture_err
 async def logs_chat(_, message: Message):
     keyb = types.InlineKeyboardMarkup(
         [
@@ -78,6 +80,7 @@ def speed_convert(size):
 @app.on_message(
     filters.user(SUDOERS) & cust_filter.command(commands=("speedtest"))
 )
+@capture_err
 async def get_speedtest_result(_, message: Message):
     m = await message.reply_text("`Performing A Speedtest!`")
     speed = speedtest.Speedtest()
@@ -96,6 +99,7 @@ Latency  - {round((i["latency"]))} ms
 @app.on_message(
     filters.user(SUDOERS) & cust_filter.command(commands=("stats"))
 )
+@capture_err
 async def get_stats(_, message: Message):
     bot_uptime = int(time.time() - bot_start_time)
     cpu = psutil.cpu_percent(interval=0.5)
@@ -131,6 +135,7 @@ async def get_stats(_, message: Message):
 @app.on_message(
     filters.user(SUDOERS) & cust_filter.command(commands=("song"))
 )
+@capture_err
 async def song(_, message: Message):
     if len(message.command) < 2:
         await message.reply_text("/song requires an argument.")

+ 48 - 0
wbb/utils/errors.py

@@ -0,0 +1,48 @@
+import sys
+import traceback
+from functools import wraps
+from wbb import app, OWNER_ID
+
+def split_limits(text):
+    if len(text) < 2048:
+        return [text]
+
+    lines = text.splitlines(True)
+    small_msg = ''
+    result = []
+    for line in lines:
+        if len(small_msg) + len(line) < 2048:
+            small_msg += line
+        else:
+            result.append(small_msg)
+            small_msg = line
+    else:
+        result.append(small_msg)
+
+    return result
+
+def capture_err(func):
+    @wraps(func)
+    async def capture(client, message, *args, **kwargs):
+        try:
+            return await func(client, message, *args, **kwargs)
+        except Exception as err:
+            exc_type, exc_obj, exc_tb = sys.exc_info()
+            errors = traceback.format_exception(
+                etype=exc_type, value=exc_obj, tb=exc_tb,
+            )
+            error_feedback = split_limits(
+                '**ERROR** | `{}` | `{}`\n\n```{}```\n\n```{}```\n'.format(
+                    0 if not message.from_user else message.from_user.id,
+                    0 if not message.chat else message.chat.id,
+                    message.text or message.caption,
+                    ''.join(errors),
+                ),
+            )
+            for x in error_feedback:
+                await app.send_message(
+                    OWNER_ID,
+                    x
+                )
+            raise err
+    return capture