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
|
|
|
|
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-21 16:06:17 +00:00
|
|
|
from graia.application.message.elements.internal import Plain, Image, Source, At
|
2020-09-20 11:11:11 +00:00
|
|
|
from modules.camr import camr
|
|
|
|
from modules.dfile import dfile
|
2020-08-18 13:12:38 +00:00
|
|
|
from modules.findimage import findimage
|
2020-08-12 16:01:34 +00:00
|
|
|
|
2020-09-20 16:24:56 +00:00
|
|
|
import traceback
|
2020-10-09 11:13:19 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
cachepath = abspath('./assets/cache/')
|
|
|
|
cachefile = os.listdir(cachepath)
|
|
|
|
for file in cachefile:
|
|
|
|
os.remove(f'{cachepath}/{file}')
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
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-09-26 05:37:55 +00:00
|
|
|
command = __import__('CommandGen',fromlist=[runfun])
|
|
|
|
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-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-20 10:19:47 +00:00
|
|
|
if msgtype == 'Group':
|
2020-09-19 06:21:06 +00:00
|
|
|
voice = re.findall(r'https?://.*?/File:.*?\.(?:ogg|m4a|mp3|flac|wav)', run, re.I)
|
2020-09-20 10:19:47 +00:00
|
|
|
for voicelink in voice:
|
2020-09-20 16:24:56 +00:00
|
|
|
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-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-10-09 11:13:19 +00:00
|
|
|
fuimg = re.findall(r'\[\[uimgc:.*\]\]', msg)
|
|
|
|
print(fuimg)
|
|
|
|
msgbase = re.sub(r'\[\[uimgc:.*\]\]', '', msg)
|
|
|
|
msgchain = MessageChain.create([Plain(msgbase)])
|
|
|
|
for imgc in fuimg:
|
|
|
|
img = re.match(r'\[\[uimgc:(.*)\]\]',imgc)
|
|
|
|
if img:
|
|
|
|
try:
|
|
|
|
msgchain = msgchain.plusWith(
|
|
|
|
[Image.fromLocalFile(filepath=abspath(img.group(1)), method=mth)])
|
|
|
|
except Exception:
|
|
|
|
traceback.print_exc()
|
2020-08-30 08:26:54 +00:00
|
|
|
r = re.findall(r'(https?://.*?/File:.*?\.(?:png|gif|jpg|jpeg|webp|bmp|ico))', msg, re.I)
|
|
|
|
for d in r:
|
2020-09-21 16:06:17 +00:00
|
|
|
try:
|
|
|
|
d1 = await findimage(d)
|
|
|
|
print(d1)
|
|
|
|
msgchain = msgchain.plusWith([Image.fromNetworkAddress(url=d1, method=mth)])
|
|
|
|
except Exception:
|
|
|
|
traceback.print_exc()
|
2020-08-30 08:26:54 +00:00
|
|
|
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
|