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/MessageGen.py

100 lines
4.3 KiB
Python
Raw Normal View History

2020-08-20 05:15:24 +00:00
import asyncio
2020-08-30 08:26:54 +00:00
import random
import re
from os.path import abspath
import graia.application.interrupt as inter
2020-08-12 16:01:34 +00:00
from graia.application.message.chain import MessageChain
2020-09-19 11:01:27 +00:00
from graia.application.message.elements.internal import Plain, Image, Source
2020-08-12 16:01:34 +00:00
2020-08-07 06:19:34 +00:00
from CommandGen import command
2020-08-18 13:12:38 +00:00
from modules.findimage import findimage
2020-08-12 16:01:34 +00:00
2020-09-19 10:35:13 +00:00
2020-08-30 08:26:54 +00:00
async def gen(bcc, app, message, target1, target2='0', msgtype='None'):
im = inter.InterruptControl(bcc)
2020-09-19 06:21:06 +00:00
if msgtype == 'Group':
run = await command(message.asDisplay(), target1.id)
2020-08-30 08:37:51 +00:00
else:
run = await command(message.asDisplay())
2020-09-19 10:35:13 +00:00
# print(run)
2020-08-07 06:19:34 +00:00
if run != None:
2020-08-30 10:08:26 +00:00
print(run)
2020-08-30 08:26:54 +00:00
msgchain = await makemsgchain(run, msgtype)
2020-09-19 10:35:13 +00:00
send = await sendmessage(app, msgchain, target1, target2, msgtype,
message[Source][0] if msgtype == 'Group' else 0)
2020-09-19 06:21:06 +00:00
'''
if msgtype == 'group':
voice = re.findall(r'https?://.*?/File:.*?\.(?:ogg|m4a|mp3|flac|wav)', run, re.I)
for x in voice:
y = re.match(r'(https?://.*?/)File:(.*?\.(?:ogg|m4a|mp3|flac|wav))', x, re.I)
z = await dfile(y.group(1), y.group(2))
c = await camr(z)
voicemsgchain = MessageChain.create(Voice.fromExternal(abspath(c)))
await app.sendGroupMessage(group, voicemsgchain)
'''
2020-08-20 10:33:40 +00:00
if run.find('[一分钟后撤回本消息]') != -1:
2020-08-20 05:15:24 +00:00
await asyncio.sleep(60)
await app.revokeMessage(send)
2020-08-31 13:26:34 +00:00
if run.find('[30秒后撤回本消息]') != -1:
2020-08-20 10:33:40 +00:00
await asyncio.sleep(30)
await app.revokeMessage(send)
2020-08-30 08:26:54 +00:00
if run.find('[wait]') != -1:
ranint = random.randint(1, 3)
if ranint == 2:
2020-09-07 13:13:23 +00:00
waitmsg = await makemsgchain('提示:你可以发送“是”字来将所有无效结果再次查询。(考虑到实现复杂性,恕不提供选择性查询)', msgtype)
await sendmessage(app, waitmsg, target1, target2, msgtype)
2020-09-19 06:21:06 +00:00
MessageEventImport = __import__('graia.application', fromlist=[f'{msgtype}Message'])
MessageEvent = getattr(MessageEventImport, f'{msgtype}Message')
2020-09-19 10:35:13 +00:00
InterruptImport = __import__('graia.application.interrupt.interrupts',
fromlist=[f'{msgtype}MessageInterrupt'])
2020-09-19 06:21:06 +00:00
Interrupt = getattr(InterruptImport, f'{msgtype}MessageInterrupt')
if msgtype == 'Friend':
event: MessageEvent = await im.wait(Interrupt(target1.id))
else:
event: MessageEvent = await im.wait(Interrupt(target1, target2))
2020-08-30 08:26:54 +00:00
print(event)
if event.messageChain.asDisplay() == '':
msg2 = await command(run)
msgchain = await makemsgchain(msg2, msgtype)
2020-08-31 13:15:50 +00:00
await sendmessage(app, msgchain, target1, target2, msgtype)
2020-08-30 08:26:54 +00:00
else:
pass
async def makemsgchain(msg, msgtype):
msg = re.sub('\[wait\]', '', msg)
2020-09-19 11:01:27 +00:00
exec('from graia.application.message.elements.internal import UploadMethods')
2020-09-20 04:31:15 +00:00
mth = eval(f'UploadMethods.{msgtype}')
2020-08-30 08:26:54 +00:00
if msg.find('[[usn:') != -1:
user = re.sub(r'.*\[\[usn:|\]\]', '', msg)
msg = re.sub(r'\[\[.*\]\]', '', msg)
msgchain = MessageChain.create(
[Plain(msg)])
msgchain = msgchain.plusWith(
[Image.fromLocalFile(filepath=abspath(f"./assests/usercard/{user}.png"), method=mth)])
else:
msgchain = MessageChain.create(
[Plain(msg)])
r = re.findall(r'(https?://.*?/File:.*?\.(?:png|gif|jpg|jpeg|webp|bmp|ico))', msg, re.I)
for d in r:
d1 = await findimage(d)
print(d1)
msgchain = msgchain.plusWith([Image.fromNetworkAddress(url=d1, method=mth)])
return msgchain
2020-08-31 13:15:50 +00:00
async def sendmessage(app, msgchain, target1, target2, msgtype, quoteid=0):
2020-09-19 06:21:06 +00:00
if msgtype == 'Friend':
2020-08-31 13:15:50 +00:00
friend = target1
send = await app.sendFriendMessage(friend, msgchain.asSendable())
2020-09-19 06:21:06 +00:00
if msgtype == 'Group':
2020-08-31 13:15:50 +00:00
group = target1
send = await app.sendGroupMessage(group, msgchain.asSendable(), quote=quoteid if quoteid != 0 else None)
2020-09-19 06:21:06 +00:00
if msgtype == 'Temp':
2020-08-31 13:15:50 +00:00
group = target1
member = target2
send = await app.sendTempMessage(group=group, target=member, message=msgchain.asSendable())
2020-09-19 10:35:13 +00:00
return send