Closed Well2333 closed 1 year ago
毕竟最近两个月都在努力的做到 make it work
,这方面还是欠缺着的,希望理解……
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()
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()
这样如何?
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()
跟进一下,还差protocol的初始化没做
目前初始化阶段的代码大致如下
我们不难发现,其中包含了过多的 import 和偏底层的服务配置,导致代码看起来十分混乱,同时也导致了在缺乏文档的情况下根本无法根据其他类似协议的配置方法进行尝试。
因此,我个人认为以下是值得重新设计或探讨的问题
按照以上思想,设计优化后的的代码如下