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

155 lines
6.4 KiB
Python
Raw Normal View History

2020-08-20 05:15:24 +00:00
import asyncio
2020-09-20 11:11:11 +00:00
import os
2020-08-30 08:26:54 +00:00
import random
import re
2020-10-11 07:49:09 +00:00
import traceback
2020-08-30 08:26:54 +00:00
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-10-11 07:49:09 +00:00
from graia.application.message.elements.internal import Plain, Image, Source
2020-08-12 16:01:34 +00:00
2020-10-11 07:49:09 +00:00
from modules.findimage import findimage
try:
cachepath = abspath('./assets/cache/')
cachefile = os.listdir(cachepath)
for file in cachefile:
os.remove(f'{cachepath}/{file}')
except Exception:
pass
2020-10-11 07:49:09 +00:00
2020-09-26 05:37:55 +00:00
async def gen(bcc, app, message, target1, target2='0', msgtype='None', runfun='command'):
2020-08-30 08:26:54 +00:00
im = inter.InterruptControl(bcc)
2020-10-11 07:49:09 +00:00
command = __import__('CommandGen', fromlist=[runfun])
2020-09-26 05:37:55 +00:00
command = getattr(command, runfun)
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-10-11 07:49:09 +00:00
# print(run)
2020-08-07 06:19:34 +00:00
if run != None:
2020-10-11 07:49:09 +00:00
await msgproc(run, app, im, command, message, target1, target2, msgtype)
async def msgproc(resultmsgraw, app, im, command, message, target1, target2='0', msgtype='None'):
print(resultmsgraw)
msgchain = await makemsgchain(resultmsgraw)
send = await sendmessage(app, msgchain, target1, target2, msgtype,
message[Source][0] if msgtype == 'Group' else 0)
2020-10-12 13:37:05 +00:00
uimgcs = re.findall(r'\[\[uimgc:.*\]\]', resultmsgraw, re.I | re.M)
for uimgc in uimgcs:
uimgc = re.match(r'\[\[uimgc:(.*)\]\]', uimgc)
if uimgc:
await uimgsend(app, message, target1, target2, msgtype, uimgc.group(1))
2020-10-11 07:49:09 +00:00
r = re.findall(r'(https?://.*?/File:.*?\.(?:png|gif|jpg|jpeg|webp|bmp|ico))', resultmsgraw, re.I)
for d in r:
d1 = await findimage(d)
if d1 is not None:
await linkimgsend(app, d1, target1, target2, msgtype)
await afterproc(resultmsgraw, app, im, command, send, message, target1, target2, msgtype)
async def makemsgchain(msg):
2020-08-30 08:26:54 +00:00
msg = re.sub('\[wait\]', '', msg)
msgbase = re.sub(r'\[\[uimgc:.*\]\]', '', msg)
msgchain = MessageChain.create([Plain(msgbase)])
2020-10-11 07:49:09 +00:00
return msgchain
async def afterproc(resultmsgraw, app, im, command, send, message, target1, target2='0', msgtype='None'):
if resultmsgraw.find('[一分钟后撤回本消息]') != -1:
await asyncio.sleep(60)
await app.revokeMessage(send)
if resultmsgraw.find('[30秒后撤回本消息]') != -1:
await asyncio.sleep(30)
await app.revokeMessage(send)
if resultmsgraw.find('[wait]') != -1:
ranint = random.randint(1, 3)
if ranint == 2:
waitmsg = await makemsgchain('提示:你可以发送“是”字来将所有无效结果再次查询。(考虑到实现复杂性,恕不提供选择性查询)')
await sendmessage(app, waitmsg, target1, target2, msgtype)
MessageEventImport = __import__('graia.application', fromlist=[f'{msgtype}Message'])
MessageEvent = getattr(MessageEventImport, f'{msgtype}Message')
InterruptImport = __import__('graia.application.interrupt.interrupts',
fromlist=[f'{msgtype}MessageInterrupt'])
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))
print(event)
if event.messageChain.asDisplay() == '':
msg2 = await command(resultmsgraw)
2020-10-11 10:36:07 +00:00
await msgproc(msg2, app, im, command, message, target1, target2, msgtype)
2020-10-11 07:49:09 +00:00
else:
pass
2020-10-12 13:37:05 +00:00
async def uimgsend(app, message, target1, target2, msgtype, link):
2020-10-11 07:49:09 +00:00
exec('from graia.application.message.elements.internal import UploadMethods')
mth = eval(f'UploadMethods.{msgtype}')
2020-10-12 13:37:05 +00:00
try:
msgchain = MessageChain.create(
[Image.fromLocalFile(filepath=abspath(link), method=mth)])
print('Sending Image...')
await sendmessage(app, msgchain, target1, target2, msgtype,
message[Source][0] if msgtype == 'Group' else 0)
except Exception:
traceback.print_exc()
msgchain = MessageChain.create(
[Plain('上传过程中遇到了问题,图片发送失败。')])
await sendmessage(app, msgchain, target1, target2, msgtype,
message[Source][0] if msgtype == 'Group' else 0)
2020-10-11 07:49:09 +00:00
2020-10-11 10:34:09 +00:00
async def linkimgsend(app, sendlink, target1, target2, msgtype):
2020-10-11 07:49:09 +00:00
exec('from graia.application.message.elements.internal import UploadMethods')
mth = eval(f'UploadMethods.{msgtype}')
try:
msgchain = MessageChain.create([Image.fromNetworkAddress(url=sendlink, method=mth)])
2020-10-11 09:11:53 +00:00
print('Sending Image...')
2020-10-11 07:49:09 +00:00
await sendmessage(app, msgchain, target1, target2, msgtype)
2020-10-11 09:11:53 +00:00
except Exception:
traceback.print_exc()
2020-10-11 07:49:09 +00:00
msgchain = MessageChain.create(
2020-10-11 09:11:53 +00:00
[Plain('上传过程中遇到了问题,图片发送失败。')])
2020-10-11 07:49:09 +00:00
await sendmessage(app, msgchain, target1, target2, msgtype)
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
2020-10-11 07:49:09 +00:00
"""
if msgtype == 'Group':
voice = re.findall(r'https?://.*?/File:.*?\.(?:ogg|m4a|mp3|flac|wav)', run, re.I)
for voicelink in voice:
try:
findvoicename = re.match(r'(https?://.*?/)File:(.*?\.(?:ogg|m4a|mp3|flac|wav))', voicelink, re.I)
downloadfile = await dfile(findvoicename.group(1), findvoicename.group(2))
print(downloadfile)
conventamr = await camr(downloadfile)
print(conventamr)
readfile = open(conventamr, 'rb+')
uploadvoice = await app.uploadVoice(readfile.read())
voicemsgchain = MessageChain.create([uploadvoice])
await app.sendGroupMessage(target1, voicemsgchain)
readfile.close()
os.remove(downloadfile)
os.remove(conventamr)
except Exception:
traceback.print_exc()
2020-10-11 10:34:09 +00:00
"""