Merge branch 'v4-dev' of https://github.com/Teahouse-Studios/bot into v4-dev
This commit is contained in:
commit
d5febadd68
10 changed files with 129 additions and 55 deletions
|
@ -3,6 +3,7 @@ import traceback
|
|||
import discord
|
||||
from core.elements import Plain, Image, MessageSession, MsgInfo, Session
|
||||
from core.bots.discord.client import client
|
||||
from core.elements.others import confirm_command
|
||||
|
||||
|
||||
class Template(MessageSession):
|
||||
|
@ -33,11 +34,6 @@ class Template(MessageSession):
|
|||
session=Session(message=send_list, target=send.channel, sender=send.author))
|
||||
|
||||
async def waitConfirm(self):
|
||||
confirm_command = ["是", "对", '确定', '是吧', '大概是',
|
||||
'也许', '可能', '对的', '是呢', '对呢', '嗯', '嗯呢',
|
||||
'吼啊', '资瓷', '是呗', '也许吧', '对呗', '应该',
|
||||
'yes', 'y', 'yeah', 'yep', 'ok', 'okay', '⭐', '√']
|
||||
|
||||
def check(m):
|
||||
return m.channel == self.session.message.channel and m.author == self.session.message.author
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ from graia.broadcast.interrupt.waiter import Waiter
|
|||
|
||||
from core.elements import Plain as BPlain, Image as BImage, Voice as BVoice, MessageSession, MsgInfo, Session
|
||||
from core.bots.graia.broadcast import app, bcc
|
||||
from core.elements.others import confirm_command
|
||||
|
||||
|
||||
class Template(MessageSession):
|
||||
|
@ -58,10 +59,6 @@ class Template(MessageSession):
|
|||
:return: 若对象发送confirm_command中的其一文本时返回True,反之则返回False
|
||||
"""
|
||||
inc = InterruptControl(bcc)
|
||||
confirm_command = ["是", "对", '确定', '是吧', '大概是',
|
||||
'也许', '可能', '对的', '是呢', '对呢', '嗯', '嗯呢',
|
||||
'吼啊', '资瓷', '是呗', '也许吧', '对呗', '应该',
|
||||
'yes', 'y', 'yeah', 'yep', 'ok', 'okay', '⭐', '√']
|
||||
if isinstance(self.session.target, Group):
|
||||
@Waiter.create_using_function([GroupMessage])
|
||||
def waiter(waiter_group: Group,
|
||||
|
|
|
@ -26,7 +26,7 @@ class MessageSession:
|
|||
class Typing:
|
||||
def __init__(self, msg: MessageSession): ...
|
||||
@staticmethod
|
||||
def bind_template(cls, BotTemplate):...
|
||||
def bind_template(BotTemplate):...
|
||||
def checkSuperUser(self):...
|
||||
class Feature:
|
||||
image = ...
|
||||
|
|
4
core/elements/others/__init__.py
Normal file
4
core/elements/others/__init__.py
Normal file
|
@ -0,0 +1,4 @@
|
|||
confirm_command = ["是", "对", '确定', '是吧', '大概是',
|
||||
'也许', '可能', '对的', '是呢', '对呢', '嗯', '嗯呢',
|
||||
'吼啊', '资瓷', '是呗', '也许吧', '对呗', '应该',
|
||||
'yes', 'y', 'yeah', 'yep', 'ok', 'okay', '⭐', '√']
|
0
core/unit_test/__init__.py
Normal file
0
core/unit_test/__init__.py
Normal file
59
core/unit_test/template.py
Normal file
59
core/unit_test/template.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
from core.elements import MessageSession, Plain, Image as BImage, Session
|
||||
from PIL import Image
|
||||
|
||||
from core.elements.others import confirm_command
|
||||
|
||||
|
||||
class Template(MessageSession):
|
||||
all_func = ("Feature", "sendMessage", "waitConfirm", "asDisplay", "delete", "checkPermission", "Typing", "checkSuperUser")
|
||||
|
||||
class Feature:
|
||||
image = True
|
||||
voice = False
|
||||
|
||||
async def sendMessage(self, msgchain, quote=True) -> MessageSession:
|
||||
if isinstance(msgchain, str):
|
||||
print(msgchain)
|
||||
return MessageSession(target=self.target, session=Session(message=msgchain, target='TEST|Console', sender='TEST|Console'))
|
||||
if isinstance(msgchain, list):
|
||||
msg_list = []
|
||||
for x in msgchain:
|
||||
if isinstance(x, Plain):
|
||||
print(x.text)
|
||||
msg_list.append(x.text)
|
||||
if isinstance(x, BImage):
|
||||
img = Image.open(x.image)
|
||||
img.show()
|
||||
return MessageSession(target=self.target, session=Session(message=str(msg_list), target='TEST|Console', sender='TEST|Console'))
|
||||
|
||||
async def waitConfirm(self):
|
||||
c = input('Confirm: ')
|
||||
if c in confirm_command:
|
||||
return True
|
||||
return False
|
||||
|
||||
def asDisplay(self):
|
||||
return self.session.message
|
||||
|
||||
async def delete(self):
|
||||
print(f"(Try to delete {self.session.message}, but I'm a console so I cannot do it :< )")
|
||||
return True
|
||||
|
||||
def checkPermission(self):
|
||||
print(f"(Try to check your permissions, but this is a unit test environment. Have fun!)")
|
||||
return True
|
||||
|
||||
def checkSuperUser(self):
|
||||
print(f"(Try to check if you are superuser, but this is a unit test environment. Have fun!)")
|
||||
return True
|
||||
|
||||
class Typing:
|
||||
def __init__(self, msg: MessageSession):
|
||||
self.msg = msg
|
||||
|
||||
async def __aenter__(self):
|
||||
print('Console is typing...')
|
||||
|
||||
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
||||
pass
|
||||
|
|
@ -1,8 +1,7 @@
|
|||
from core.elements import MessageSession
|
||||
|
||||
from .tables import WikiTargetSetInfo, WikiInfo
|
||||
import json
|
||||
from .orm import session
|
||||
from .orm import session, WikiTargetSetInfo, WikiInfo
|
||||
|
||||
|
||||
class WikiTargetInfo:
|
||||
|
@ -19,9 +18,8 @@ class WikiTargetInfo:
|
|||
return True
|
||||
|
||||
def get_start_wiki(self):
|
||||
get = self.query.link
|
||||
if get is not None:
|
||||
return get
|
||||
if self.query is not None:
|
||||
return self.query.link if self.query.link is not None else False
|
||||
return False
|
||||
|
||||
def config_interwikis(self, iw: str, iwlink: str = None, let_it=True):
|
||||
|
@ -59,8 +57,8 @@ class WikiTargetInfo:
|
|||
return True
|
||||
|
||||
def get_headers(self):
|
||||
q = self.query.headers
|
||||
if q is not None:
|
||||
if self.query is not None:
|
||||
q = self.query.headers
|
||||
headers = json.loads(q)
|
||||
else:
|
||||
headers = {'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6'}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from .tables import Base
|
||||
from sqlalchemy import Column, String, Text, TIMESTAMP, text
|
||||
from sqlalchemy.dialects.mysql import LONGTEXT
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from config import Config
|
||||
|
||||
|
||||
|
@ -8,7 +10,25 @@ DB_LINK = Config('db_path')
|
|||
|
||||
engine = create_engine(DB_LINK)
|
||||
|
||||
Base.metadata.create_all(bind=engine, checkfirst=True)
|
||||
|
||||
session = sessionmaker(engine)()
|
||||
|
||||
Base = declarative_base()
|
||||
table_prefix = 'module_wiki_'
|
||||
|
||||
|
||||
class WikiTargetSetInfo(Base):
|
||||
__tablename__ = table_prefix + 'TargetSetInfo'
|
||||
targetId = Column(String(512), primary_key=True)
|
||||
link = Column(Text)
|
||||
iws = Column(Text)
|
||||
headers = Column(Text)
|
||||
|
||||
|
||||
class WikiInfo(Base):
|
||||
__tablename__ = table_prefix + 'WikiInfo'
|
||||
apiLink = Column(String(512), primary_key=True)
|
||||
siteInfo = Column(LONGTEXT if session.bind.dialect.name == 'mysql' else Text)
|
||||
timestamp = Column(TIMESTAMP, default=text('CURRENT_TIMESTAMP'))
|
||||
|
||||
|
||||
Base.metadata.create_all(bind=engine, checkfirst=True)
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
from sqlalchemy import Column, String, Text, TIMESTAMP, text
|
||||
from sqlalchemy.dialects.mysql import LONGTEXT
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
|
||||
Base = declarative_base()
|
||||
table_prefix = 'module_wiki_'
|
||||
|
||||
|
||||
class WikiTargetSetInfo(Base):
|
||||
__tablename__ = table_prefix + 'TargetSetInfo'
|
||||
targetId = Column(String(512), primary_key=True)
|
||||
link = Column(Text)
|
||||
iws = Column(Text)
|
||||
headers = Column(Text)
|
||||
|
||||
|
||||
class WikiInfo(Base):
|
||||
__tablename__ = table_prefix + 'WikiInfo'
|
||||
apiLink = Column(String(512), primary_key=True)
|
||||
siteInfo = Column(LONGTEXT)
|
||||
timestamp = Column(TIMESTAMP, default=text('CURRENT_TIMESTAMP'))
|
49
unit_test.py
49
unit_test.py
|
@ -1,18 +1,39 @@
|
|||
from config import Config
|
||||
|
||||
if not Config('db_path'):
|
||||
raise AttributeError('Wait! You need to fill a valid database address into the config.cfg "db_path"\n'
|
||||
'Example: \ndb_path = sqlite:///database/save.db\n'
|
||||
'(Also you can fill in the above example directly,'
|
||||
' bot will automatically create a SQLite database in the "./database/save.db")')
|
||||
|
||||
|
||||
import asyncio
|
||||
import traceback
|
||||
|
||||
from core.parser.parser import parser
|
||||
from core.elements.message import MessageSession, MsgInfo, Session
|
||||
from core.unit_test.template import Template
|
||||
from core.parser.message import parser
|
||||
from core.loader import Modules
|
||||
|
||||
while True:
|
||||
try:
|
||||
kwargs = {}
|
||||
kwargs['TEST'] = True
|
||||
kwargs['command'] = input('> ')
|
||||
if '~' not in kwargs['command']:
|
||||
kwargs['command'] = '~' + kwargs['command']
|
||||
asyncio.run(parser(kwargs))
|
||||
except KeyboardInterrupt:
|
||||
print('已退出。')
|
||||
break
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
|
||||
MessageSession.bind_template(Template)
|
||||
|
||||
|
||||
async def unit_test():
|
||||
while True:
|
||||
try:
|
||||
m = input('> ')
|
||||
await parser(MessageSession(target=MsgInfo(targetId='TEST|0',
|
||||
senderId='TEST|0',
|
||||
senderName='',
|
||||
targetFrom='TEST|Console',
|
||||
senderFrom='TEST|Console'),
|
||||
session=Session(message=m, target='TEST|0', sender='TEST|0')))
|
||||
print('----Process end----')
|
||||
except KeyboardInterrupt:
|
||||
print('Exited.')
|
||||
break
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
|
||||
asyncio.run(unit_test())
|
||||
|
|
Reference in a new issue