Skip to content

Commit

Permalink
所有bot.send()全部转为utils.MessageChainSender.messagechain_sender(),方便进行调试和调用
Browse files Browse the repository at this point in the history
  • Loading branch information
NekoRabi committed Jun 16, 2023
1 parent d68e239 commit eb1dbec
Show file tree
Hide file tree
Showing 79 changed files with 465 additions and 201 deletions.
3 changes: 2 additions & 1 deletion core/fun/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
from core.fun.gethelp import *
from core.fun.signin import *
from core.fun.roll import *
from core.fun.tarot import *
from core.fun.tarot import *
from .bot_state_in_group import *
29 changes: 15 additions & 14 deletions core/fun/admin_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async def deladmin(event: FriendMessage):
else:
return await messagechain_sender(event=event, msg=f"{m.group(1)} 不是再管理员了")
else:
await bot.send(event, await messagechain_builder(text="抱歉,您无权这么做哦", rndimg=True))
await messagechain_sender(event=event, msg=await messagechain_builder(text="抱歉,您无权这么做哦", rndimg=True))
return


Expand All @@ -74,8 +74,8 @@ async def getbotinfo(event: FriendMessage):
m = re.match(fr"^{commandpre}{commands_map['sys']['getbotinfo']}", msg.strip())
if m:
if userid in admin:
return await bot.send(event,
f"机器人设置:{config}\n白名单用户:{_whitelist}\n黑名单用户:{_black_list['user']}\n屏蔽群组:{_black_list['group']}")
return await messagechain_sender(event=event,
msg=f"机器人设置:{config}\n白名单用户:{_whitelist}\n黑名单用户:{_black_list['user']}\n屏蔽群组:{_black_list['group']}")


@bot.on(GroupMessage)
Expand All @@ -90,9 +90,9 @@ async def addwhitelist(event: GroupMessage):
_whitelist.append(int(m.group(1)))
write_file(content=config, path=r'./config/config.yml')
print(m)
return await bot.send(event, "添加成功")
return await messagechain_sender(event=event, msg="添加成功")
else:
return await bot.send(event, "添加失败,用户已存在")
return await messagechain_sender(event=event, msg="添加失败,用户已存在")


@bot.on(FriendMessage)
Expand All @@ -104,14 +104,14 @@ async def addblacklist(event: FriendMessage):
if m:
if userid in admin:
if int(m.group(1)) in admin:
return await bot.send(event, "请不要将管理员加入黑名单")
return await messagechain_sender(event=event, msg="请不要将管理员加入黑名单")
_black_list['user'].append(int(m.group(1)))

write_file(content=config, path=r'./config/config.yml')
print(m)
return await bot.send(event, "添加成功")
return await messagechain_sender(event=event, msg="添加成功")
else:
return await bot.send(event, "添加失败,用户已存在")
return await messagechain_sender(event=event, msg="添加失败,用户已存在")


@bot.on(FriendMessage)
Expand All @@ -126,9 +126,9 @@ async def delblacklist(event: FriendMessage):
_black_list['user'].remove(delperson)

write_file(content=config, path=r'./config/config.yml')
return await bot.send(event, "删除成功")
return await messagechain_sender(event=event, msg="删除成功")
else:
return await bot.send(event, "删除失败,用户不存在")
return await messagechain_sender(event=event, msg="删除失败,用户不存在")


@bot.on(FriendMessage)
Expand All @@ -138,7 +138,7 @@ async def getsyslog(event: FriendMessage):
m = re.match(
fr"^{commandpre}{commands_map['sys']['log']}", msg.strip())
if m:
return await bot.send(event, "日志功能开发中")
return await messagechain_sender(event=event, msg="日志功能开发中")


@bot.on(GroupMessage)
Expand All @@ -150,11 +150,12 @@ async def tell_to_master(event: GroupMessage):
if m:
qqid = event.sender.id
if qqid in _black_list:
return await bot.send(event, await messagechain_builder(text='你已被列入黑名单,禁止使用该功能'))
return await messagechain_sender(event=event, msg=await messagechain_builder(text='你已被列入黑名单,禁止使用该功能'))
if master != 0:
if not cmdbuffer.updategroupcache(LongTimeGroupCommand(event.group.id, event.sender.id, 'tell_master')):
return bot.send(event, await messagechain_builder(text="只能每5分钟发一条消息哦~", at=event.sender.id))
return messagechain_sender(event=event,
msg=await messagechain_builder(text="只能每5分钟发一条消息哦~", at=event.sender.id))

message = m.group(1)
await bot.send_friend_message(master, await messagechain_builder(text=f'qq {qqid} 的人说:{message}'))
await bot.send(event, await messagechain_builder(text='已转告主人'))
await messagechain_sender(event=event, msg=await messagechain_builder(text='已转告主人'))
65 changes: 65 additions & 0 deletions core/fun/bot_state_in_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
:Author: NekoRabi
:Create: 2023/6/15 3:03
:Update: /
:Describe: Bot群状态监控工具
:Version: 0.0.1
"""

from mirai.models import BotLeaveEventKick, BotMuteEvent, BotUnmuteEvent, BotJoinGroupEvent

from core import bot, master
from utils import root_logger
from utils.MessageChainBuilder import messagechain_builder
from utils.MessageChainSender import messagechain_sender

__all__ = ['bot_muted', 'bot_unmuted', 'bot_leave_group', 'bot_join_group']


@bot.on(BotLeaveEventKick)
async def bot_leave_group(event: BotLeaveEventKick):
"""Bot被踢出群"""
try:
await messagechain_sender(friendtarget=master, msg=await messagechain_builder(text=f"Bot被踢出群{event.group.id}"))
root_logger.error(f'Bot被踢出群聊{event.group.id}')
except Exception as _e:
print(f'Bot被踢出出群聊时发生错误\n{_e}')
root_logger.error(f'Bot被踢出出群聊时发生错误\n{_e}')
return


@bot.on(BotMuteEvent)
async def bot_muted(event: BotMuteEvent):
"""Bot在x群被禁言"""
try:
await messagechain_sender(friendtarget=master, msg=await messagechain_builder(text=f"Bot被群{event.group.id}禁言了"))
root_logger.error(f'Bot被群聊 {event.group.id} 禁言')
except Exception as _e:
print(f'Bot在被{event.group.id}被禁言时发生错误\n{_e}')
root_logger.error(f'Bot在被{event.group.id}被禁言时发生错误\n{_e}')
return


@bot.on(BotUnmuteEvent)
async def bot_unmuted(event: BotUnmuteEvent):
"""Bot在x群被解除禁言"""
try:
await messagechain_sender(friendtarget=master,
msg=await messagechain_builder(text=f"Bot在{event.group.id}群解除禁言"))
root_logger.error(f'Bot被群聊 {event.group.id} 解除禁言')
except Exception as _e:
print(f'Bot在群{event.group.id}时被解除禁言发生错误\n{_e}')
root_logger.error(f'Bot在群{event.group.id}时被解除禁言发生错误\n{_e}')
return

@bot.on(BotJoinGroupEvent)
async def bot_join_group(event: BotJoinGroupEvent):
"""Bot加入群"""
try:
await messagechain_sender(friendtarget=master,
msg=await messagechain_builder(text=f"Bot加入群聊 {event.group.id}"))
root_logger.error(f"Bot加入群聊 {event.group.id}")
except Exception as _e:
print(f'Bot加入群聊 {event.group.id} 时发生错误\n{_e}')
root_logger.error(f'Bot加入群聊 {event.group.id} 时发生错误\n{_e}')
return
11 changes: 6 additions & 5 deletions core/fun/gethelp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
:Author: NekoRabi
:Create: 2022/8/18 2:19
:Update: /
:Describe: 获取机器人帮助,或者说是呼出指令面板
:Describe: 获取机器人帮助,或者说是呼出指令面板和获取项目地址
:Version: 0.0.1
"""

import re
from mirai import MessageEvent, Plain
from core import bot, commandpre, commands_map, config
from utils.MessageChainBuilder import messagechain_builder
from utils.MessageChainSender import messagechain_sender

_settings = config.get('settings')

Expand All @@ -23,16 +24,16 @@ async def getsyshelp(event: MessageEvent):
fr"^{commandpre}{commands_map['sys']['help']}", msg.strip())
if m and _settings['help']:
# if not cmdbuffer.updategroupcache(groupcommand(event.group.id, event.sender.id, 'help')):
# return bot.send(event, await messagechain_builder()(text="帮助文档刚刚才发过哦~", rndimg=True, at=event.sender.id))
return await bot.send(event, await messagechain_builder(imgpath="./images/grouphelp.png"))
# return messagechain_sender(event=event,msg= await messagechain_builder()(text="帮助文档刚刚才发过哦~", rndimg=True, at=event.sender.id))
return await messagechain_sender(event=event, msg=await messagechain_builder(imgpath="./images/grouphelp.png"))


@bot.on(MessageEvent)
async def getprojectlink(event: MessageEvent):
msg = "".join(map(str, event.message_chain[Plain]))
m = re.match(fr"^{commandpre}项目地址\s*$", msg.strip())
if m:
return await bot.send(event, await messagechain_builder(text="Github : https://github.com/NekoRabi/Majsoul-QQBot\n"
"如果觉得好可以点个star⭐"))
return await messagechain_sender(event=event, msg=await messagechain_builder(
text="Github : https://github.com/NekoRabi/Majsoul-QQBot\n如果觉得好用可以帮忙点个star⭐"))

# 与机器人互动
2 changes: 1 addition & 1 deletion core/fun/ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def ping(event: FriendMessage):
m = re.match(
fr"^{commandpre}{commands_map['sys']['ping']}", msg.strip())
if m:
await bot.send(event, "pong!")
await messagechain_sender(event=event, msg="pong!")
return


Expand Down
13 changes: 8 additions & 5 deletions core/fun/signin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
os.mkdir("./database/sys")



def db_init():
cx = sqlite3.connect("./database/sys/sys.sqlite")
cursor = cx.cursor()
Expand Down Expand Up @@ -99,12 +98,15 @@ async def sign_in(event: GroupMessage):
success, signmsg = signin(event.sender.id)
if success:
card = tarotcards.drawcards(userid=event.sender.id)[0]
return await bot.send(event,
await messagechain_builder(at=event.sender.id, text=signmsg, imgbase64=card.imgcontent))
return await messagechain_sender(event=event,
msg=await messagechain_builder(at=event.sender.id, text=signmsg,
imgbase64=card.imgcontent))
# else:
# return await messagechain_sender(await messagechain_builder(at=event.sender.id, text=signmsg))
else:
return await bot.send(event, await messagechain_builder(at=event.sender.id, text=signmsg, rndimg=True))
return await messagechain_sender(event=event,
msg=await messagechain_builder(at=event.sender.id, text=signmsg,
rndimg=True))


# 查询积分
Expand All @@ -118,6 +120,7 @@ async def getuserscore(event: GroupMessage):
if m:
scoremsg = getscore(
userid=event.sender.id)
return await bot.send(event, await messagechain_builder(text=scoremsg, rndimg=True))
return await messagechain_sender(event=event, msg=await messagechain_builder(text=scoremsg, rndimg=True))


db_init()
5 changes: 5 additions & 0 deletions core/fun/silence_toggle.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from mirai import GroupMessage, Plain, FriendMessage

from core import bot, config, commandpre, commands_map
from utils.MessageChainSender import messagechain_sender
from utils.cfg_loader import write_file

settings = config.get('settings')
Expand All @@ -31,9 +32,11 @@ async def be_silence_from_friend(event: FriendMessage):
if m.group(1).lower() == 'on' or m.group(1).lower() == 'true':
settings['silence'] = True
write_file(content=config, path=r'./config/config.yml')
await messagechain_sender(friendtarget=event.sender.id, msg='我会少说几句的')
else:
settings['silence'] = False
write_file(content=config, path=r'./config/config.yml')
await messagechain_sender(friendtarget=event.sender.id, msg='那我多说几句')


# 单群沉默 - 从群聊沉默
Expand All @@ -54,7 +57,9 @@ async def be_groupsilence_from_group(event: GroupMessage):
if event.group.id not in silencegroup:
silencegroup.append(event.group.id)
write_file(content=config, path=r'./config/config.yml')
# await messagechain_sender(friendtarget=event.sender.id, msg='吵到了你的话,那我会少说几句的~')
else:
if event.group.id in silencegroup:
silencegroup.remove(event.group.id)
write_file(content=config, path=r'./config/config.yml')
# await messagechain_sender(friendtarget=event.sender.id, msg='这么欢迎我,那我多说几句~')
6 changes: 3 additions & 3 deletions core/fun/tarot.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,13 @@ async def getsometarots(event: GroupMessage):
msgC.append(fmn)
# msgC.append(Image(base64=card.imgcontent))
# ForwardMessageNode(event.sender,MessageChain(msgC))
return await bot.send(event, Forward(node_list=msgC))
return await messagechain_sender(event=event, msg=Forward(node_list=msgC))
else:
return await messagechain_sender(event=event,
msg=await messagechain_builder(text='每次只能抽1-9张塔罗牌哦', rndimg=True))
else:
card = tarotcards.drawcards(userid=event.sender.id)[0]
return await bot.send(event, await messagechain_builder(imgbase64=card.imgcontent))
return await messagechain_sender(event=event, msg=await messagechain_builder(imgbase64=card.imgcontent))


# 获取塔罗牌抽卡记录
Expand All @@ -265,4 +265,4 @@ async def getmytarots(event: GroupMessage):
m = re.match(fr"^{commandpre}{commands_map['sys']['getmytarot']}", msg.strip())
if m:
msg = tarotcards.getmydrawcardsinfo(event.sender.id)
return await bot.send(event, await messagechain_builder(text=msg))
return await messagechain_sender(event=event, msg=await messagechain_builder(text=msg))
39 changes: 39 additions & 0 deletions core/scheduler.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
"""
:Author: NekoRabi
:Create: 2023/6/16 1:22
:Update: /
:Describe: 计时器模块,本来还想做一个检测更新的
:Version: 0.0.1
"""
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from mirai.bot import Startup, Shutdown
# import logging
# import aiohttp
# import asyncio
# from bs4 import BeautifulSoup

from core import bot



__all__ = ['scheduler', 'start_scheduler', 'stop_scheduler']

scheduler = AsyncIOScheduler(timezone="Asia/Shanghai")

# scheduler.add_job(majsoul.set_link_node, 'cron', hour='0')

_version = 'v0.7.0'

@bot.on(Startup)
def start_scheduler(_):
Expand All @@ -14,3 +32,24 @@ def start_scheduler(_):
@bot.on(Shutdown)
def stop_scheduler(_):
scheduler.shutdown(True) # 结束定时器


# async def _auto_update() -> bool:
# """
# 自动检查有无版本更新
# Returns:
#
# """
# try:
# async with aiohttp.ClientSession(
# connector=aiohttp.TCPConnector(ssl=False), timeout=aiohttp.ClientTimeout(total=25),headers={'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"}) as session:
# async with session.get('https://github.com/NekoRabi/Majsoul-QQBot/releases') as response:
# if response.status == 503:
# return False
# html = response.text()
#
# except aiohttp.client.ClientConnectorError as _e:
# logging.warning('检测更新时发生了意外的错误,类别为aiohttp.client.ClientConnectorError,可能的原因是连接达到上限,可以尝试关闭代理:\n{_e}')
# return False
# bs = BeautifulSoup(html)
# return
Loading

0 comments on commit eb1dbec

Please sign in to comment.