async def enable_service(bot: Bot, event: CQEvent): if isinstance(event, GroupMessageEvent): names = event.get_plaintext().split() uid = event.user_id user_info = await bot.get_stranger_info(user_id=uid) nickname = user_info.get('nickname', '未知用户') if not names: await enable.finish(f'>{nickname}\n请在空格后接要启用的服务名') group_id = event.group_id svs = Service.get_loaded_services() succ, notfound = [], [] for name in names: if name in svs: sv = svs[name] u_priv = priv.get_user_priv(event) if u_priv >= sv.manage_priv: sv.set_enable(group_id) succ.append(name) else: await enable.finish( f'>{nickname}\n权限不足!启用[{name}]需要权限:{sv.manage_priv},您的权限:{u_priv}\n{PRIV_TIP}' ) else: notfound.append(name) msg = [f'>{nickname}'] if succ: msg.append('已启用服务:' + '、'.join(succ)) if notfound: msg.append('未找到服务:' + '、'.join(notfound)) if msg: await enable.finish('\n'.join(msg)) elif isinstance(event, PrivateMessageEvent): if event.user_id not in salmon.configs.SUPERUSERS: await enable.finish('请在群聊中启用服务') args = event.get_plaintext().split() if len(args) < 2: await enable.finish( 'Input not supported.\nUsage: <service_name> <group_id1> [<group_id2>, ...]' ) name, *group_ids = args svs = Service.get_loaded_services() if name not in svs: await enable.finish(f'未找到服务:{name}') sv = svs[name] succ = [] for gid in group_ids: try: gid = int(gid) sv.set_enable(gid) succ.append(gid) except: await bot.send(event, f'非法群号:{gid}') await enable.finish(f'服务[{name}]已于{len(succ)}个群内启用:{succ}')
async def help_handle(bot: Bot, event: CQEvent): name = event.get_plaintext().strip() bundles = Service.get_bundles() svs = Service.get_loaded_services() info = Service.get_help() if not name: await send_help.finish(TOP_MANUAL) elif name in bundles: if isinstance(event, GroupMessageEvent): msg = get_bundle_manual(name, bundles[name], event.group_id) elif isinstance(event, PrivateMessageEvent): msg = get_private_manual(name, bundles[name]) await send_help.finish(msg) elif name in svs: msg = get_service_help(name, info[name]) await send_help.finish(msg)
async def ls_service(bot: Bot, event: CQEvent, service_name: str): all_services = Service.get_loaded_services() if service_name in all_services: sv = all_services[service_name] on_g = '\n'.join(map(lambda x: str(x), sv.enable_group)) off_g = '\n'.join(map(lambda x: str(x), sv.disable_group)) default_ = 'enabled' if sv.enable_on_default else 'disabled' msg = f"服务{sv.name}:\n默认:{default_}\nmanage_priv={sv.manage_priv}\nvisible={sv.visible}\n启用群:\n{on_g}\n禁用群:\n{off_g}" await bot.send(event, msg) else: await bot.send(event, f'未找到服务{service_name}')
async def _(bot: Bot, event: CQEvent, state: T_State): if not 'gids' in state: await lssv.finish('Invalid input.') verbose_all = state['args'] svs = Service.get_loaded_services().values() u_priv = priv.get_user_priv(bot, event) if u_priv >= 2: for gid in state['gids']: msg = [f"群{gid}服务一览:"] svs = map(lambda sv: (sv, sv.check_enabled(gid)), svs) key = cmp_to_key(lambda x, y: (y[1] - x[1]) or (-1 if x[ 0].name < y[0].name else 1 if x[0].name > y[0].name else 0)) svs = sorted(svs, key=key) for sv, on in svs: if sv.visible or verbose_all: ox = 'O' if on else 'X' msg.append(f"|{ox}| {sv.name}") await lssv.finish('\n'.join(msg)) else: await lssv.finish('查看服务列表需要管理及以上的权限')
class TweetRouter: def __init__(self): self.follows: Dict[str, FollowEntry] = {} def add(self, service: Service, follow_names: Iterable[str]): for f in follow_names: if f not in self.follows: self.follows[f] = FollowEntry() self.follows[f].services.add(service) def set_media_only(self, screen_name, media_only=True): self.follows[screen_name].media_only = media_only sv = Service("twitter-poller", use_priv=priv.SUPERUSER, manage_priv=priv.SUPERUSER, visible=False) sv_kc = Service("kc-twitter", help_="艦これ推特转发", enable_on_default=False, bundle="kancolle") sv_pcr = Service("pcr-twitter", help_="日服Twitter转发", enable_on_default=True, bundle="pcr订阅") sv_uma = Service("uma-twitter", help_="ウマ娘推特转发", enable_on_default=False, bundle="umamusume") sv_pripri = Service("pripri-twitter", help_="番剧《公主代理人》官推转发",
import random from nonebot.plugin import on_command from nonebot.rule import to_me from salmon import Bot, R, Service, priv, util from salmon.typing import CQEvent, Message, GroupMessageEvent, PrivateMessageEvent # basic function for debug, not included in Service('chat') zai = on_command('zai?', to_me(), aliases={'在?', '在?', '在吗', '在么?', '在嘛', '在嘛?'}) @zai.handle() async def say_hello(bot: Bot, event: CQEvent): await zai.send('はい!私はいつも貴方の側にいますよ!') sv = Service('chat', visible=False) sorry = sv.on_fullmatch('沙雕机器人', only_group=False) waifu = sv.on_fullmatch('老婆', to_me(), aliases={'waifu', 'laopo'}, only_group=False) laogong = sv.on_fullmatch('老公', to_me(), only_group=False) mua = sv.on_fullmatch('mua', to_me(), only_group=False) seina = sv.on_fullmatch('来点星奏', only_group=False) dd = sv.on_fullmatch('我朋友说他好了', aliases={'我有个朋友说他好了'}, only_group=False) haole = sv.on_fullmatch('我好了', only_group=False) @sorry.handle() async def say_sorry(bot: Bot, event: CQEvent): await bot.send(event, 'ごめんなさい!嘤嘤嘤(〒︿〒)') @waifu.handle()
※黄骑四号位例外较多 ※对面羊驼或中后卫坦 有可能歪 ※我方羊驼算一号位 ※图片搬运自漪夢奈特''') sv_help = ''' [日/台/陆rank] rank推荐表 [查看当前/全部rank更新源] [设置rank更新源] [更新rank表缓存] [挖矿15001] 矿场余钻 [黄骑充电表] 黄骑1动规律 [谁是霸瞳] 角色别称查询 '''.strip() sv = Service('pcr-query', help_=sv_help, bundle='pcr查询') miner = sv.on_prefix('挖矿', aliases={'jjc钻石', '竞技场钻石', 'jjc钻石查询', '竞技场钻石查询'}, only_group=False) rank = sv.on_rex(r'^(\*?([日台国陆b])服?([前中后]*)卫?)?rank(表|推荐|指南)?$', only_group=False) current_source = sv.on_fullmatch('查看当前rank更新源', only_group=False) all_source = sv.on_fullmatch('查看全部rank更新源', only_group=False) set_source = sv.on_rex(r'^设置rank更新源 (.{0,5}) (.{0,10}) (.{0,20})$', only_group=False) cache_update = sv.on_fullmatch('更新rank表缓存', only_group=False) yukari = sv.on_fullmatch(('yukari-sheet', '黄骑充电', '酒鬼充电', '酒鬼充电表', '黄骑充电表'), only_group=False) who_is = sv.on_prefix('谁是', only_group=False)
from PIL import Image, ImageDraw, ImageFont import salmon from salmon import aiohttpx, configs, Service, R, Bot from salmon.typing import CQEvent, MessageSegment, GroupMessageEvent, PrivateMessageEvent, FinishedException, T_State from salmon.util import FreqLimiter, pic2b64 from salmon.modules.priconne.pcr_data import chara try: import ujson as json except: import json sv_help = ''' [怎么拆] 接防守队角色名 查询竞技场解法 '''.strip() sv = Service('pcr-arena', help_=sv_help, enable_on_default=False, bundle='pcr查询') lmt = FreqLimiter(5) """ Database for arena likes & dislikes DB is a dict like: { 'md5_id': {'like': set(qq), 'dislike': set(qq)} } """ DB_PATH = os.path.expanduser("~/.salmon/arena_db.json") DB = {} try: with open(DB_PATH, encoding="utf8") as f: DB = json.load(f) for k in DB: DB[k] = {
import re import salmon from salmon import Service, Bot from salmon.configs import picfinder from salmon.util import DailyNumberLimiter from salmon.service import add_header from salmon.typing import CQEvent, T_State, GroupMessageEvent, PrivateMessageEvent, Message from salmon.configs.picfinder import threshold, SAUCENAO_KEY, CHAIN_REPLY, DAILY_LIMIT from salmon.modules.picfinder.image import get_image_data_sauce, get_image_data_ascii helptext = ''' [搜图] 单张/多张搜图 '''.strip() sv = Service('picfinder', help_=helptext) lmtd = DailyNumberLimiter(DAILY_LIMIT) picfind = sv.on_prefix('搜图', aliases={'识图', '查图', '找图'}, only_group=False) @picfind.handle() async def pic_rec(bot: Bot, event: CQEvent, state: T_State): uid = event.user_id if not lmtd.check(uid): await picfind.finish(f'您今天已经搜过{DAILY_LIMIT}次图了,休息一下明天再来吧~', call_header=True) args = str(event.message).strip() if args: state['pic'] = args message = await add_header(bot,
import re import random from salmon import Service, Bot from salmon.typing import CQEvent, Message, T_State, GroupMessageEvent, PrivateMessageEvent sv = Service('dice', help_=''' [.r] 掷骰子 [.r 3d12] 掷3次12面骰子 [.qj dihe] ケッコンカッコカリ '''.strip()) async def do_dice(bot: Bot, event: CQEvent, num, min_, max_, opr, offset, TIP="的掷骰结果是:"): if num == 0: await bot.send(event, '咦?骰子呢?') return min_, max_ = min(min_, max_), max(min_, max_) rolls = list(map(lambda _: random.randint(min_, max_), range(num))) sum_ = sum(rolls) rolls_str = '+'.join(map(lambda x: str(x), rolls)) if len(rolls_str) > 100: rolls_str = str(sum_) res = sum_ + opr * offset msg = [ f'{TIP}\n', str(num) if num > 1 else '', 'D', f'{min_}~' if min_ != 1 else '', str(max_), (' +-'[opr] + str(offset)) if offset else '', '=', rolls_str, (' +-'[opr] + str(offset)) if offset else '', f'={res}' if offset or num > 1 else '', ] msg = ''.join(msg)
import os import re from urllib.parse import urlparse, parse_qs try: import ujson as json except: import json import salmon from salmon import aiohttpx, R, Service, Bot, scheduler from salmon.typing import CQEvent, T_State, FinishedException, Message, GroupMessageEvent, PrivateMessageEvent sv_help = ''' 官方四格漫画更新(日文) [官漫132] 阅览指定话 '''.strip() sv = Service('pcr-comic', help_=sv_help, bundle='pcr订阅') def load_index(): with open(R.get('img/priconne/comic/index.json').path, encoding='utf8') as f: return json.load(f) def get_pic_name(id_): pre = 'episode_' end = '.png' return f'{pre}{id_}{end}' comic = sv.on_fullmatch('官漫', only_group=False)
定义: W_cheru = '切' ^ `CHERU_SET`+ 切噜词均以'切'开头,可用字符集为`CHERU_SET` L_cheru = {W_cheru ∪ `\\W`}* 切噜语由切噜词与标点符号连接而成 """ import re from itertools import zip_longest from salmon import Service, util, Bot from salmon.typing import CQEvent, T_State, GroupMessageEvent, PrivateMessageEvent sv = Service('pcr-cherugo', bundle='pcr娱乐', help_=''' [切噜一下] 转换为切噜语 [切噜~♪切啰巴切拉切蹦切蹦] 切噜语翻译 '''.strip()) CHERU_SET = '切卟叮咧哔唎啪啰啵嘭噜噼巴拉蹦铃' CHERU_DEKINAI = '切、切噜太长切不动勒切噜噜...' CHERU_DIC = {c: i for i, c in enumerate(CHERU_SET)} ENCODING = 'gb18030' rex_split = re.compile(r'\b', re.U) rex_word = re.compile(r'^\w+$', re.U) rex_cheru_word: re.Pattern = re.compile(rf'切[{CHERU_SET}]+', re.U) def grouper(iterable, n, fillvalue=None): args = [iter(iterable)] * n return zip_longest(*args, fillvalue=fillvalue)
import random from datetime import datetime from lxml import etree import salmon from salmon import Service, aiohttpx, scheduler, Bot from salmon.typing import CQEvent sv = Service('bangumi', enable_on_default=False, help_='蜜柑番剧更新推送') class Mikan: link_cache = set() rss_cache = [] @staticmethod def get_token(): return salmon.configs.mikan.MIKAN_TOKEN @staticmethod async def get_rss(): res = [] try: resp = await aiohttpx.get('https://mikanani.me/RSS/MyBangumi', params={'token': Mikan.get_token()}, timeout=10) rss = etree.XML(resp.content) except Exception as e: salmon.logger.error(f'[get_rss] Error: {e}') return [] for i in rss.xpath('/rss/channel/item'): link = i.find('./link').text description = i.find('./description').text pubDate = i.find('.//xmlns:pubDate', namespaces={'xmlns': 'https://mikanani.me/0.1/'}).text
import salmon from salmon import Bot, Service, priv from salmon.typing import (CQEvent, GroupMessageEvent, Message, PrivateMessageEvent, T_State) from salmon.util import DailyNumberLimiter sv = Service('_feedback_', manage_priv=priv.SUPERUSER, help_='[反馈] 后接反馈内容 联系维护组') _max = 1 lmt = DailyNumberLimiter(_max) EXCEED_NOTICE = f'您今天已经反馈过{_max}次了,请明早5点后再来!' feed_back = sv.on_fullmatch('反馈', aliases={'来杯咖啡', '来杯红茶'}, only_group=False) @feed_back.handle() async def feed_rec(bot: Bot, event: CQEvent, state: T_State): uid = event.user_id user_info = await bot.get_stranger_info(user_id=uid) nickname = user_info.get('nickname', '未知用户') if not lmt.check(uid): if isinstance(event, GroupMessageEvent): await feed_back.finish(f'>{nickname}\n您今天已经反馈过{_max}次了,请明早5点后再来!') elif isinstance(event, PrivateMessageEvent): await feed_back.finish(EXCEED_NOTICE) lmt.increase(uid) args = event.get_message() if args: state['text'] = args
import asyncio import traceback import salmon from salmon import Service, scheduler, Bot, util, priv from salmon.service import add_header from salmon.typing import CQEvent, PrivateMessageEvent, GroupMessageEvent, T_State, Message from salmon.modules.setu_hina.base import get_spec_image, get_setu, search_setu, check_path from salmon.modules.setu_hina.lolicon import get_config, get_group_config, set_group_config, lolicon_fetch_process HELP_MSG = ''' 来张 [keyword] 涩/色/瑟图 : 来张keyword的涩图(不指定关键字发送一张随机涩图) 提取图片pid : 获取指定id的p站图片,没有时发送链接 ''' sv = Service('setu', bundle='娱乐', help_=HELP_MSG) # 设置limiter tlmt = util.DailyNumberLimiter(get_config('base', 'daily_max')) flmt = util.FreqLimiter(get_config('base', 'freq_limit')) set_conf = sv.on_prefix('setu', only_group=False) setu = sv.on_rex( r'^[色涩瑟][图圖]$|^[来來发發给給]((?P<num>\d+)|(?:.*))[张張个個幅点點份丶](?P<keyword>.*?)[色涩瑟][图圖]$', only_group=False) get_pic = sv.on_prefix('提取图片', only_group=False) def check_lmt(uid, num): if uid in salmon.configs.SUPERUSERS: return 0, '' if not tlmt.check(uid): return 1, f"您今天已经冲过{get_config('base', 'daily_max')}次了,请明天再来~"
※黄骑四号位例外较多 ※对面羊驼或中后卫坦 有可能歪 ※我方羊驼算一号位 ※图片搬运自漪夢奈特''') sv_help = ''' [rank日服] rank推荐表 [rank台服] rank推荐表 [rank陆服] rank推荐表 [挖矿15001] 矿场余钻 [黄骑充电表] 黄骑1动规律 [谁是霸瞳] 角色别称查询 '''.strip() sv = Service('pcr-query', help_=sv_help, bundle='pcr查询') miner = sv.on_fullmatch('挖矿', aliases={'jjc钻石', '竞技场钻石', 'jjc钻石查询', '竞技场钻石查询'}, only_group=False) rank = sv.on_fullmatch('rank', aliases={'Rank', 'rank表', 'Rank表'}, only_group=False) yukari = sv.on_fullmatch('yukari-sheet', aliases={'黄骑充电', '酒鬼充电', '酒鬼充电表', '黄骑充电表'}, only_group=False) who_is = sv.on_prefix('谁是', only_group=False) is_who = sv.on_suffix('是谁', only_group=False) @miner.handle() async def miner_rec(bot: Bot, event: CQEvent, state: T_State): user_info = await bot.get_stranger_info(user_id=event.user_id) nickname = user_info.get('nickname', '未知用户') if isinstance(event, GroupMessageEvent): state['prompt'] = f'>{nickname}\n请发送当前竞技场排名' elif isinstance(event, PrivateMessageEvent): state['prompt'] = '请发送当前竞技场排名'
import random from salmon import Service, R, Bot from salmon.typing import CQEvent, Message, GroupMessageEvent, PrivateMessageEvent from salmon.util import DailyNumberLimiter sv = Service('pcr-login-bonus', bundle='pcr娱乐', help_='[签到] 给主さま盖章章') lmt = DailyNumberLimiter(1) login_presents = [ '扫荡券×5', '卢币×1000', '普通EXP药水×5', '宝石×50', '玛那×3000', '扫荡券×10', '卢币×1500', '普通EXP药水×15', '宝石×80', '白金转蛋券×1', '扫荡券×15', '卢币×2000', '上级精炼石×3', '宝石×100', '白金转蛋券×1', ] todo_list = [ '找伊绪老师上课', '给宫子买布丁', '和真琴一起寻找伤害优衣的人', '找小雪探讨女装', '与吉塔一起登上骑空艇', '和霞一起调查伤害优衣的人', '和佩可小姐一起吃午饭', '找小小甜心玩过家家', '帮碧寻找新朋友', '去真步真步王国', '找镜华补习数学', '陪胡桃排练话剧', '和初音一起午睡', '成为露娜的朋友', '帮铃莓打扫咲恋育幼院', '和静流小姐一起做巧克力', '去伊丽莎白农场给栞小姐送书', '观看慈乐之音的演出', '解救挂树的群友', '来一发十连', '井一发当期的限定池', '给妈妈买一束康乃馨', '购买黄金保值', '竞技场背刺', '去赛马场赛马', '用决斗为海马带来笑容', '成为魔法少女', '来几局日麻'
from salmon import Service, priv, Bot from salmon.typing import CQEvent, GroupMessageEvent, PrivateMessageEvent sv = Service('_help_', manage_priv=priv.SUPERUSER, visible=False) TOP_MANUAL = ''' ===================== - SalmonBot使用说明 - ===================== 发送方括号[]内的关键词即可触发 ※功能采取模块化管理,群管理可控制开关 [lssv] (群管理)查看所有功能开关 [反馈] 联系维护组 ===================== [帮助分组] 查看各功能分组 [帮助+分组名] 查看分组功能 [帮助+功能名] 查看功能帮助 [启用/禁用] (群管理)空格后接功能名 ======== ※除这里中写明外 另有其他隐藏功能:) ※隐藏功能属于赠品 不保证可用性 ※本bot开源,可自行搭建 ※服务器运行及开发维护需要成本,赞助支持请私戳维护 ※您的支持是本bot更新维护的动力 ※※调教时请注意使用频率,您的滥用可能会导致bot账号被封禁 '''.strip() Bundle_help = ''' ===================== [pcr会战] pcr会战相关功能 [pcr查询] pcr相关查询功能
from salmon import Service, scheduler svtw = Service('pcr-arena-reminder-tw', enable_on_default=False, help_='背刺时间提醒(台B)', bundle='pcr订阅') svjp = Service('pcr-arena-reminder-jp', enable_on_default=False, help_='背刺时间提醒(日)', bundle='pcr订阅') msg = '骑士君、准备好背刺了吗?' @scheduler.scheduled_job('cron', id='背刺提醒(台B)', hour='14', minute='45', jitter=20) async def pcr_reminder_tw(): await svtw.broadcast(msg, 'pcr-reminder-tw', 0.2) @scheduler.scheduled_job('cron', id='背刺提醒(日)', hour='13', minute='45', jitter=20) async def pcr_reminder_jp(): await svjp.broadcast(msg, 'pcr-reminder-jp', 0.2)
import salmon from salmon import Bot, Service, priv, scheduler from salmon.typing import CQEvent from salmon.modules.check.check import Check sv = Service('check', use_priv=priv.SUPERUSER, manage_priv=priv.SUPERUSER, visible=False) MAX_PERFORMANCE_PERCENT = salmon.configs.check.MAX_PERFORMANCE_PERCENT PROCESS_NAME_LIST = salmon.configs.check.PROCESS_NAME_LIST check = Check(salmon.configs.check.PROCESS_NAME_LIST) check_self = sv.on_fullmatch(('自检', '自檢', '自我检查', '自我檢查'), only_group=False) @check_self.handle() async def _(bot: Bot, event: CQEvent): check_report_admin = await check.get_check_info() if check_report_admin: await bot.send(event, check_report_admin) else: salmon.logger.error("Not found Check Report") await bot.send(event, "[ERROR]Not found Check Report") @scheduler.scheduled_job('cron', id='自我检查', hour='*/3', minute='13') async def check_task(): weihu = salmon.configs.SUPERUSERS[0] result = await check.get_check_easy() for bot in salmon.get_bot_list(): await bot.send_private_msg(user_id=weihu, message=result)
import abc from dataclasses import dataclass from bs4 import BeautifulSoup import salmon from salmon import aiohttpx, Service, scheduler, Bot from salmon.typing import List, Union, CQEvent svtw = Service('pcr-news-tw', bundle='pcr订阅', help_='台服新闻') svbl = Service('pcr-news-bili', bundle='pcr订阅', help_='B服新闻') @dataclass class Item: idx: Union[str, int] content: str = "" def __eq__(self, other): return self.idx == other.idx class BaseSpider(abc.ABC): url = None src_name = None idx_cache = set() item_cache = [] @classmethod async def get_response(cls) -> aiohttpx.Response: resp = await aiohttpx.get(cls.url) resp.raise_for_status() return resp
from salmon.typing import MessageSegment, Message, CQEvent, GroupMessageEvent, PrivateMessageEvent from salmon.modules.priconne.pcr_data import _pcr_data, chara PATCH_SIZE = 32 PREPARE_TIME = 5 A_TURN_TIME = 20 D_TURN_TIME = 12 TURN_NUMBER = 5 DB_PATH_AVATAR = os.path.expanduser("~/.salmon/pcr_avatar_guess.db") DB_PATH_DESC = os.path.expanduser("~/.salmon/pcr_desc_guess.db") BLACKLIST_ID = [1072, 1908, 4031, 9000] sva = Service( "pcr-avatar-guess", bundle="pcr娱乐", help_=""" [猜头像] 猜猜bot随机发送的头像的一小部分来自哪位角色 [猜头像排行] 显示小游戏的群排行榜(只显示前十) """.strip(), ) svd = Service("pcr-desc-guess", bundle="pcr娱乐", help_=""" [猜角色] 猜猜bot在描述哪位角色 [猜角色排行] 显示小游戏的群排行榜(只显示前十) """.strip()) avatar_rank = sva.on_fullmatch('猜头像排行', aliases={'猜头像排名', '猜头像排行榜', '猜头像群排行'}, only_group=False) desc_rank = svd.on_fullmatch('猜角色排行',
from salmon.modules.priconne.pcr_data import chara from salmon.modules.priconne.pool import Gacha try: import ujson as json except: import json gacha_help = ''' [来发十连] 转蛋模拟 [来发单抽] 转蛋模拟 [来一井] 3w钻! [查看卡池] 模拟卡池&出率 [切换卡池] 更换模拟卡池 '''.strip() sv = Service('gacha', bundle='pcr查询', help_=gacha_help) jewel_limit = DailyNumberLimiter(6000) tenjo_limit = DailyNumberLimiter(1) JEWEL_EXCEED_NOTICE = f'您今天已经抽过{jewel_limit.max}钻了,欢迎明早5点后再来!' TENJO_EXCEED_NOTICE = f'您今天已经抽过{tenjo_limit.max}张天井券了,欢迎明早5点后再来!' POOL = ('MIX', 'JP', 'TW', 'BL') DEFAULT_POOL = POOL[0] _pool_config_file = os.path.expanduser('~/.salmon/group_pool_config.json') _group_pool = {} try: with open(_pool_config_file, encoding='utf8') as f: _group_pool = json.load(f) except FileNotFoundError as e:
import pytz import aiohttp from datetime import datetime from salmon import Service, scheduler sv = Service('hourcall', enable_on_default=False, help_='时报') tz = pytz.timezone('Asia/Shanghai') @scheduler.scheduled_job('cron', id='整点报时', hour='*') async def hour_call(): now = datetime.now(tz) nowtime = now.strftime("%H:%M") async with aiohttp.request('POST', 'https://v1.jinrishici.com/all.json') as resp: res = await resp.json() poem = res["content"] title = res["origin"] auth = res["author"] msg = f"现在时间: {nowtime}\n{poem}\n————{auth}『{title}』" await sv.broadcast(msg, 'hourcall', 0)
from time import time from urllib.parse import quote_plus import aiohttp import salmon from salmon import Bot, Service from salmon.typing import CQEvent, MessageEvent, ActionFailed, T_State, GroupMessageEvent, PrivateMessageEvent try: import ujson as json except ImportError: import json TXT = ''' [翻译] 机器翻译 '''.strip() sv = Service('机器翻译', bundle='查询', help_=TXT) app_id = salmon.configs.translate.tencent_app_id app_key = salmon.configs.translate.tencent_app_key def loadsJson(dict: str) -> dict: return json.loads(dict) translate = sv.on_fullmatch('翻译', aliases={'机翻'}, only_group=False) async def getReqSign(params: dict) -> str: keys = [] for key in sorted(params):
import requests import random from nonebot.adapters.cqhttp.exception import NetworkError from salmon import priv, Service, R, Bot import salmon from salmon.util import DailyNumberLimiter, FreqLimiter from salmon.typing import CQEvent, T_State, GroupMessageEvent, PrivateMessageEvent, Message _max = 5 EXCEED_NOTICE = f'您今天已经冲过{_max}次了,请明早5点后再来!' CD_NOTICE = '您冲得太快了,请稍候再冲' _nlmt = DailyNumberLimiter(_max) _flmt = FreqLimiter(5) sv = Service('setu', manage_priv=priv.SUPERUSER, enable_on_default=False, visible=False) setu_folder = R.img('setu/').path if not setu_folder: os.makedirs(os.path.expanduser(setu_folder), exist_ok=True) def setu_gener(): while True: filelist = os.listdir(setu_folder) random.shuffle(filelist) for filename in filelist: if os.path.isfile(os.path.join(setu_folder, filename)): yield R.img('setu/', filename) setu_gener = setu_gener()
from nonebot.adapters.cqhttp import GroupIncreaseNoticeEvent, GroupDecreaseNoticeEvent import salmon from salmon import Service, Bot from salmon.typing import Message sv1 = Service('group-leave-notice', help_='退群通知') sv2 = Service('group-welcome', help_='入群欢迎') group_decrease = sv1.on_notice() group_increase = sv2.on_notice() @group_decrease.handle() async def leave_notice(bot: Bot, event: GroupDecreaseNoticeEvent): if not event.is_tome(): user_info = await bot.get_stranger_info(user_id=event.user_id) nickname = user_info.get('nickname', '未知用户') await group_decrease.finish(f'{nickname}({event.user_id})退群了.') @group_increase.handle() async def increace_welcome(bot: Bot, event: GroupIncreaseNoticeEvent): if event.user_id == event.self_id: return # ignore myself welcomes = salmon.configs.groupmaster.increase_welcome gid = event.group_id if gid in welcomes: await bot.send(event, welcomes[gid], at_sender=True) # elif 'default' in welcomes: # await bot.send(event, welcomes[default], at_sender=True)
import random import salmon from salmon import Bot, Service, util from salmon.typing import CQEvent sv = Service('random-repeater', help_='随机复读机') PROB_A = 1.4 group_stat = {} # group_id: (last_msg, is_repeated, p) random_repeater = sv.on_message() @random_repeater.handle() async def rd(bot: Bot, event: CQEvent): group_id = event.group_id msg = str(event.message) if group_id not in group_stat: group_stat[group_id] = (msg, False, 0) return last_msg, is_repeated, p = group_stat[group_id] if last_msg == msg: # 群友正在复读 if not is_repeated: # 机器人尚未复读过,开始测试复读 if random.random() < p: # 概率测试通过,复读并设flag try: group_stat[group_id] = (msg, True, 0) await bot.send(event, util.filt_message(event.message)) except Exception as e: salmon.logger.error(f'复读失败: {type(e)}') salmon.logger.exception(e) else: # 概率测试失败,蓄力