From 742f29a6e821680bd755bd71a5ed4c93c43e3f58 Mon Sep 17 00:00:00 2001 From: CMHopeSunshine <277073121@qq.com> Date: Sun, 25 Sep 2022 18:05:57 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20`=E7=BD=91=E9=A1=B5=E6=88=AA?= =?UTF-8?q?=E5=9B=BE`=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=AE=E9=A1=B9?= =?UTF-8?q?=EF=BC=8C=E5=8F=AF=E8=AE=BE=E4=B8=BA=E4=BB=85=E8=B6=85=E7=BA=A7?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=8F=AF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LittlePaimon/manager/plugin_manager/model.py | 2 + LittlePaimon/plugins/tools/__init__.py | 43 ++++++++++++-------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/LittlePaimon/manager/plugin_manager/model.py b/LittlePaimon/manager/plugin_manager/model.py index 839c384..3b2cd26 100644 --- a/LittlePaimon/manager/plugin_manager/model.py +++ b/LittlePaimon/manager/plugin_manager/model.py @@ -69,3 +69,5 @@ class Config(BaseModel): auto_add_friend: bool = Field(False, alias='自动接受好友请求') auto_add_group: bool = Field(False, alias='自动接受群邀请') notice_event: bool = Field(True, alias='启用好友和群欢迎消息') + + screenshot_enable: bool = Field(True, alias='启用网页截图权限') diff --git a/LittlePaimon/plugins/tools/__init__.py b/LittlePaimon/plugins/tools/__init__.py index 6edfb34..dfb672e 100644 --- a/LittlePaimon/plugins/tools/__init__.py +++ b/LittlePaimon/plugins/tools/__init__.py @@ -1,10 +1,19 @@ from nonebot import on_command from nonebot.params import CommandArg +from nonebot.rule import Rule from nonebot.adapters.onebot.v11 import Message, MessageEvent, MessageSegment from nonebot.plugin import PluginMetadata - +from LittlePaimon import SUPERUSERS +from LittlePaimon.manager.plugin_manager import plugin_manager as pm from LittlePaimon.utils.brower import get_browser + +async def permission_check(event: MessageEvent) -> bool: + if pm.config.screenshot_enable: + return True + return event.user_id not in SUPERUSERS and event.sender.role not in ['admin', 'owner'] + + __plugin_meta__ = PluginMetadata( name='实用工具', description='一些实用的工具插件', @@ -16,7 +25,7 @@ __plugin_meta__ = PluginMetadata( } ) -screenshot_cmd = on_command('网页截图', priority=10, block=True, state={ +screenshot_cmd = on_command('网页截图', priority=10, block=True, rule=Rule(permission_check), state={ 'pm_name': '网页截图', 'pm_description': '对指定链接页面进行截图,例:【网页截图www.baidu.com】', 'pm_usage': '网页截图<链接>', @@ -35,25 +44,27 @@ baidu_cmd = on_command('百度一下', priority=10, block=True, state={ async def _(event: MessageEvent, msg: Message = CommandArg()): await screenshot_cmd.send('正在尝试截图,请稍等...') url = msg.extract_plain_text().split(' ')[0] - brower = await get_browser() - page = await brower.new_page() - await page.goto(url, wait_until='networkidle') - if page is None: - await screenshot_cmd.finish('截图失败,无法访问该网页') - img = await page.screenshot(full_page=True) - await screenshot_cmd.finish(MessageSegment.image(img)) + try: + brower = await get_browser() + page = await brower.new_page() + await page.goto(url, wait_until='networkidle') + img = await page.screenshot(full_page=True) + await screenshot_cmd.send(MessageSegment.image(img)) + except Exception: + await screenshot_cmd.finish('截图失败,无法访问该网页,请稍后再试') @baidu_cmd.handle() async def _(event: MessageEvent, msg: Message = CommandArg()): await baidu_cmd.send('正在为你百度,请稍等...') keyword = msg.extract_plain_text() - brower = await get_browser() - page = await brower.new_page() - await page.goto(f'https://www.baidu.com/s?wd={keyword}', wait_until='networkidle', timeout=10000) - if page is None: + try: + brower = await get_browser() + page = await brower.new_page() + await page.goto(f'https://www.baidu.com/s?wd={keyword}', wait_until='networkidle', timeout=15000) + context = await page.query_selector('#content_left') + img = await context.screenshot() + await baidu_cmd.send(MessageSegment.image(img)) + except Exception: await baidu_cmd.finish('百度失败,请稍后再试') - context = await page.query_selector('#content_left') - img = await context.screenshot() - await baidu_cmd.finish(MessageSegment.image(img))