From cfd94e667a2436e1d31ec2211c6ed62739ede605 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Fri, 2 Jul 2021 20:04:08 +0800 Subject: [PATCH] fix some bugs --- dme.py | 12 ++++++++---- eat.py | 5 +++++ list.json | 16 ++++++++-------- nthmsg.py | 8 ++++++-- rape.py | 6 +++++- throwit.py | 5 +++++ vip.py | 6 ++++-- whois.py | 6 +++++- yb-dl.py | 13 +++++++++---- 9 files changed, 55 insertions(+), 22 deletions(-) diff --git a/dme.py b/dme.py index 6c7c1e0..5a96a00 100644 --- a/dme.py +++ b/dme.py @@ -2,7 +2,7 @@ from asyncio import sleep from os import path, remove from os.path import exists -from PIL import Image +from PIL import Image, UnidentifiedImageError from pagermaid import redis, log, redis_status from pagermaid.listener import listener from pagermaid.utils import alias_command @@ -20,14 +20,18 @@ async def dme(context): remove('plugins/dme.jpg') target_file = reply.photo await context.client.download_media( - await context.get_reply_message(), file = "plugins/dme.jpg" + await context.get_reply_message(), file="plugins/dme.jpg" ) await context.edit("替换图片设置完成。") elif reply and reply.sticker: if exists('plugins/dme.jpg'): remove('plugins/dme.jpg') - await context.client.download_media(reply.media.document, file = "plugins/dme.webp") - im = Image.open("plugins/dme.webp") + await context.client.download_media(reply.media.document, file="plugins/dme.webp") + try: + im = Image.open("plugins/dme.webp") + except UnidentifiedImageError: + await context.edit("替换图片设置发生错误。") + return im.save('plugins/dme.png', "png") remove('plugins/dme.webp') target_file = await context.client.upload_file('plugins/dme.png') diff --git a/eat.py b/eat.py index b4b5a7b..24f7222 100644 --- a/eat.py +++ b/eat.py @@ -7,6 +7,7 @@ from requests import get from random import randint from telethon.tl.functions.users import GetFullUserRequest from telethon.tl.types import MessageEntityMentionName +from telethon.errors.rpcerrorlist import ChatSendStickersForbiddenError from struct import error as StructError from pagermaid.listener import listener from pagermaid.utils import alias_command @@ -138,6 +139,8 @@ async def eat(context): return except TypeError: await context.edit("此用户未设置头像或头像对您不可见。") + except ChatSendStickersForbiddenError: + await context.edit("此群组无法发送贴纸。") else: try: await context.client.send_file( @@ -155,3 +158,5 @@ async def eat(context): return except TypeError: await context.edit("此用户未设置头像或头像对您不可见。") + except ChatSendStickersForbiddenError: + await context.edit("此群组无法发送贴纸。") diff --git a/list.json b/list.json index e80308b..65394c4 100644 --- a/list.json +++ b/list.json @@ -12,7 +12,7 @@ }, { "name": "dme", - "version": "1.11", + "version": "1.12", "section": "chat", "maintainer": "xtaodada", "size": "4.3 kb", @@ -72,7 +72,7 @@ }, { "name": "yb-dl", - "version": "1.01", + "version": "1.02", "section": "daily", "maintainer": "xtaodada", "size": "18.8 kb", @@ -82,7 +82,7 @@ }, { "name": "throwit", - "version": "1.611", + "version": "1.612", "section": "profile", "maintainer": "xtaodada", "size": "6.9 kb", @@ -142,7 +142,7 @@ }, { "name": "nthmsg", - "version": "1.01", + "version": "1.02", "section": "chat", "maintainer": "seiuneko", "size": "0.9 kb", @@ -192,7 +192,7 @@ }, { "name": "whois", - "version": "1.13", + "version": "1.131", "section": "daily", "maintainer": "KorenKrita", "size": "1.1 kb", @@ -202,7 +202,7 @@ }, { "name": "eat", - "version": "1.02", + "version": "1.03", "section": "profile", "maintainer": "SF_PICK", "size": "6.4 kb", @@ -342,7 +342,7 @@ }, { "name": "vip", - "version": "0.3", + "version": "0.31", "section": "chat", "maintainer": "xtaodada", "size": "4.0 kb", @@ -492,7 +492,7 @@ }, { "name": "rape", - "version": "1.02", + "version": "1.03", "section": "chat", "maintainer": "Pentacene", "size": "4.0 kb", diff --git a/nthmsg.py b/nthmsg.py index 9cc9313..ebafe56 100644 --- a/nthmsg.py +++ b/nthmsg.py @@ -17,6 +17,10 @@ async def nthmsg(context): m = object() async for m in context.client.iter_messages(context.chat_id, from_user="me", reverse=True, limit=n): pass - r = await context.client( - functions.channels.ExportMessageLinkRequest(channel=m.to_id, id=m.id, grouped=True)) + try: + r = await context.client( + functions.channels.ExportMessageLinkRequest(channel=m.to_id, id=m.id, grouped=True)) + except AttributeError: + await context.edit('获取失败。') + return await context.edit(r.link) diff --git a/rape.py b/rape.py index 72ba26c..b971e3e 100644 --- a/rape.py +++ b/rape.py @@ -63,7 +63,11 @@ async def rape(context): if context.arguments == '': return else: - userid = int(context.arguments) + try: + userid = int(context.arguments) + except ValueError: + await context.edit('无法识别的账号 id 。') + return admins = await context.client.get_participants(context.chat, filter=ChannelParticipantsAdmins) if context.sender in admins: try: diff --git a/throwit.py b/throwit.py index b1b2ae7..f5250ba 100644 --- a/throwit.py +++ b/throwit.py @@ -7,6 +7,7 @@ from requests import get from random import randint from telethon.tl.functions.users import GetFullUserRequest from telethon.tl.types import MessageEntityMentionName +from telethon.errors.rpcerrorlist import ChatSendStickersForbiddenError from struct import error as StructError from pagermaid.listener import listener from pagermaid.utils import alias_command @@ -158,6 +159,8 @@ async def throwit(context): return except TypeError: await context.edit("此用户未设置头像或头像对您不可见。") + except ChatSendStickersForbiddenError: + await context.edit("此群组无法发送贴纸。") else: try: await context.client.send_file( @@ -174,3 +177,5 @@ async def throwit(context): return except TypeError: await context.edit("此用户未设置头像或头像对您不可见。") + except ChatSendStickersForbiddenError: + await context.edit("此群组无法发送贴纸。") diff --git a/vip.py b/vip.py index f2cb29b..01e416d 100644 --- a/vip.py +++ b/vip.py @@ -79,13 +79,15 @@ async def pixiv(context): pixiv_albums = pixiv_list[1].split('|||') pixiv_album = [] await context.edit("下载图片中 . . .") - for i in range(0, len(pixiv_albums)): + if len(pixiv_albums) > 8: + await context.edit('获取的图片数大于 8 ,将只发送前8张图片,下载图片中 . . .') + for i in range(0, min(len(pixiv_albums), 8)): r = get(pixiv_albums[i]) with open("pixiv." + str(i) + ".jpg", "wb") as code: code.write(r.content) pixiv_album.extend(["pixiv." + str(i) + ".jpg"]) await context.client.send_file(context.chat_id, pixiv_album, - caption=pixiv_list[0]) + caption=pixiv_list[0]) await context.delete() for i in pixiv_album: try: diff --git a/whois.py b/whois.py index 17d5b15..e36c642 100644 --- a/whois.py +++ b/whois.py @@ -16,7 +16,11 @@ async def whois(context): return req = get("https://tenapi.cn/whois/?url=" + message) if req.status_code == 200: - data = json.loads(req.text)['data'] + try: + data = json.loads(req.text)['data'] + except KeyError: + await context.edit("出错了呜呜呜 ~ 可能是域名不正确。") + return res = '域名: `' + data['url'] + '`\n注册商: `' + str(data['registrar']) + '`\n联系人: `' + str( data['registrant']) + '`\n联系邮箱: `' + str(data['mail']) + '`\n注册时间: `' + str( data['registration']) + '`\n过期时间: `' + str(data['expiration']) + '`\nDNS: ' + str(data['dns']).replace( diff --git a/yb-dl.py b/yb-dl.py index 9b148cd..2bd0747 100644 --- a/yb-dl.py +++ b/yb-dl.py @@ -3,6 +3,7 @@ from os import remove from os.path import exists from youtube_dl import YoutubeDL +from youtube_dl.utils import DownloadError from re import compile as regex_compile from pagermaid import bot, log from pagermaid.listener import listener @@ -26,16 +27,20 @@ async def ybdl(context): bilibili_pattern = regex_compile(r"^(http(s)?://)?((w){3}.)?bilibili(\.com)?/.+") youtube_pattern = regex_compile(r"^(http(s)?://)?((w){3}.)?youtu(be|.be)?(\.com)?/.+") if youtube_pattern.match(url): - if not await fetch_video(url, context.chat_id, reply_id): - await context.edit("出错了呜呜呜 ~ 视频下载失败。") - await log(f"已拉取UTB视频,地址: {url}.") - await context.edit("视频获取成功!") + try: + if not await fetch_video(url, context.chat_id, reply_id): + await context.edit("出错了呜呜呜 ~ 视频下载失败。") + await log(f"已拉取UTB视频,地址: {url}.") + await context.edit("视频获取成功!") + except DownloadError: + await context.edit("视频下载失败,可能是视频受到 DRM 保护。") if bilibili_pattern.match(url): if not await fetch_video(url, context.chat_id, reply_id): await context.edit("出错了呜呜呜 ~ 视频下载失败。") await log(f"已拉取 Bilibili 视频,地址: {url}.") await context.edit("视频获取成功!") + async def fetch_video(url, chat_id, reply_id): """ Extracts and uploads YouTube video. """ youtube_dl_options = {