koishi-plugin-common
Advanced tools
Comparing version 4.0.0-beta.19 to 4.0.0
@@ -16,4 +16,5 @@ import { Context, User, Channel, Argv } from 'koishi-core'; | ||
admin?: boolean; | ||
generateToken?: () => string; | ||
} | ||
export default function apply(ctx: Context, config?: AdminConfig): void; | ||
export {}; |
@@ -12,3 +12,2 @@ import { Context } from 'koishi-core'; | ||
export declare const name = "common"; | ||
export declare const disposable = true; | ||
export declare function apply(ctx: Context, config?: Config): void; |
@@ -21,5 +21,3 @@ var __create = Object.create; | ||
var __toModule = (module2) => { | ||
if (module2 && module2.__esModule) | ||
return module2; | ||
return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", {value: module2, enumerable: true})), module2); | ||
return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2); | ||
}; | ||
@@ -33,3 +31,2 @@ | ||
debug: () => apply2, | ||
disposable: () => disposable, | ||
handler: () => apply3, | ||
@@ -83,5 +80,5 @@ name: () => name, | ||
if (options.set || options.unset) { | ||
const notFound = import_koishi_utils.difference(flags, import_koishi_utils.enumKeys(map)); | ||
const notFound = (0, import_koishi_utils.difference)(flags, (0, import_koishi_utils.enumKeys)(map)); | ||
if (notFound.length) | ||
return import_koishi_utils.template("admin.unknown-flag", notFound.join(", ")); | ||
return (0, import_koishi_utils.template)("admin.unknown-flag", notFound.join(", ")); | ||
for (const name2 of flags) { | ||
@@ -93,3 +90,3 @@ options.set ? target.flag |= map[name2] : target.flag &= ~map[name2]; | ||
if (options.list) { | ||
return import_koishi_utils.template("admin.all-flags", import_koishi_utils.enumKeys(map).join(", ")); | ||
return (0, import_koishi_utils.template)("admin.all-flags", (0, import_koishi_utils.enumKeys)(map).join(", ")); | ||
} | ||
@@ -104,4 +101,4 @@ let flag = target.flag; | ||
if (!keys.length) | ||
return import_koishi_utils.template("admin.no-flags"); | ||
return import_koishi_utils.template("admin.current-flags", keys.join(", ")); | ||
return (0, import_koishi_utils.template)("admin.no-flags"); | ||
return (0, import_koishi_utils.template)("admin.current-flags", keys.join(", ")); | ||
} | ||
@@ -129,7 +126,7 @@ import_koishi_core.Command.prototype.adminUser = function(callback) { | ||
if (!data) | ||
return import_koishi_utils.template("admin.user-not-found"); | ||
return (0, import_koishi_utils.template)("admin.user-not-found"); | ||
if (session.user.authority <= data.authority) { | ||
return import_koishi_utils.template("internal.low-authority"); | ||
return (0, import_koishi_utils.template)("internal.low-authority"); | ||
} else { | ||
target = import_koishi_utils.observe(data, (diff) => database.setUser(platform, userId, diff), `user ${options.target}`); | ||
target = (0, import_koishi_utils.observe)(data, (diff) => database.setUser(platform, userId, diff), `user ${options.target}`); | ||
} | ||
@@ -142,7 +139,7 @@ } | ||
return result; | ||
if (!import_koishi_utils.difference(Object.keys(target._diff), diffKeys).length) { | ||
return import_koishi_utils.template("admin.user-unchanged"); | ||
if (!(0, import_koishi_utils.difference)(Object.keys(target._diff), diffKeys).length) { | ||
return (0, import_koishi_utils.template)("admin.user-unchanged"); | ||
} | ||
await target._update(); | ||
return import_koishi_utils.template("admin.user-updated"); | ||
return (0, import_koishi_utils.template)("admin.user-updated"); | ||
}); | ||
@@ -164,6 +161,6 @@ return command; | ||
if (!data) | ||
return import_koishi_utils.template("admin.channel-not-found"); | ||
target = import_koishi_utils.observe(data, (diff) => database.setChannel(platform, channelId, diff), `channel ${options.target}`); | ||
return (0, import_koishi_utils.template)("admin.channel-not-found"); | ||
target = (0, import_koishi_utils.observe)(data, (diff) => database.setChannel(platform, channelId, diff), `channel ${options.target}`); | ||
} else { | ||
return import_koishi_utils.template("admin.not-in-group"); | ||
return (0, import_koishi_utils.template)("admin.not-in-group"); | ||
} | ||
@@ -174,6 +171,6 @@ const result = await callback({...argv, target}, ...args); | ||
if (!Object.keys(target._diff).length) { | ||
return import_koishi_utils.template("admin.channel-unchanged"); | ||
return (0, import_koishi_utils.template)("admin.channel-unchanged"); | ||
} | ||
await target._update(); | ||
return import_koishi_utils.template("admin.channel-updated"); | ||
return (0, import_koishi_utils.template)("admin.channel-updated"); | ||
}); | ||
@@ -192,12 +189,12 @@ return command; | ||
if (user.name) { | ||
return import_koishi_utils.template("callme.current", session.username); | ||
return (0, import_koishi_utils.template)("callme.current", session.username); | ||
} else { | ||
return import_koishi_utils.template("callme.unnamed"); | ||
return (0, import_koishi_utils.template)("callme.unnamed"); | ||
} | ||
} else if (name2 === user.name) { | ||
return import_koishi_utils.template("callme.unchanged"); | ||
return (0, import_koishi_utils.template)("callme.unchanged"); | ||
} else if (!(name2 = name2.trim())) { | ||
return import_koishi_utils.template("callme.empty"); | ||
return (0, import_koishi_utils.template)("callme.empty"); | ||
} else if (name2.includes("[CQ:")) { | ||
return import_koishi_utils.template("callme.invalid"); | ||
return (0, import_koishi_utils.template)("callme.invalid"); | ||
} | ||
@@ -210,9 +207,9 @@ const result = ctx.bail("common/callme", name2, session); | ||
await user._update(); | ||
return import_koishi_utils.template("callme.updated", session.username); | ||
return (0, import_koishi_utils.template)("callme.updated", session.username); | ||
} catch (error) { | ||
if (error[Symbol.for("koishi.error-type")] === "duplicate-entry") { | ||
return import_koishi_utils.template("callme.duplicate"); | ||
return (0, import_koishi_utils.template)("callme.duplicate"); | ||
} else { | ||
ctx.logger("common").warn(error); | ||
return import_koishi_utils.template("callme.failed"); | ||
return (0, import_koishi_utils.template)("callme.failed"); | ||
} | ||
@@ -222,4 +219,5 @@ } | ||
const tokens = {}; | ||
function generateToken(session, pending) { | ||
const token = "koishi/" + import_koishi_utils.Random.uuid(); | ||
const {generateToken = () => "koishi/" + import_koishi_utils.Random.uuid()} = config; | ||
function generate(session, pending) { | ||
const token = generateToken(); | ||
tokens[token] = [session.platform, session.userId, pending]; | ||
@@ -230,4 +228,4 @@ setTimeout(() => delete tokens[token], 5 * import_koishi_utils.Time.minute); | ||
ctx.command("user/bind", "绑定到账号", {authority: 0}).action(({session}) => { | ||
const token = generateToken(session, +(session.subtype === "group")); | ||
return import_koishi_utils.template("bind.generated-1", token); | ||
const token = generate(session, +(session.subtype === "group")); | ||
return (0, import_koishi_utils.template)("bind.generated-1", token); | ||
}); | ||
@@ -244,17 +242,17 @@ ctx.middleware(async (session, next) => { | ||
await user._update(); | ||
return session.send(import_koishi_utils.template("bind.success")); | ||
return session.send((0, import_koishi_utils.template)("bind.success")); | ||
} else { | ||
const user = await session.observeUser(["authority", data[0]]); | ||
if (!user.authority) | ||
return session.send(import_koishi_utils.template("internal.low-authority")); | ||
return session.send((0, import_koishi_utils.template)("internal.low-authority")); | ||
if (user[data[0]]) | ||
return session.send(import_koishi_utils.template("bind.failed")); | ||
return session.send((0, import_koishi_utils.template)("bind.failed")); | ||
delete tokens[session.content]; | ||
if (data[2]) { | ||
const token = generateToken(session, -1); | ||
return session.send(import_koishi_utils.template("bind.generated-2", token)); | ||
const token = generate(session, -1); | ||
return session.send((0, import_koishi_utils.template)("bind.generated-2", token)); | ||
} else { | ||
user[data[0]] = data[1]; | ||
await user._update(); | ||
return session.send(import_koishi_utils.template("bind.success")); | ||
return session.send((0, import_koishi_utils.template)("bind.success")); | ||
} | ||
@@ -265,11 +263,11 @@ } | ||
const authority = Number(value); | ||
if (!import_koishi_utils.isInteger(authority) || authority < 0) | ||
if (!(0, import_koishi_utils.isInteger)(authority) || authority < 0) | ||
return "参数错误。"; | ||
if (authority >= session.user.authority) | ||
return import_koishi_utils.template("internal.low-authority"); | ||
return (0, import_koishi_utils.template)("internal.low-authority"); | ||
if (authority === target.authority) | ||
return import_koishi_utils.template("admin.user-unchanged"); | ||
return (0, import_koishi_utils.template)("admin.user-unchanged"); | ||
await ctx.database.createUser(session.platform, target[session.platform], {authority}); | ||
target._merge({authority}); | ||
return import_koishi_utils.template("admin.user-updated"); | ||
return (0, import_koishi_utils.template)("admin.user-updated"); | ||
}); | ||
@@ -286,3 +284,3 @@ ctx.command("user.flag [-s|-S] [...flags]", "标记信息", {authority: 3}).userFields(["flag"]).option("list", "-l 标记列表").option("set", "-s 添加标记", {authority: 4}).option("unset", "-S 删除标记", {authority: 4}).adminUser(flagAction.bind(null, import_koishi_core.User.Flag)); | ||
const count = +value; | ||
if (!import_koishi_utils.isInteger(count) || count < 0) | ||
if (!(0, import_koishi_utils.isInteger)(count) || count < 0) | ||
return "参数错误。"; | ||
@@ -340,6 +338,6 @@ target.usage[name2] = count; | ||
if (assignee === target.assignee) | ||
return import_koishi_utils.template("admin.channel-unchanged"); | ||
return (0, import_koishi_utils.template)("admin.channel-unchanged"); | ||
await ctx.database.createChannel(session.platform, session.channelId, {assignee}); | ||
target._merge({assignee}); | ||
return import_koishi_utils.template("admin.channel-updated"); | ||
return (0, import_koishi_utils.template)("admin.channel-updated"); | ||
}); | ||
@@ -452,4 +450,4 @@ ctx.command("channel.flag [-s|-S] [...flags]", "标记信息", {authority: 3}).channelFields(["flag"]).option("list", "-l 标记列表").option("set", "-s 添加标记", {authority: 4}).option("unset", "-S 删除标记", {authority: 4}).adminChannel(flagAction.bind(null, import_koishi_core.Channel.Flag)); | ||
async function handleMessage(deps, template3, session) { | ||
const params = import_koishi_utils2.pick(session, ["platform", "channelId", "groupId", "userId", "selfId"]); | ||
Object.assign(params, import_koishi_utils2.pick(session.author, ["username", "nickname"])); | ||
const params = (0, import_koishi_utils2.pick)(session, ["platform", "channelId", "groupId", "userId", "selfId"]); | ||
Object.assign(params, (0, import_koishi_utils2.pick)(session.author, ["username", "nickname"])); | ||
await Promise.all(deps.map(async (key) => { | ||
@@ -459,3 +457,3 @@ const callback = tasks[key]; | ||
})); | ||
logger.debug(import_koishi_utils2.interpolate(template3, params)); | ||
logger.debug((0, import_koishi_utils2.interpolate)(template3, params)); | ||
} | ||
@@ -509,3 +507,3 @@ ctx.intersect((session) => { | ||
respondents.length && ctx.middleware((session, next) => { | ||
const message = import_koishi_core2.simplify(session.content); | ||
const message = (0, import_koishi_core2.simplify)(session.content); | ||
for (const {match, reply} of respondents) { | ||
@@ -521,2 +519,9 @@ const capture = typeof match === "string" ? message === match && [message] : message.match(match); | ||
// packages/plugin-common/src/repeater.ts | ||
var import_koishi_core3 = __toModule(require("koishi-core")); | ||
function onRepeat(options) { | ||
if (!options || typeof options !== "object") | ||
return options; | ||
const {minTimes, probability = 1} = options; | ||
return ({repeated, times, content}) => times >= minTimes && !repeated && import_koishi_core3.Random.bool(probability) ? content : ""; | ||
} | ||
function apply4(ctx, options = {}) { | ||
@@ -550,3 +555,3 @@ ctx = ctx.group(); | ||
const check = (handle) => { | ||
const text = handle == null ? void 0 : handle(state, content, userId); | ||
const text = handle == null ? void 0 : handle(state, session); | ||
return text && next(() => { | ||
@@ -560,3 +565,3 @@ ctx.emit("repeater", session, state); | ||
state.users[userId] = (state.users[userId] || 0) + 1; | ||
return check(options.onRepeat) || next(); | ||
return check(onRepeat(options.onRepeat)) || next(); | ||
} | ||
@@ -575,3 +580,3 @@ const result = check(options.onInterrupt); | ||
// packages/plugin-common/src/sender.ts | ||
var import_koishi_core3 = __toModule(require("koishi-core")); | ||
var import_koishi_core4 = __toModule(require("koishi-core")); | ||
var import_koishi_utils3 = __toModule(require("koishi-utils")); | ||
@@ -591,3 +596,3 @@ import_koishi_utils3.template.set("common", { | ||
if (!message) | ||
return import_koishi_utils3.template("common.expect-text"); | ||
return (0, import_koishi_utils3.template)("common.expect-text"); | ||
if (options.escape) { | ||
@@ -597,9 +602,9 @@ message = import_koishi_utils3.segment.unescape(message); | ||
if (options.forceAnonymous) { | ||
message = import_koishi_utils3.segment("anonymous") + message; | ||
message = (0, import_koishi_utils3.segment)("anonymous") + message; | ||
} else if (options.anonymous) { | ||
message = import_koishi_utils3.segment("anonymous", {ignore: true}) + message; | ||
message = (0, import_koishi_utils3.segment)("anonymous", {ignore: true}) + message; | ||
} | ||
return message; | ||
}); | ||
const operator = import_koishi_utils3.makeArray(config.operator); | ||
const operator = (0, import_koishi_utils3.makeArray)(config.operator); | ||
if (operator.length) { | ||
@@ -609,6 +614,6 @@ const feedbacks = {}; | ||
if (!text) | ||
return import_koishi_utils3.template("common.expect-text"); | ||
return (0, import_koishi_utils3.template)("common.expect-text"); | ||
const {username: name2, userId} = session; | ||
const nickname = name2 === "" + userId ? userId : `${name2} (${userId})`; | ||
const message = import_koishi_utils3.template("common.feedback-receive", nickname, text); | ||
const message = (0, import_koishi_utils3.template)("common.feedback-receive", nickname, text); | ||
const delay = ctx.app.options.delay.broadcast; | ||
@@ -618,8 +623,8 @@ const data = [session.sid, session.channelId]; | ||
if (index && delay) | ||
await import_koishi_utils3.sleep(delay); | ||
const [platform, userId2] = import_koishi_core3.Argv.parsePid(operator[index]); | ||
await (0, import_koishi_utils3.sleep)(delay); | ||
const [platform, userId2] = import_koishi_core4.Argv.parsePid(operator[index]); | ||
const id = await ctx.getBot(platform).sendPrivateMessage(userId2, message); | ||
feedbacks[id] = data; | ||
} | ||
return import_koishi_utils3.template("common.feedback-success"); | ||
return (0, import_koishi_utils3.template)("common.feedback-success"); | ||
}); | ||
@@ -636,10 +641,10 @@ ctx.middleware((session, next) => { | ||
} | ||
const relay = import_koishi_utils3.makeArray(config.relay); | ||
const relay = (0, import_koishi_utils3.makeArray)(config.relay); | ||
const relayMap = {}; | ||
async function sendRelay(session, {destination, selfId, lifespan = import_koishi_utils3.Time.hour}) { | ||
const [platform, channelId] = import_koishi_core3.Argv.parsePid(destination); | ||
const [platform, channelId] = import_koishi_core4.Argv.parsePid(destination); | ||
const bot = ctx.getBot(platform, selfId); | ||
if (!session.parsed.content) | ||
return; | ||
const content = import_koishi_utils3.template("common.relay", session.username, session.parsed.content); | ||
const content = (0, import_koishi_utils3.template)("common.relay", session.username, session.parsed.content); | ||
const id = await bot.sendMessage(channelId, content); | ||
@@ -665,3 +670,3 @@ relayMap[id] = {source: destination, destination: session.cid, selfId: session.selfId, lifespan}; | ||
if (!message) | ||
return import_koishi_utils3.template("common.expect-text"); | ||
return (0, import_koishi_utils3.template)("common.expect-text"); | ||
if (!options.only) { | ||
@@ -676,3 +681,3 @@ await ctx.broadcast(message, options.forced); | ||
if (!options.forced) { | ||
groups = groups.filter((g) => !(g.flag & import_koishi_core3.Channel.Flag.silent)); | ||
groups = groups.filter((g) => !(g.flag & import_koishi_core4.Channel.Flag.silent)); | ||
} | ||
@@ -684,6 +689,6 @@ await session.bot.broadcast(groups.map((g) => g.id.slice(session.platform["length"] + 1)), message); | ||
if (!message) | ||
return import_koishi_utils3.template("common.expect-command"); | ||
return (0, import_koishi_utils3.template)("common.expect-command"); | ||
if (options.member) { | ||
if (session.subtype === "private") { | ||
return import_koishi_utils3.template("common.invalid-private-member"); | ||
return (0, import_koishi_utils3.template)("common.invalid-private-member"); | ||
} | ||
@@ -694,5 +699,5 @@ options.channel = session.cid; | ||
if (!options.user && !options.channel) { | ||
return import_koishi_utils3.template("common.expect-context"); | ||
return (0, import_koishi_utils3.template)("common.expect-context"); | ||
} | ||
const sess = new import_koishi_core3.Session(ctx.app, session); | ||
const sess = new import_koishi_core4.Session(ctx.app, session); | ||
sess.send = session.send.bind(session); | ||
@@ -703,6 +708,6 @@ sess.sendQueued = session.sendQueued.bind(session); | ||
} else if (options.channel !== session.cid) { | ||
sess.channelId = import_koishi_core3.Argv.parsePid(options.channel)[1]; | ||
sess.channelId = import_koishi_core4.Argv.parsePid(options.channel)[1]; | ||
sess.cid = `${sess.platform}:${sess.channelId}`; | ||
sess.subtype = "group"; | ||
await sess.observeChannel(import_koishi_core3.Channel.fields); | ||
await sess.observeChannel(import_koishi_core4.Channel.fields); | ||
} else { | ||
@@ -712,7 +717,7 @@ sess.channel = session.channel; | ||
if (options.user && options.user !== session.uid) { | ||
sess.userId = sess.author.userId = import_koishi_core3.Argv.parsePid(options.user)[1]; | ||
sess.userId = sess.author.userId = import_koishi_core4.Argv.parsePid(options.user)[1]; | ||
sess.uid = `${sess.platform}:${sess.userId}`; | ||
const user = await sess.observeUser(import_koishi_core3.User.fields); | ||
const user = await sess.observeUser(import_koishi_core4.User.fields); | ||
if (session.user.authority <= user.authority) { | ||
return import_koishi_utils3.template("internal.low-authority"); | ||
return (0, import_koishi_utils3.template)("internal.low-authority"); | ||
} | ||
@@ -735,3 +740,2 @@ } else { | ||
var name = "common"; | ||
var disposable = true; | ||
function apply6(ctx, config = {}) { | ||
@@ -738,0 +742,0 @@ ctx.command("common", "基础功能"); |
@@ -1,2 +0,2 @@ | ||
import { Context } from 'koishi-core'; | ||
import { Context, Session } from 'koishi-core'; | ||
declare module 'koishi-core' { | ||
@@ -13,8 +13,12 @@ interface EventMap { | ||
} | ||
declare type RepeatHandler = (state: RepeatState, content: string, userId: string) => void | string; | ||
declare type StateCallback = (state: RepeatState, session: Session) => void | string; | ||
interface RepeatHandler { | ||
minTimes: number; | ||
probability?: number; | ||
} | ||
export interface RepeaterOptions { | ||
onRepeat?: RepeatHandler; | ||
onInterrupt?: RepeatHandler; | ||
onRepeat?: RepeatHandler | StateCallback; | ||
onInterrupt?: StateCallback; | ||
} | ||
export default function apply(ctx: Context, options?: RepeaterOptions): void; | ||
export {}; |
{ | ||
"name": "koishi-plugin-common", | ||
"description": "Common plugins for Koishi", | ||
"version": "4.0.0-beta.19", | ||
"version": "4.0.0", | ||
"main": "dist/index.js", | ||
@@ -33,8 +33,8 @@ "typings": "dist/index.d.ts", | ||
"peerDependencies": { | ||
"koishi-core": "^3.0.0", | ||
"koishi-core": "^3.0.1", | ||
"koishi-utils": "^4.0.1" | ||
}, | ||
"devDependencies": { | ||
"koishi-test-utils": "^6.0.0-beta.9" | ||
"koishi-test-utils": "^6.0.0-beta.10" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
83676
811
1