async def test_roll_get_arg( app: App, mocker: MockerFixture, ): """测试点数,获取参数""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.roll import roll_cmd randint = mocker.patch("src.plugins.roll.roll.randint") randint.return_value = 1 async with app.test_matcher(roll_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/roll")) next_event = fake_group_message_event(message=Message("d100")) ctx.receive_event(bot, event) ctx.should_call_send( event, "欢迎使用 NGA 风格 ROLL 点插件\n请问你想怎么 ROLL 点\n你可以输入 d100\n也可以输入 2d100+2d50", "result", ) ctx.should_rejected() ctx.receive_event(bot, next_event) ctx.should_call_send(next_event, "d100=d100(1)=1", "result", at_sender=True) ctx.should_finished() randint.assert_called_once_with(1, 100)
async def test_gete( app: App, mocker: MockerFixture, ): """测试藏宝选门,两个门的情况""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.ff14 import gate_cmd from src.plugins.ff14.gate import EXPR_GATE render_expression = mocker.patch("src.plugins.ff14.gate.render_expression") render_expression.return_value = Message("test") randint = mocker.patch("src.plugins.ff14.gate.randint") randint.return_value = 1 async with app.test_matcher(gate_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/gate 2")) ctx.receive_event(bot, event) ctx.should_call_send(event, Message("test"), "result", at_sender=True) ctx.should_finished() randint.assert_called_once_with(1, 2) render_expression.assert_called_once_with(EXPR_GATE, direction="左边")
async def test_history_get_invalid_arg(app: App): """测试历史""" from nonebot.adapters.onebot.v11 import Message from src.plugins.repeat import history_cmd async with app.test_matcher(history_cmd) as ctx: bot = ctx.create_bot() event = fake_group_message_event(message=Message("/history")) year_event = fake_group_message_event(message=Message("2020")) invalid_month_event = fake_group_message_event(message=Message("test")) month_event = fake_group_message_event(message=Message("1")) day_event = fake_group_message_event(message=Message("0")) ctx.receive_event(bot, event) ctx.should_call_send(event, "你请输入你要查询的年份", True) ctx.receive_event(bot, year_event) ctx.should_call_send(year_event, "你请输入你要查询的月份", True) ctx.receive_event(bot, invalid_month_event) ctx.should_call_send(invalid_month_event, "请只输入数字,不然我没法理解呢!", True) ctx.should_rejected() ctx.receive_event(bot, month_event) ctx.should_call_send(month_event, "你请输入你要查询的日期(如查询整月排名请输入 0)", True) ctx.receive_event(bot, day_event) ctx.should_call_send(day_event, "2020 年 1 月的数据不存在,请换个试试吧 0.0", True)
async def test_history_get_arg(app: App): """测试历史""" from nonebot.adapters.onebot.v11 import Message from src.plugins.repeat import history_cmd, recorder_obj recorder_obj._msg_number_list = {10000: {1: {10: 100}}} recorder_obj._repeat_list = {10000: {1: {10: 10}}} async with app.test_matcher(history_cmd) as ctx: bot = ctx.create_bot() event = fake_group_message_event(message=Message("/history")) year_event = fake_group_message_event(message=Message("2020")) month_event = fake_group_message_event(message=Message("1")) day_event = fake_group_message_event(message=Message("0")) ctx.receive_event(bot, event) ctx.should_call_send(event, "你请输入你要查询的年份", True) ctx.receive_event(bot, year_event) ctx.should_call_send(year_event, "你请输入你要查询的月份", True) ctx.receive_event(bot, month_event) ctx.should_call_send(month_event, "你请输入你要查询的日期(如查询整月排名请输入 0)", True) ctx.receive_event(bot, day_event) ctx.should_call_send(day_event, "2020 年 1 月的数据不存在,请换个试试吧 0.0", True)
async def test_rank_private(app: App): """测试历史""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.repeat import rank_cmd, recorder_obj recorder_obj._msg_number_list = {10000: {1: {10: 100}}} recorder_obj._repeat_list = {10000: {1: {10: 10}}} async with app.test_matcher(rank_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_private_message_event(message=Message("/rank")) next_event = fake_private_message_event(message=Message("10000")) ctx.receive_event(bot, event) ctx.should_call_send(event, "请问你想查询哪个群?", "result") ctx.receive_event(bot, next_event) ctx.should_call_api( "get_group_member_info", data={ "group_id": 10000, "user_id": 10, "no_cache": True }, result={"card": "test"}, ) ctx.should_call_send( next_event, "Love Love Ranking\ntest:10.00%\n\n复读次数排行榜\ntest:10次", "result") ctx.should_finished()
async def test_music_get_arg( app: App, mocker: MockerFixture, ): """测试启动问候已开启的情况""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message, MessageSegment from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.music import music_cmd get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get) async with app.test_matcher(music_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/music")) next_event = fake_group_message_event( message=Message(MessageSegment.image("12"))) final_event = fake_group_message_event(message=Message("test")) ctx.receive_event(bot, event) ctx.should_call_send(event, "你想听哪首歌呢?", "result") ctx.should_rejected() ctx.receive_event(bot, next_event) ctx.should_call_send(next_event, "歌曲名不能为空呢,请重新输入!", "result") ctx.should_rejected() ctx.receive_event(bot, final_event) ctx.should_call_send(final_event, MessageSegment.music("163", 1825190456), "result") ctx.should_finished() get.assert_called_once_with("http://netease:3000/search?keywords=test")
async def rss_to_msg(rss: RSS, info: dict) -> Optional[Message]: msg = Message() img = await rss_to_image(rss, info) if not img: return None msg.append(MessageSegment.image(img)) # msg.append(info["link"]) return msg
async def _(event: MessageEvent): username = event.sender.card or event.sender.nickname message = Message(f"来看看 {username} 抽到了什么:") img = await get_tarot() if img: message.append(MessageSegment.image(img)) await tarot.finish(message) else: await tarot.finish("出错了,请稍后再试")
async def test_ban_group_get_arg( app: App, mocker: MockerFixture, sender: dict, ): """测试群聊天,获取参数禁言 1 分钟 机器人为普通群员,禁言对象为管理员或普通群员 """ from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.ban import EXPR_OK, ban_cmd render_expression = mocker.patch("src.plugins.ban.render_expression") render_expression.return_value = Message("test") async with app.test_matcher(ban_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/ban"), sender=sender) next_event = fake_group_message_event(message=Message("1"), sender=sender) ctx.receive_event(bot, event) ctx.should_call_api("get_group_list", data={}, result=[{ "group_id": 10000 }]) ctx.should_call_api( "get_group_member_info", data={ "group_id": 10000, "user_id": 1 }, result={"role": "owner"}, ) ctx.should_call_send(event, "你想被禁言多少分钟呢?", True) ctx.should_rejected() ctx.receive_event(bot, next_event) ctx.should_call_api( "set_group_ban", data={ "group_id": 10000, "user_id": 10, "duration": 60 }, result=[], ) ctx.should_call_send(next_event, Message("test"), True, at_sender=True) ctx.should_finished() render_expression.assert_called_once_with(EXPR_OK, duration=1)
async def test_price_default(app: App, mocker: MockerFixture): """测试查价,默认值""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.ff14 import price_cmd get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get) async with app.test_matcher(price_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/查价 默认值")) ctx.receive_event(bot, event) ctx.should_call_send(event, "当前设置的默认值为:猫小胖", True) ctx.should_finished() async with app.test_matcher(price_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/查价 萨维奈舞裙")) ctx.receive_event(bot, event) ctx.should_call_send( event, "萨维奈舞裙 在市场的价格是:\n233100*1 服务器: 静语庄园\n233193*1 HQ 服务器: 静语庄园\n233330*1 服务器: 静语庄园\n233334*1 服务器: 静语庄园\n239166*1 HQ 服务器: 紫水栈桥\n239166*1 HQ 服务器: 紫水栈桥\n数据更新时间: 2021年12月29日 05时00分", True, ) ctx.should_finished() async with app.test_matcher(price_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/查价 默认值 静语庄园")) ctx.receive_event(bot, event) ctx.should_call_send(event, "查询区域默认值设置成功!", True) ctx.should_finished() async with app.test_matcher(price_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/查价 默认值")) ctx.receive_event(bot, event) ctx.should_call_send(event, "当前设置的默认值为:静语庄园", True) ctx.should_finished() get.assert_has_calls([ mocker.call("https://cafemaker.wakingsands.com/search?string=萨维奈舞裙"), mocker.call("https://universalis.app/api/v2/猫小胖/10393?listings=6"), ])
async def calculate(matcher: Matcher, msg: Message = CommandArg()): s_msg = str(msg) res = "" if (not is_safe(str(s_msg))): res = Message.template("参数包含非法字符串{}").format(MessageSegment.face(96)) else: try: res = str(eval(s_msg)) except Exception as e: print(e) res = "看不懂" + Message(MessageSegment.face(178)) matcher.stop_propagation() await matcher.finish(res)
async def _(bot: Bot, event: Event, state: T_State): pair = state["_prefix"]["command_arg"] if len(pair) < 1: await save.finish(message=Message('错误的格式')) state['instruct'] = str(pair[0]).strip() if len(pair) > 1: state['content'] = pair[1:]
async def PushAnswer(matcher: Matcher, msg: Message = CommandArg()): matcher.stop_propagation() l = await GetJson("https://lab.magiconch.com/api/nbnhhsh/guess", "post", data={"text": f"{msg}"}) res = "" for d in l: res += Message(f"====={d['name']}=====\n") if ('trans' in d.keys()): res += Message( "\n".join([f"{i+1}. {s}" for i, s in enumerate(d['trans'])]) + "\n") else: res += Message("啥意思啊?我不道啊!") await matcher.finish(res)
async def test_price_item_not_found(app: App, mocker: MockerFixture): """测试查价,物品不存在""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.ff14 import price_cmd get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get) async with app.test_matcher(price_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/查价 未命名 猫小胖")) ctx.receive_event(bot, event) ctx.should_call_send( event, "抱歉,没有找到 未命名,请检查物品名称是否正确。", True, ) ctx.should_finished() get.assert_called_once_with( "https://cafemaker.wakingsands.com/search?string=未命名")
async def test_daily_note_full( app: App, mocker: MockerFixture, ): """测试原神实时便笺,体力满的情况""" from nonebot.adapters.onebot.v11 import Message from src.plugins.genshin import daily_note_cmd from src.plugins.genshin.config import set_cookie set_cookie(10, "full") get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get) async with app.test_matcher(daily_note_cmd) as ctx: bot = ctx.create_bot() event = fake_group_message_event(message=Message("/ys.dailynote")) ctx.receive_event(bot, event) ctx.should_call_send( event, "原粹树脂: 160/160 (已完全恢复)\n洞天宝钱: 2400/2400 (已满)\n每日委托任务: 0/4 (今日完成委托次数不足)\n值得铭记的强敌: 0/3 (本周剩余消耗减半次数)\n探索派遣: 5/5\n角色1: 已完成派遣\n角色2: 已完成派遣\n角色3: 已完成派遣\n角色4: 已完成派遣\n角色5: 已完成派遣", "", ) ctx.should_finished() assert get.call_count == 2
async def test_roll_complex( app: App, mocker: MockerFixture, ): """测试点数,复杂点的输入""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.roll import roll_cmd randint = mocker.patch("src.plugins.roll.roll.randint") randint.return_value = 1 async with app.test_matcher(roll_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/roll d100+2d50")) ctx.receive_event(bot, event) ctx.should_call_send( event, "d100+2d50=d100(1)+d50(1)+d50(1)=3", "result", at_sender=True, ) ctx.should_finished() randint.assert_has_calls( [ mocker.call(1, 100), mocker.call(1, 50), mocker.call(1, 50), ] )
async def _(arg: Message = CommandArg()): msg = arg.extract_plain_text().strip() if not msg: await mc.finish() if msg.startswith("status"): addr = msg.replace("status", "", 1).strip() if addr: status = await get_mcstatus(addr) if status: await mc.finish(status) else: await mc.finish("出错了,请稍后再试") else: types = ["avatar", "head", "body", "skin", "cape", "model"] for t in types: if msg.startswith(t): username = msg.replace(t, "", 1).strip() if username: uuid = await get_mc_uuid(username) if not uuid: await mc.finish("出错了,请稍后再试") if uuid == "none": await mc.finish("找不到该用户") if t == "model": await mc.send("生成中,请耐心等待。。。") result = await get_mcmodel(uuid) else: result = await get_crafatar(t, uuid) if result: await mc.finish(MessageSegment.image(result)) else: await mc.finish("出错了,请稍后再试") await mc.finish()
async def test_eorzean( app: App, mocker: MockerFixture, ): """测试艾欧泽亚天气""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.weather import weather_cmd mocked_time = mocker.patch("src.plugins.weather.eorzean_api.time") mocked_time.time.return_value = 1641619586 async with app.test_matcher(weather_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/天气 利姆萨·罗敏萨")) ctx.receive_event(bot, event) ctx.should_call_send( event, "利姆萨·罗敏萨\n当前天气:碧空\n还剩13分34秒切换到天气:阴云\n还剩36分54秒切换到天气:碧空\n还剩1时0分14秒切换到天气:碧空\n还剩1时23分34秒切换到天气:晴朗\n还剩1时46分54秒切换到天气:碧空\n还剩2时10分14秒切换到天气:碧空\n还剩2时33分34秒切换到天气:碧空\n还剩2时56分54秒切换到天气:晴朗\n还剩3时20分14秒切换到天气:晴朗", "result", ) ctx.should_finished() mocked_time.time.assert_called_once()
async def test_status( app: App, mocker: MockerFixture, ): """测试状态""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.repeat import recorder_obj, status_cmd recorder_obj.start_time = datetime(2020, 1, 1, 0, 0, 0) mocked_datetime = mocker.patch("src.plugins.repeat.status.datetime") mocked_datetime.now.return_value = datetime(2021, 2, 2, 1, 1, 1) mocked_server_status = mocker.patch("src.plugins.repeat.status.server_status") mocked_server_status.return_value = "test" async with app.test_matcher(status_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/status")) ctx.receive_event(bot, event) ctx.should_call_send(event, "已在线 1 年 1 月 1 天 1 小时 1 分钟 1 秒\ntest", "result") ctx.should_finished() mocked_datetime.now.assert_called_once() mocked_server_status.assert_called_once()
async def test_nuannuan( app: App, mocker: MockerFixture, ): """测试时尚品鉴""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.ff14 import nuannuan_cmd with open(Path(__file__).parent / "nuannuan.json", "r", encoding="utf-8") as f: data = json.load(f) async_client = mocker.patch("httpx.AsyncClient.get") r = mocker.MagicMock() r.json = mocker.MagicMock(return_value=data) async_client.return_value = r async with app.test_matcher(nuannuan_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/时尚品鉴")) ctx.receive_event(bot, event) ctx.should_call_send( event, "【FF14/时尚品鉴】第204期 满分攻略 12月24日 最终幻想14\n游玩C哩酱攻略站:www.youwanc.com\n-\n【12月24日 第204周时尚品鉴预告预测】\n主题:高贵的魔法师\n\n【身体防具】*提示内容:决斗者\n——*往期:116 92 57\n狂妄长衣 莽撞长衣 匿盗外套 鬼盗外套 红盗外套 瘟疫使者长衣 瘟疫医生长衣 \n60级副本地脉灵灯天狼星灯塔/草木庭园圣茉夏娜植物园\n\n【腿部防具】*提示内容:暗魔\n——*往期:169 139\n暗魔xx打底裤\n24人副本影之国获得\n\n【脚部防具】*提示内容:祭祀仪式\n——*往期:132 115\n真狮鹫革XX凉鞋/XX战靴\n70\nhttps://www.bilibili.com/video/BV1Pq4y1m7wK", "result", ) ctx.should_finished()
async def generate_messages(self): if self._message is None: await self._pic_merge() msgs = [] text = "" if self.text: if self._use_pic(): text += "{}".format(self.text) else: text += "{}".format( self.text if len(self.text) < 500 else self.text[:500] + "..." ) text += "\n来源: {}".format(self.target_type) if self.target_name: text += " {}".format(self.target_name) if self._use_pic(): msgs.append(await parse_text(text)) if not self.target_type == "rss" and self.url: msgs.append(MessageSegment.text(self.url)) else: if self.url: text += " \n详情: {}".format(self.url) msgs.append(MessageSegment.text(text)) for pic in self.pics: # if isinstance(pic, bytes): # pic = 'base64://' + base64.b64encode(pic).decode() # msgs.append(Message("[CQ:image,file={url}]".format(url=pic))) msgs.append(MessageSegment.image(pic)) if self.compress: msgs = [reduce(lambda x, y: x.append(y), msgs, Message())] msgs.extend(self.extra_msg) self._message = msgs return self._message
async def test_calendar( app: App, mocker: MockerFixture, ): """测试日程""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.pcr import calendar_cmd get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get) mocked_datetime = mocker.patch("src.plugins.pcr.data.datetime") mocked_datetime.now.return_value = datetime(2022, 1, 8) mocked_datetime.strptime = datetime.strptime async with app.test_matcher(calendar_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/pcr.calendar")) ctx.receive_event(bot, event) ctx.should_call_send( event, "一周日程:\n======20220108======\n⨠圣迹2.0倍\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n⨠神殿2.0倍\n======20220109======\n⨠圣迹2.0倍\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n⨠神殿2.0倍\n======20220110======\n⨠圣迹2.0倍\n⨠地下城2.0倍\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n⨠神殿2.0倍\n======20220111======\n⨠圣迹2.0倍\n⨠地下城2.0倍\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n⨠神殿2.0倍\n======20220112======\n⨠N图2.0倍\n⨠地下城2.0倍\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n======20220113======\n⨠N图2.0倍\n⨠地下城2.0倍\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n======20220114======\n⨠N图2.0倍\n⨠地下城2.0倍\n⨠探索2.0倍\n⨠活动:新春破晓之星大危机\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n\n更多日程:https://pcrbot.github.io/pcr-calendar/#cn", "result", ) ctx.should_finished() get.assert_called_once_with( "https://pcrbot.github.io/calendar-updater-action/cn.json") assert mocked_datetime.now.call_count == 2
async def _(bot: Bot, event: Event, state: T_State): instructs = str(state['instructs']).strip().split(' ') if len(instructs) == 0: await drop.finish(message=Message('参数错误')) async def rm(instruct): file = os.path.join(cfg.STORE_PATH, instruct) if not os.path.exists(file): await drop.send(message=Message(f'没有{instruct}这条圣经')) return False os.remove(file) await cfg.drop_ls(instruct) return True if all([await rm(ins) for ins in instructs]): await drop.finish(message=Message('👌'))
async def price_handle(event: MessageEvent, args: Message = CommandArg()): """查价""" argv = args.extract_plain_text().split() if len(argv) == 0: await price_cmd.finish(get_command_help("ff14.price")) if len(argv) == 1 and argv[0] == "默认值": world_or_dc = DATA.config.get(f"price-default-{event.user_id}", "猫小胖") await price_cmd.finish(f"当前设置的默认值为:{world_or_dc}") if len(argv) == 2 and argv[0] == "默认值": DATA.config.set(f"price-default-{event.user_id}", argv[1]) await price_cmd.finish("查询区域默认值设置成功!") if len(argv) > 0: name = argv[0] if len(argv) >= 2: world_or_dc = argv[1] else: world_or_dc = DATA.config.get(f"price-default-{event.user_id}", "猫小胖") try: reply = await get_item_price(name, world_or_dc) except httpx.HTTPError: reply = "抱歉,网络出错,请稍后再试。" await price_cmd.finish(reply)
async def test_query_sub_empty(app: App): """测试查询订阅,订阅为空的情况""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.web.nonebot_bison import query_sub_cmd async with app.test_matcher(query_sub_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/sub.query")) ctx.receive_event(bot, event) ctx.should_call_send(event, Message("当前无订阅"), "result") ctx.should_finished()
async def test_price_world_not_found(app: App, mocker: MockerFixture): """测试查价,服务器/大区不存在""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.ff14 import price_cmd get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get) async with app.test_matcher(price_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/查价 萨维奈舞裙 静语")) ctx.receive_event(bot, event) ctx.should_call_send( event, "抱歉,没有找到 静语 的数据,请检查大区或服务器名称是否正确。", True, ) ctx.should_finished() get.assert_has_calls([ mocker.call("https://cafemaker.wakingsands.com/search?string=萨维奈舞裙"), mocker.call("https://universalis.app/api/v2/静语/10393?listings=6"), ])
async def morning_handle(event: GroupMessageEvent, arg: Message = CommandArg()): args = arg.extract_plain_text() group_id = event.group_id if args == "today": await morning_cmd.finish(await get_moring_message()) if args == "update": await HOLIDAYS_DATA.update() await morning_cmd.finish("节假日数据更新成功") if args and group_id: if strtobool(args): plugin_config.morning_group_id += [group_id] await morning_cmd.finish("已在本群开启每日早安功能") else: plugin_config.morning_group_id = [ n for n in plugin_config.morning_group_id if n != group_id ] await morning_cmd.finish("已在本群关闭每日早安功能") else: if group_id in plugin_config.morning_group_id: await morning_cmd.finish("每日早安功能开启中") else: await morning_cmd.finish("每日早安功能关闭中")
async def test_query_sub(app: App): """测试查询订阅""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.web.nonebot_bison import query_sub_cmd from src.web.nonebot_bison.config import DATA with DATA.open("bison.json", "w", encoding="utf8") as f: json.dump( { "user_target": { "1": { "user": 10000, "user_type": "group", "subs": [{ "target": "https://devblogs.microsoft.com/python/feed/", "target_type": "rss", "target_name": "Python", "cats": [], "tags": [], }], } } }, f, ) async with app.test_matcher(query_sub_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/sub.query")) ctx.receive_event(bot, event) ctx.should_call_send( event, Message( "订阅的帐号为:\nrss Python https://devblogs.microsoft.com/python/feed/" ), "result", ) ctx.should_finished()
async def basic_chat(matcher: Matcher, msg: Message = EventMessage()): chat_data = load_data(data_path) msg = str(msg) if (msg == None or msg == ''): return if (msg in chat_data.keys()): await matcher.finish(Message(chat_data[msg]))
async def rm(instruct): file = os.path.join(cfg.STORE_PATH, instruct) if not os.path.exists(file): await drop.send(message=Message(f'没有{instruct}这条圣经')) return False os.remove(file) await cfg.drop_ls(instruct) return True