diff --git a/bot.py b/bot.py index e047c4e7..474c7f90 100644 --- a/bot.py +++ b/bot.py @@ -23,9 +23,9 @@ if config.onebot: logger.info("检测到 Onebot 配置,将启动 Onebot 模式……") - from platforms.onebot_bot import start_task + from platforms.onebot_bot import start_task, daily_care - bots.append(loop.create_task(start_task())) + bots.extend((loop.create_task(start_task()), loop.create_task(daily_care()))) if config.telegram: logger.info("检测到 telegram 配置,将启动 telegram bot 模式……") from platforms.telegram_bot import start_task diff --git a/chat.pkl b/chat.pkl new file mode 100644 index 00000000..9e770997 Binary files /dev/null and b/chat.pkl differ diff --git a/platforms/onebot_bot.py b/platforms/onebot_bot.py index ec1a3fa8..67aefe48 100644 --- a/platforms/onebot_bot.py +++ b/platforms/onebot_bot.py @@ -18,6 +18,12 @@ from middlewares.ratelimit import manager as ratelimit_manager from universal import handle_message +import pickle +import datetime +import time +import asyncio +import random + bot = CQHttp() @@ -133,6 +139,55 @@ async def respond(resp): FriendTrigger = DetectPrefix(config.trigger.prefix + config.trigger.prefix_friend) +async def daily_care(): + with open('./chat.pkl', 'rb') as f: + event = pickle.load(f) + send_times = [[8, 0], [22, 0]] + morning_msg = "现在是早上8点0分,发个早安祝福语。" + evening_msg = "现在是晚上22点0分,发个晚安祝福语。" + require = "要求:注意要有文采,要体现出贴心和关怀,不要显得流程化,确保每次发送的内容风格不同。" + messages = [morning_msg, evening_msg] + # 发送列表 + targets = {"小明": 123456789} # QQ号 + nicknames = {"小明": "小明"} # QQ昵称 + promots = {"小明": "小明是个大学生[可以补充个性化信息]。" + } + while True: + now = datetime.datetime.now() # 获取当前时间 + for id, send_time in enumerate(send_times): + if now.hour == send_time[0] and now.minute == send_time[1]: + message = messages[id] + for name, qq in targets.items(): + event.user_id = qq + event.message = message + salt = random.randint(1, 10000) + if (salt % 3) == 0: + event.message = f"{event.message}提示:{promots[name]}" + event.message += require + event.raw_message = event.message + event.sender = {'age': 0, 'nickname': nicknames[name], 'sex': 'unknown', 'user_id': qq} + if event.message.startswith('.'): + return + chain = transform_message_chain(event.message) + try: + msg = await FriendTrigger(chain, None) + except: + logger.debug(f"丢弃私聊消息:{event.message}(原因:不符合触发前缀)") + return + logger.debug(f"私聊消息:{event.message}") + try: + await handle_message( + response(event, False), + f"friend-{event.user_id}", + msg.display, + chain, + is_manager=event.user_id == config.onebot.manager_qq, + nickname=event.sender.get("nickname", "好友"), + request_from=constants.BotPlatform.Onebot + ) + except Exception as e: + logger.exception(e) + await asyncio.sleep(60) @bot.on_message('private') async def _(event: Event):