refactor: use enum to specify type

This commit is contained in:
lanvent
2023-03-13 19:44:24 +08:00
parent 1dc3f85a66
commit ad6ae0b32a
13 changed files with 185 additions and 118 deletions

View File

@@ -1,5 +1,7 @@
# encoding:utf-8
from bridge.context import ContextType
from bridge.reply import Reply, ReplyType
import plugins
from plugins import *
from common.log import logger
@@ -14,31 +16,31 @@ class Hello(Plugin):
def on_handle_context(self, e_context: EventContext):
if e_context['context']['type'] != "TEXT":
if e_context['context'].type != ContextType.TEXT:
return
content = e_context['context']['content']
content = e_context['context'].content
logger.debug("[Hello] on_handle_context. content: %s" % content)
if content == "Hello":
reply = {}
reply['type'] = "TEXT"
reply = Reply()
reply.type = ReplyType.TEXT
msg = e_context['context']['msg']
if e_context['context']['isgroup']:
reply['content'] = "Hello, " + msg['ActualNickName'] + " from " + msg['User'].get('NickName', "Group")
reply.content = "Hello, " + msg['ActualNickName'] + " from " + msg['User'].get('NickName', "Group")
else:
reply['content'] = "Hello, " + msg['User'].get('NickName', "My friend")
reply.content = "Hello, " + msg['User'].get('NickName', "My friend")
e_context['reply'] = reply
e_context.action = EventAction.BREAK_PASS # 事件结束并跳过处理context的默认逻辑
if content == "Hi":
reply={}
reply['type'] = "TEXT"
reply['content'] = "Hi"
reply = Reply()
reply.type = ReplyType.TEXT
reply.content = "Hi"
e_context['reply'] = reply
e_context.action = EventAction.BREAK # 事件结束进入默认处理逻辑一般会覆写reply
if content == "End":
# 如果是文本消息"End",将请求转换成"IMAGE_CREATE"并将content设置为"The World"
e_context['context']['type'] = "IMAGE_CREATE"
e_context['context'].type = "IMAGE_CREATE"
content = "The World"
e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑