summaryrefslogtreecommitdiff
path: root/ATRI/plugins/chat
diff options
context:
space:
mode:
authorKyomotoi <[email protected]>2021-07-31 16:21:23 +0800
committerKyomotoi <[email protected]>2021-07-31 16:21:23 +0800
commit1d54ec291f2c25cc01f24d0d0163d0bf889457fd (patch)
tree251004df3f659f79b96b9abce59cc1140e24498c /ATRI/plugins/chat
parentbeb16b60fe4d8586436f5ada5fdabb6db23f5a29 (diff)
parent36d26d1dc61c36b4601aaf75e148060c5bcb98a7 (diff)
downloadATRI-1d54ec291f2c25cc01f24d0d0163d0bf889457fd.tar.gz
ATRI-1d54ec291f2c25cc01f24d0d0163d0bf889457fd.tar.bz2
ATRI-1d54ec291f2c25cc01f24d0d0163d0bf889457fd.zip
Merge branch 'main' of https://github.com/Kyomotoi/ATRI into main
Diffstat (limited to 'ATRI/plugins/chat')
-rw-r--r--ATRI/plugins/chat/__init__.py37
-rw-r--r--ATRI/plugins/chat/data_source.py37
2 files changed, 40 insertions, 34 deletions
diff --git a/ATRI/plugins/chat/__init__.py b/ATRI/plugins/chat/__init__.py
index 79664cc..9f7f26c 100644
--- a/ATRI/plugins/chat/__init__.py
+++ b/ATRI/plugins/chat/__init__.py
@@ -15,20 +15,23 @@ _chat_flmt_notice = choice(["慢...慢一..点❤", "冷静1下", "歇会歇会~
chat = Chat().on_message("闲聊(文爱")
+
@chat.handle()
async def _chat(bot: Bot, event: MessageEvent):
print(1)
user_id = event.get_user_id()
if not _chat_flmt.check(user_id):
await chat.finish(_chat_flmt_notice)
-
+
msg = str(event.message)
repo = await Chat().deal(msg, user_id)
_chat_flmt.start_cd(user_id)
await chat.finish(repo)
+
my_name_is = Chat().on_command("叫我", "更改闲聊(划掉 文爱)时的称呼", aliases={"我是"}, priority=1)
+
@my_name_is.args_parser # type: ignore
async def _get_name(bot: Bot, event: MessageEvent, state: T_State):
msg = str(event.message).strip()
@@ -40,26 +43,30 @@ async def _get_name(bot: Bot, event: MessageEvent, state: T_State):
else:
state["name"] = msg
+
@my_name_is.handle()
async def _name(bot: Bot, event: MessageEvent, state: T_State):
user_id = event.get_user_id()
if not _chat_flmt.check(user_id):
await my_name_is.finish(_chat_flmt_notice)
-
+
msg = str(event.message).strip()
if msg:
state["name"] = msg
+
@my_name_is.got("name", "欧尼酱想让咱如何称呼呢!0w0")
async def _deal_name(bot: Bot, event: MessageEvent, state: T_State):
user_id = event.get_user_id()
new_name = state["name"]
- repo = choice([
- f"好~w 那咱以后就称呼你为{new_name}!",
- f"噢噢噢!原来你叫{new_name}阿~",
- f"好欸!{new_name}ちゃん~~~",
- "很不错的称呼呢w"
- ])
+ repo = choice(
+ [
+ f"好~w 那咱以后就称呼你为{new_name}!",
+ f"噢噢噢!原来你叫{new_name}阿~",
+ f"好欸!{new_name}ちゃん~~~",
+ "很不错的称呼呢w",
+ ]
+ )
Chat().name_is(user_id, new_name)
_chat_flmt.start_cd(user_id)
await my_name_is.finish(repo)
@@ -67,6 +74,7 @@ async def _deal_name(bot: Bot, event: MessageEvent, state: T_State):
say = Chat().on_command("说", "别人让我说啥就说啥(", priority=1)
+
@say.args_parser # type: ignore
async def _get_say(bot: Bot, event: MessageEvent, state: T_State):
msg = str(event.message).strip()
@@ -78,29 +86,26 @@ async def _get_say(bot: Bot, event: MessageEvent, state: T_State):
else:
state["say"] = msg
+
@say.handle()
async def _ready_say(bot: Bot, event: MessageEvent, state: T_State):
user_id = event.get_user_id()
if not _chat_flmt.check(user_id):
await say.finish(_chat_flmt_notice)
-
+
msg = str(event.message)
if msg:
state["say"] = msg
+
@say.got("say")
async def _deal_say(bot: Bot, event: MessageEvent, state: T_State):
msg = state["say"]
check = CoolqCodeChecker(msg).check
if not check:
- repo = choice([
- "不要...",
- "这个咱不想复读!",
- "不可以",
- "不好!"
- ])
+ repo = choice(["不要...", "这个咱不想复读!", "不可以", "不好!"])
await say.finish(repo)
-
+
user_id = event.get_user_id()
_chat_flmt.start_cd(user_id)
await say.finish(msg)
diff --git a/ATRI/plugins/chat/data_source.py b/ATRI/plugins/chat/data_source.py
index 15c495d..4c3578d 100644
--- a/ATRI/plugins/chat/data_source.py
+++ b/ATRI/plugins/chat/data_source.py
@@ -21,16 +21,17 @@ KIMO_URL = "https://cdn.jsdelivr.net/gh/Kyomotoi/AnimeThesaurus/data.json"
class Chat(Service):
-
def __init__(self):
- Service.__init__(self, "闲聊", __doc__, rule=to_bot() & is_in_service("闲聊"), priority=5)
-
+ Service.__init__(
+ self, "闲聊", __doc__, rule=to_bot() & is_in_service("闲聊"), priority=5
+ )
+
@staticmethod
async def _request(url: str) -> dict:
res = await request.get(url)
data = await res.json()
return data
-
+
@classmethod
async def _generate_data(cls) -> None:
file_name = "kimo.json"
@@ -44,18 +45,18 @@ class Chat(Service):
log.info("生成完成")
except WriteError:
raise WriteError("Writing kimo words failed!")
-
+
@classmethod
async def _load_data(cls) -> dict:
file_name = "kimo.json"
path = CHAT_PATH / file_name
if not path.is_file():
await cls._generate_data()
-
+
with open(path, "r", encoding="utf-8") as r:
data = json.loads(r.read())
return data
-
+
@classmethod
async def update_data(cls) -> None:
log.info("更新闲聊词库ing...")
@@ -63,17 +64,17 @@ class Chat(Service):
path = CHAT_PATH / file_name
if not path.is_file():
await cls._generate_data()
-
+
updata_data = await cls._request(KIMO_URL)
data = json.loads(path.read_bytes())
for i in updata_data:
if i not in data:
data[i] = updata_data[i]
-
+
with open(path, "w", encoding="utf-8") as w:
w.write(json.dumps(data, indent=4))
log.info("闲聊词库更新完成")
-
+
@staticmethod
def name_is(user_id: str, new_name: str):
file_name = "users.json"
@@ -82,7 +83,7 @@ class Chat(Service):
with open(path, "w", encoding="utf-8") as w:
w.write(json.dumps({}))
data = {}
-
+
data = json.loads(path.read_bytes())
data[user_id] = new_name
try:
@@ -90,7 +91,7 @@ class Chat(Service):
w.write(json.dumps(data, indent=4))
except ReadFileError:
raise ReadFileError("Update user name failed!")
-
+
@staticmethod
def load_name(user_id: str) -> str:
file_name = "users.json"
@@ -99,21 +100,21 @@ class Chat(Service):
with open(path, "w", encoding="utf-8") as w:
w.write(json.dumps({}))
return "你"
-
+
data = json.loads(path.read_bytes())
try:
result = data[user_id]
except BaseException:
result = "你"
return result
-
+
@classmethod
async def deal(cls, msg: str, user_id: str) -> str:
keywords = posseg.lcut(msg)
shuffle(keywords)
-
+
data = await cls._load_data()
-
+
repo = str()
for i in keywords:
a = i.word
@@ -125,14 +126,14 @@ class Chat(Service):
pass
if a in data:
repo = data.get(a, str())
-
+
if not repo:
temp_data = list(data)
shuffle(temp_data)
for i in temp_data:
if i in msg:
repo = data.get(i, str())
-
+
a = choice(repo) if type(repo) is list else repo
user_name = cls.load_name(user_id)
repo = a.replace("你", user_name)