Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/core/template.py

189 lines
6.4 KiB
Python
Raw Normal View History

2021-04-07 15:34:54 +00:00
import asyncio
import traceback
import uuid
from os.path import abspath
import aiohttp
2021-02-01 15:13:11 +00:00
import eventlet
from graia.application import MessageChain, GroupMessage, FriendMessage
from graia.application.friend import Friend
from graia.application.group import Group, Member
from graia.application.message.elements.internal import Plain, Image, Source
from graia.broadcast.interrupt import InterruptControl
from graia.broadcast.interrupt.waiter import Waiter
2021-04-02 16:35:26 +00:00
from core.loader import logger_info
2021-02-01 15:13:11 +00:00
from core.broadcast import app, bcc
2021-03-21 08:14:28 +00:00
from database import BotDB
import filetype as ft
from graiax import silkcoder
2021-03-21 08:14:28 +00:00
database = BotDB()
2021-02-01 15:13:11 +00:00
2021-02-03 07:40:17 +00:00
async def sendMessage(kwargs: dict, msgchain, Quote=True):
2021-02-21 15:07:14 +00:00
"""
2021-02-03 07:40:17 +00:00
用于发送一条消息兼容Group和Friend消息
:param kwargs: 函数传入的dict
:param msgchain: 消息链若传入str则自动创建一条带有Plain元素的消息链
:param Quote: 是否引用传入dict中的消息仅对Group消息有效
:return: 被发送的消息链
2021-02-21 15:07:14 +00:00
"""
2021-02-01 15:13:11 +00:00
if isinstance(msgchain, str):
msgchain = MessageChain.create([Plain(msgchain)])
2021-02-19 08:56:57 +00:00
if 'TEST' not in kwargs:
if Quote:
QuoteTarget = kwargs[MessageChain][Source][0].id
else:
QuoteTarget = None
2021-02-01 15:13:11 +00:00
if Group in kwargs:
try:
eventlet.monkey_patch()
with eventlet.Timeout(15):
2021-02-03 07:40:17 +00:00
send = await app.sendGroupMessage(kwargs[Group], msgchain, quote=QuoteTarget)
2021-02-01 15:13:11 +00:00
return send
except eventlet.timeout.Timeout:
split_msg = msgchain.get(Plain)
sent_msgs = []
for msgs in split_msg:
2021-02-01 15:25:57 +00:00
send = await app.sendGroupMessage(kwargs[Group], MessageChain.create([msgs]),
2021-02-03 07:40:17 +00:00
quote=QuoteTarget)
2021-02-01 15:13:11 +00:00
sent_msgs.append(send)
split_img = msgchain.get(Image)
for imgs in split_img:
2021-02-01 15:25:57 +00:00
send = await app.sendGroupMessage(kwargs[Group], MessageChain.create([imgs]),
2021-02-03 07:40:17 +00:00
quote=QuoteTarget)
2021-02-01 15:13:11 +00:00
sent_msgs.append(send)
return sent_msgs
if Friend in kwargs:
try:
eventlet.monkey_patch()
with eventlet.Timeout(15):
send = await app.sendFriendMessage(kwargs[Friend], msgchain)
return send
except eventlet.timeout.Timeout:
split_msg = msgchain.get(Plain)
sent_msgs = []
for msgs in split_msg:
send = await app.sendFriendMessage(kwargs[Friend], MessageChain.create([msgs]))
sent_msgs.append(send)
split_img = msgchain.get(Image)
for imgs in split_img:
send = await app.sendFriendMessage(kwargs[Friend], MessageChain.create([imgs]))
sent_msgs.append(send)
return sent_msgs
2021-02-19 08:56:57 +00:00
if 'TEST' in kwargs:
2021-02-19 12:20:00 +00:00
print(msgchain.asDisplay())
2021-02-01 15:13:11 +00:00
async def wait_confirm(kwargs: dict):
2021-02-21 15:07:14 +00:00
"""
2021-02-03 07:40:17 +00:00
一次性模板用于等待触发对象确认兼容Group和Friend消息
:param kwargs: 函数传入的dict
:return: 若对象发送confirm_command中的其一文本时返回True反之则返回False
2021-02-21 15:07:14 +00:00
"""
2021-02-01 15:13:11 +00:00
inc = InterruptControl(bcc)
confirm_command = ["", "", 'yes', 'y']
if Group in kwargs:
@Waiter.create_using_function([GroupMessage])
def waiter(waiter_group: Group,
waiter_member: Member, waiter_message: MessageChain):
if all([
waiter_group.id == kwargs[Group].id,
waiter_member.id == kwargs[Member].id,
]):
if waiter_message.asDisplay() in confirm_command:
return True
else:
return False
if Friend in kwargs:
@Waiter.create_using_function([FriendMessage])
def waiter(waiter_friend: Friend, waiter_message: MessageChain):
if all([
waiter_friend.id == kwargs[Friend].id,
]):
if waiter_message.asDisplay() in confirm_command:
return True
else:
return False
return await inc.wait(waiter)
2021-03-31 12:57:51 +00:00
async def wait_anything(kwargs: dict):
inc = InterruptControl(bcc)
if Group in kwargs:
@Waiter.create_using_function([GroupMessage])
def waiter(waiter_group: Group,
waiter_member: Member, waiter_message: MessageChain):
if all([
waiter_group.id == kwargs[Group].id,
waiter_member.id == kwargs[Member].id,
]):
return True
if Friend in kwargs:
@Waiter.create_using_function([FriendMessage])
def waiter(waiter_friend: Friend, waiter_message: MessageChain):
if all([
waiter_friend.id == kwargs[Friend].id,
]):
return True
return await inc.wait(waiter)
2021-02-01 15:13:11 +00:00
async def revokeMessage(msgchain):
2021-02-21 15:07:14 +00:00
"""
2021-02-03 07:40:17 +00:00
用于撤回消息
:param msgchain: 需要撤回的已发送/接收的消息链
:return: 无返回
2021-02-21 15:07:14 +00:00
"""
2021-02-01 15:13:11 +00:00
if isinstance(msgchain, list):
for msg in msgchain:
await app.revokeMessage(msg)
else:
await app.revokeMessage(msgchain)
def check_permission(kwargs):
2021-02-21 15:07:14 +00:00
"""
2021-02-03 07:40:17 +00:00
检查对象是否拥有某项权限
:param kwargs: 从函数传入的dict
:return: 若对象为群主管理员或机器人超管则为True
2021-02-21 15:07:14 +00:00
"""
2021-02-01 15:13:11 +00:00
if Group in kwargs:
2021-03-27 15:33:59 +00:00
if str(kwargs[Member].permission) in ['MemberPerm.Administrator', 'MemberPerm.Owner'] or database.check_superuser(
2021-02-01 16:49:08 +00:00
kwargs):
2021-02-01 15:13:11 +00:00
return True
if Friend in kwargs:
2021-03-21 08:14:28 +00:00
if database.check_superuser(kwargs[Friend].id):
2021-02-01 15:13:11 +00:00
return True
2021-02-01 15:25:57 +00:00
return False
2021-03-27 15:33:59 +00:00
async def download_to_cache(link):
try:
async with aiohttp.ClientSession() as session:
async with session.get(link) as resp:
res = await resp.read()
ftt = ft.match(res).extension
path = abspath(f'./cache/{str(uuid.uuid4())}.{ftt}')
with open(path, 'wb+') as file:
file.write(res)
return path
except:
traceback.print_exc()
return False
async def slk_converter(filepath):
filepath2 = filepath + '.silk'
2021-04-07 15:52:24 +00:00
await asyncio.ensure_future(silkcoder.encode(filepath, filepath2), loop=bcc.loop)
return filepath2
2021-03-27 15:33:59 +00:00
async def Nudge(kwargs):
if Group in kwargs:
2021-03-31 12:57:51 +00:00
await app.sendNudge(kwargs[Member], kwargs[Group])