1
0
forked from bot/app

first commit

This commit is contained in:
远野千束 2024-02-29 01:03:40 +08:00
commit d425cf8685
10 changed files with 123 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea
plugin

View File

@ -0,0 +1,4 @@
{
"name": "Liteyuki Default",
"version": "1.0"
}

View File

@ -0,0 +1,4 @@
{
"name": "Liteyuki Language Pack",
"version": "1.0"
}

6
main.py Normal file
View File

@ -0,0 +1,6 @@
from src.liteyuki import *
if __name__ == '__main__':
liteyuki = Liteyuki()
app = liteyuki.get_asgi()
liteyuki.run(app="main:app")

View File

@ -0,0 +1,9 @@
from nonebot import on_command
from nonebot.rule import to_me
hello = on_command('hello', aliases={'你好'}, rule=to_me())
@hello.handle()
async def handle_first_receive(bot, event, state):
await hello.finish('Hello, world!')

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
nonebot2[fastapi]
watchdog==2.1.6

0
src/api/data.py Normal file
View File

33
src/api/utils.py Normal file
View File

@ -0,0 +1,33 @@
import os
import yaml
from nonebot import logger
def load_config() -> dict[str, any]:
"""
Load config from config.yml
:return:
"""
config = {
'host': '0.0.0.0',
'port': 20216,
'nickname': ['Liteyuki'],
'command_start': [''],
}
if not os.path.exists('config.yml'):
logger.warning('warn.config_file_not_found')
with open('config.yml', 'w', encoding='utf-8') as f:
f.write(yaml.dump(config, indent=4))
else:
try:
with open('config.yml', 'r', encoding='utf-8') as f:
config.update(yaml.load(f, Loader=yaml.FullLoader))
logger.success('success.config_loaded')
# 格式化后写入
with open('config.yml', 'w', encoding='utf-8') as f:
f.write(yaml.dump(config, indent=4))
except Exception as e:
logger.error(f'error.load_config: {e}')
return config

46
src/liteyuki.py Normal file
View File

@ -0,0 +1,46 @@
from typing import Any, Optional
from nonebot import DOTENV_TYPE
import nonebot
from nonebot import logger
from nonebot.adapters.onebot.v11 import Adapter as OnebotV11Adapter
from nonebot.adapters.onebot.v12 import Adapter as OnebotV12Adapter
from src.api.utils import load_config
app = None
adapters = [
OnebotV11Adapter,
OnebotV12Adapter
]
class Liteyuki:
def __init__(self, *, _env_file: Optional[DOTENV_TYPE] = None, **kwargs: Any):
print(
'\033[34m' + r''' __ ______ ________ ________ __ __ __ __ __ __ ______
/ | / |/ |/ |/ \ / |/ | / |/ | / |/ |
$$ | $$$$$$/ $$$$$$$$/ $$$$$$$$/ $$ \ /$$/ $$ | $$ |$$ | /$$/ $$$$$$/
$$ | $$ | $$ | $$ |__ $$ \/$$/ $$ | $$ |$$ |/$$/ $$ |
$$ | $$ | $$ | $$ | $$ $$/ $$ | $$ |$$ $$< $$ |
$$ | $$ | $$ | $$$$$/ $$$$/ $$ | $$ |$$$$$ \ $$ |
$$ |_____ _$$ |_ $$ | $$ |_____ $$ | $$ \__$$ |$$ |$$ \ _$$ |_
$$ |/ $$ | $$ | $$ | $$ | $$ $$/ $$ | $$ |/ $$ |
$$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/ ''' + '\033[0m'
)
kwargs = load_config()
self.nonebot = nonebot
self.nonebot.init(_env_file=_env_file, **kwargs)
self.driver = self.nonebot.get_driver()
def run(self, *args, **kwargs):
for adapter in adapters:
self.driver.register_adapter(adapter)
self.nonebot.load_plugin('src.liteyuki_main')
self.nonebot.load_plugins('src/builtin')
self.nonebot.load_plugins('plugins')
# Todo: load from database
self.nonebot.run(*args, **kwargs)
def get_asgi(self):
return self.nonebot.get_asgi()

16
src/liteyuki_main.py Normal file
View File

@ -0,0 +1,16 @@
from nonebot import on_command
from nonebot.adapters.onebot.v11.event import MessageEvent
from nonebot.permission import SUPERUSER
import os
folders = ['plugins']
for folder in folders:
if not os.path.exists(folder):
os.makedirs(folder)
echo = on_command('echo', permission=SUPERUSER, priority=5)
@echo.handle()
async def _(event: MessageEvent):
print(event.get_message())
await echo.finish(event.get_message())