网页截图增加配置项,可设为仅超级用户可用

This commit is contained in:
CMHopeSunshine 2022-09-25 18:05:57 +08:00
parent b4fb8d0441
commit 742f29a6e8
2 changed files with 29 additions and 16 deletions

View File

@ -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='启用网页截图权限')

View File

@ -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))