GraiaProject / Avilla

The next-gen framework for IM development. Powered by Graia Project.
MIT License
160 stars 14 forks source link

简化初始化流程 #112

Closed Well2333 closed 1 year ago

Well2333 commented 1 year ago

目前初始化阶段的代码大致如下

import os

from avilla.core import Avilla, Context, MessageReceived
from avilla.red.net.ws_client import RedWsClientConfig, RedWsClientNetworking
from avilla.red.protocol import RedProtocol
from creart import create
from graia.broadcast import Broadcast
from launart import Launart
from yarl import URL

broadcast = create(Broadcast)
launart = Launart()
protocol = RedProtocol()
service = protocol.service
config = RedWsClientConfig(URL("ws://localhost:12345"), os.getenv("QQNT_TOKEN"))
conn = RedWsClientNetworking(protocol, config)
service.connections.append(conn)
avilla = Avilla(broadcast, launart, [protocol], message_cache_size=0)

@broadcast.receiver(MessageReceived)
async def on_message_received(cx: Context, event: MessageReceived):
    await cx.scene.send_message("Hello, Avilla!")

launart.launch_blocking(loop=broadcast.loop)  # 启动应用

我们不难发现,其中包含了过多的 import 和偏底层的服务配置,导致代码看起来十分混乱,同时也导致了在缺乏文档的情况下根本无法根据其他类似协议的配置方法进行尝试。

因此,我个人认为以下是值得重新设计或探讨的问题

  1. 对底层服务进行封装,而不是需要用户手动注册
  2. 简化 Protocol 的注册流程,直接在实例化 Protocol 时传入参数
  3. 调整必要的组件的 entry point,使其能在对应协议的 `__init__`` 中引入

按照以上思想,设计优化后的的代码如下

import os

from avilla import Avilla, Context, MessageReceived
from avilla.red import RedProtocol
from aviila.onebot.v11 import OneBot11Protocol

avilla = Avilla()
avilla.register_protocol(RedProtocol(host="ws://localhost:54321", token=os.getenv("QQNT_TOKEN")))
avilla.register_protocol(OneBot11Protocol(host="ws://localhost:12345"))

@avilla.broadcast.receiver(MessageReceived)
async def on_message_received(cx: Context, event: MessageReceived):
    await cx.scene.send_message("Hello, Avilla!")

avilla.run()
GreyElaina commented 1 year ago

毕竟最近两个月都在努力的做到 make it work,这方面还是欠缺着的,希望理解……

RF-Tar-Railt commented 1 year ago
import os 
from avilla.core import Avilla, Context, MessageReceived 
from avilla.red import RedConfig, RedProtocol 
from yarl import URL

config = RedConfig(URL("ws://localhost:12345"), os.getenv("QQNT_TOKEN")) 

avilla = Avilla(message_cache_size=0)
avilla.access(RedProtocol(config))

@avilla.broadcast.receiver(MessageReceived)
async def on_message_received(cx: Context, event: MessageReceived): 
    await cx.scene.send_message("Hello, Avilla!")

avilla.launch_sync()
GreyElaina commented 1 year ago
import os 

from yarl import URL

from avilla.core import Avilla, Context, MessageReceived 
from avilla.red import RedConfig, RedProtocol

config = RedConfig(URL("ws://localhost:12345"), os.getenv("QQNT_TOKEN")) 

avilla = Avilla(message_cache_size=0)
avilla.add_protocol(RedProtocol().configure(host=URL("ws://localhost:12345"), token=os.getenv("QQNT_TOKEN")) )

@avilla.broadcast.receiver(MessageReceived)
async def on_message_received(cx: Context, event: MessageReceived): 
    await cx.scene.send_message("Hello, Avilla!")

avilla.launch()

这样如何?

RF-Tar-Railt commented 1 year ago
import os 
from avilla.core import Avilla, Context, MessageReceived 
from avilla.red import RedProtocol 
from yarl import URL

avilla = Avilla(message_cache_size=0)
avilla.add_protocol(RedProtocol().configure(URL("ws://localhost:12345"), os.getenv("QQNT_TOKEN")))

@avilla.listen(MessageReceived)
async def on_message_received(cx: Context, event: MessageReceived): 
    await cx.scene.send_message("Hello, Avilla!")

avilla.launch()
RF-Tar-Railt commented 1 year ago

跟进一下,还差protocol的初始化没做

RF-Tar-Railt commented 1 year ago

resolve in https://github.com/GraiaProject/Avilla/commit/88526bc9c63bce5e5038a89d05a1d2e75c854d13