koishi-plugin-common
Advanced tools
Comparing version 3.0.0-beta.2 to 3.0.0-beta.3
import { Observed } from 'koishi-utils'; | ||
import { Context, Session, User, Group } from 'koishi-core'; | ||
declare type ActionCallback<T extends {}, K extends keyof T> = (this: Context, session: Session<'authority'>, target: Observed<Pick<T, K>>, ...args: string[]) => Promise<any>; | ||
declare type ActionCallback<T extends {}, K extends keyof T> = (this: Context, session: Session<'authority'>, target: Observed<Pick<T, K>>, ...args: string[]) => Promise<void | string>; | ||
export interface ActionItem<T extends {}> { | ||
@@ -5,0 +5,0 @@ callback: ActionCallback<T, keyof T>; |
@@ -23,7 +23,7 @@ "use strict"; | ||
if (!koishi_utils_1.isInteger(authority) || authority < 0) | ||
return session.$send('参数错误。'); | ||
return '参数错误。'; | ||
if (authority >= session.$user.authority) | ||
return session.$send('权限不足。'); | ||
return '权限不足。'; | ||
if (authority === user.authority) { | ||
return session.$send('用户权限未改动。'); | ||
return '用户权限未改动。'; | ||
} | ||
@@ -33,3 +33,3 @@ else { | ||
await user._update(); | ||
return session.$send('用户权限已修改。'); | ||
return '用户权限已修改。'; | ||
} | ||
@@ -40,6 +40,6 @@ }, ['authority']); | ||
if (!flags.length) | ||
return session.$send(`可用的标记有 ${userFlags.join(', ')}。`); | ||
return `可用的标记有 ${userFlags.join(', ')}。`; | ||
const notFound = koishi_utils_1.difference(flags, userFlags); | ||
if (notFound.length) | ||
return session.$send(`未找到标记 ${notFound.join(', ')}。`); | ||
return `未找到标记 ${notFound.join(', ')}。`; | ||
for (const name of flags) { | ||
@@ -49,3 +49,3 @@ user.flag |= koishi_core_1.User.Flag[name]; | ||
await user._update(); | ||
return session.$send('用户信息已修改。'); | ||
return '用户信息已修改。'; | ||
}, ['flag']); | ||
@@ -55,6 +55,6 @@ exports.UserAction.add('unsetFlag', async (session, user, ...flags) => { | ||
if (!flags.length) | ||
return session.$send(`可用的标记有 ${userFlags.join(', ')}。`); | ||
return `可用的标记有 ${userFlags.join(', ')}。`; | ||
const notFound = koishi_utils_1.difference(flags, userFlags); | ||
if (notFound.length) | ||
return session.$send(`未找到标记 ${notFound.join(', ')}。`); | ||
return `未找到标记 ${notFound.join(', ')}。`; | ||
for (const name of flags) { | ||
@@ -64,3 +64,3 @@ user.flag &= ~koishi_core_1.User.Flag[name]; | ||
await user._update(); | ||
return session.$send('用户信息已修改。'); | ||
return '用户信息已修改。'; | ||
}, ['flag']); | ||
@@ -70,6 +70,6 @@ exports.UserAction.add('setUsage', async (session, user, name, _count) => { | ||
if (!koishi_utils_1.isInteger(count) || count < 0) | ||
return session.$send('参数错误。'); | ||
return '参数错误。'; | ||
user.usage[name] = count; | ||
await user._update(); | ||
return session.$send('用户信息已修改。'); | ||
return '用户信息已修改。'; | ||
}, ['usage']); | ||
@@ -86,13 +86,13 @@ exports.UserAction.add('clearUsage', async (session, user, ...commands) => { | ||
await user._update(); | ||
return session.$send('用户信息已修改。'); | ||
return '用户信息已修改。'; | ||
}, ['usage']); | ||
exports.UserAction.add('setTimer', async (session, user, name, offset) => { | ||
if (!name || !offset) | ||
return session.$send('参数不足。'); | ||
return '参数不足。'; | ||
const timestamp = koishi_utils_1.parseTime(offset); | ||
if (!timestamp) | ||
return session.$send('请输入合法的时间。'); | ||
return '请输入合法的时间。'; | ||
user.timers[name] = Date.now() + timestamp; | ||
await user._update(); | ||
return session.$send('用户信息已修改。'); | ||
return '用户信息已修改。'; | ||
}, ['timers']); | ||
@@ -109,3 +109,3 @@ exports.UserAction.add('clearTimer', async (session, user, ...commands) => { | ||
await user._update(); | ||
return session.$send('用户信息已修改。'); | ||
return '用户信息已修改。'; | ||
}, ['timers']); | ||
@@ -115,6 +115,6 @@ exports.GroupAction.add('setFlag', async (session, group, ...flags) => { | ||
if (!flags.length) | ||
return session.$send(`可用的标记有 ${groupFlags.join(', ')}。`); | ||
return `可用的标记有 ${groupFlags.join(', ')}。`; | ||
const notFound = koishi_utils_1.difference(flags, groupFlags); | ||
if (notFound.length) | ||
return session.$send(`未找到标记 ${notFound.join(', ')}。`); | ||
return `未找到标记 ${notFound.join(', ')}。`; | ||
for (const name of flags) { | ||
@@ -124,3 +124,3 @@ group.flag |= koishi_core_1.Group.Flag[name]; | ||
await group._update(); | ||
return session.$send('群信息已修改。'); | ||
return '群信息已修改。'; | ||
}, ['flag']); | ||
@@ -130,6 +130,6 @@ exports.GroupAction.add('unsetFlag', async (session, group, ...flags) => { | ||
if (!flags.length) | ||
return session.$send(`可用的标记有 ${groupFlags.join(', ')}。`); | ||
return `可用的标记有 ${groupFlags.join(', ')}。`; | ||
const notFound = koishi_utils_1.difference(flags, groupFlags); | ||
if (notFound.length) | ||
return session.$send(`未找到标记 ${notFound.join(', ')}。`); | ||
return `未找到标记 ${notFound.join(', ')}。`; | ||
for (const name of flags) { | ||
@@ -139,3 +139,3 @@ group.flag &= ~koishi_core_1.Group.Flag[name]; | ||
await group._update(); | ||
return session.$send('群信息已修改。'); | ||
return '群信息已修改。'; | ||
}, ['flag']); | ||
@@ -145,6 +145,6 @@ exports.GroupAction.add('setAssignee', async (session, group, _assignee) => { | ||
if (!koishi_utils_1.isInteger(assignee) || assignee < 0) | ||
return session.$send('参数错误。'); | ||
return '参数错误。'; | ||
group.assignee = assignee; | ||
await group._update(); | ||
return session.$send('群信息已修改。'); | ||
return '群信息已修改。'; | ||
}, ['assignee']); | ||
@@ -161,10 +161,10 @@ function apply(ctx) { | ||
if ('user' in options && isGroup) | ||
return session.$send('不能同时目标为指定用户和群。'); | ||
return '不能同时目标为指定用户和群。'; | ||
const actionMap = isGroup ? exports.GroupAction.commands : exports.UserAction.commands; | ||
const actionList = Object.keys(actionMap).map(koishi_utils_1.paramCase).join(', '); | ||
if (!name) | ||
return session.$send(`当前的可用指令有:${actionList}。`); | ||
return `当前的可用指令有:${actionList}。`; | ||
const action = actionMap[koishi_utils_1.paramCase(name)]; | ||
if (!action) | ||
return session.$send(`指令未找到。当前的可用指令有:${actionList}。`); | ||
return `指令未找到。当前的可用指令有:${actionList}。`; | ||
if (isGroup) { | ||
@@ -174,3 +174,3 @@ const fields = action.fields ? action.fields.slice() : koishi_core_1.Group.fields; | ||
if (options.thisGroup) { | ||
group = await session.observeGroup(fields); | ||
group = await session.$observeGroup(fields); | ||
} | ||
@@ -180,3 +180,3 @@ else if (koishi_utils_1.isInteger(options.group) && options.group > 0) { | ||
if (!data) | ||
return session.$send('未找到指定的群。'); | ||
return '未找到指定的群。'; | ||
group = koishi_utils_1.observe(data, diff => ctx.database.setGroup(options.group, diff), `group ${options.group}`); | ||
@@ -194,11 +194,11 @@ } | ||
if (!qq) | ||
return session.$send('未指定目标。'); | ||
return '未指定目标。'; | ||
const data = await ctx.database.getUser(qq, -1, fields); | ||
if (!data) | ||
return session.$send('未找到指定的用户。'); | ||
return '未找到指定的用户。'; | ||
if (qq === session.userId) { | ||
user = await session.observeUser(fields); | ||
user = await session.$observeUser(fields); | ||
} | ||
else if (session.$user.authority <= data.authority) { | ||
return session.$send('权限不足。'); | ||
return '权限不足。'; | ||
} | ||
@@ -210,3 +210,3 @@ else { | ||
else { | ||
user = await session.observeUser(fields); | ||
user = await session.$observeUser(fields); | ||
} | ||
@@ -213,0 +213,0 @@ return action.callback.call(ctx, session, user, ...args); |
@@ -56,3 +56,3 @@ "use strict"; | ||
if (!message) | ||
return session.$send('请输入要发送的文本。'); | ||
return '请输入要发送的文本。'; | ||
if (options.only) { | ||
@@ -59,0 +59,0 @@ let groups = await ctx.database.getAllGroups(['id', 'flag'], [session.selfId]); |
@@ -22,6 +22,6 @@ "use strict"; | ||
if (!message) | ||
return session.$send('请输入要触发的指令。'); | ||
return '请输入要触发的指令。'; | ||
if (options.member) { | ||
if (session.messageType === 'private') { | ||
return session.$send('无法在私聊上下文使用 --member 选项。'); | ||
return '无法在私聊上下文使用 --member 选项。'; | ||
} | ||
@@ -32,27 +32,23 @@ options[session.messageType] = session.$ctxId; | ||
if (!options.user && !options.group && !options.discuss) { | ||
return session.$send('请提供新的上下文。'); | ||
return '请提供新的上下文。'; | ||
} | ||
const newMeta = new koishi_core_1.Session(session); | ||
newMeta.$send = session.$send.bind(session); | ||
newMeta.$sendQueued = session.$sendQueued.bind(session); | ||
delete newMeta.groupId; | ||
delete newMeta.discussId; | ||
let ctxType, ctxId; | ||
const newSession = new koishi_core_1.Session(session); | ||
newSession.$send = session.$send.bind(session); | ||
newSession.$sendQueued = session.$sendQueued.bind(session); | ||
delete newSession.groupId; | ||
delete newSession.discussId; | ||
if (options.discuss) { | ||
newMeta.discussId = ctxId = +options.discuss; | ||
newMeta.messageType = 'discuss'; | ||
newMeta.messageType = ctxType = 'discuss'; | ||
newSession.discussId = +options.discuss; | ||
newSession.messageType = 'discuss'; | ||
} | ||
else if (options.group) { | ||
newMeta.groupId = ctxId = +options.group; | ||
newMeta.messageType = ctxType = 'group'; | ||
newMeta.subType = options.type || 'normal'; | ||
delete newMeta.$group; | ||
await newMeta.observeGroup(koishi_core_1.Group.fields); | ||
newSession.groupId = +options.group; | ||
newSession.messageType = 'group'; | ||
newSession.subType = options.type || 'normal'; | ||
delete newSession.$group; | ||
await newSession.$observeGroup(koishi_core_1.Group.fields); | ||
} | ||
else { | ||
ctxId = newMeta.userId; | ||
ctxType = 'user'; | ||
newMeta.messageType = 'private'; | ||
newMeta.subType = options.type || 'other'; | ||
newSession.messageType = 'private'; | ||
newSession.subType = options.type || 'other'; | ||
} | ||
@@ -62,22 +58,20 @@ if (options.user) { | ||
if (!id) | ||
return session.$send('未指定目标。'); | ||
newMeta.userId = id; | ||
newMeta.sender.userId = id; | ||
delete newMeta.$user; | ||
const user = await newMeta.observeUser(koishi_core_1.User.fields); | ||
return '未指定目标。'; | ||
newSession.userId = id; | ||
newSession.sender.userId = id; | ||
delete newSession.$user; | ||
const user = await newSession.$observeUser(koishi_core_1.User.fields); | ||
if (session.$user.authority <= user.authority) { | ||
return session.$send('权限不足。'); | ||
return '权限不足。'; | ||
} | ||
} | ||
if (options.group) { | ||
const info = await session.$bot.getGroupMemberInfo(ctxId, newMeta.userId).catch(() => ({})); | ||
Object.assign(newMeta.sender, info); | ||
const info = await session.$bot.getGroupMemberInfo(newSession.groupId, newSession.userId).catch(() => ({})); | ||
Object.assign(newSession.sender, info); | ||
} | ||
else if (options.user) { | ||
const info = await session.$bot.getStrangerInfo(newMeta.userId).catch(() => ({})); | ||
Object.assign(newMeta.sender, info); | ||
const info = await session.$bot.getStrangerInfo(newSession.userId).catch(() => ({})); | ||
Object.assign(newSession.sender, info); | ||
} | ||
newMeta.$ctxId = ctxId; | ||
newMeta.$ctxType = ctxType; | ||
return ctx.execute(message, newMeta); | ||
return newSession.$execute(message); | ||
}); | ||
@@ -84,0 +78,0 @@ } |
@@ -14,3 +14,3 @@ "use strict"; | ||
.usage('各 id 之间请使用逗号分隔。') | ||
.action(({ options, session }, message) => { | ||
.action(async ({ options, session }, message) => { | ||
// parse channels | ||
@@ -36,3 +36,3 @@ const channels = { | ||
// send messages | ||
return Promise.all([ | ||
await Promise.all([ | ||
...channels.private.map(id => session.$bot.sendPrivateMsgAsync(+id, message)), | ||
@@ -39,0 +39,0 @@ ...channels.group.map(id => session.$bot.sendGroupMsgAsync(+id, message)), |
import { Context } from 'koishi-core'; | ||
import { DebugOptions } from './debug'; | ||
import { RepeaterOptions } from './repeater'; | ||
@@ -16,4 +17,5 @@ import { HandlerOptions } from './handler'; | ||
usage?: false; | ||
debug?: DebugOptions; | ||
} | ||
export declare const name = "common"; | ||
export declare function apply(ctx: Context, options?: Options): void; |
@@ -35,2 +35,4 @@ "use strict"; | ||
ctx.plugin(require('./broadcast')); | ||
if (options.debug) | ||
ctx.plugin(require('./debug'), options.debug); | ||
if (options.exit !== false) | ||
@@ -37,0 +39,0 @@ ctx.plugin(require('./exit')); |
@@ -33,6 +33,6 @@ "use strict"; | ||
if (!id) | ||
return session.$send('未找到用户。'); | ||
return '未找到用户。'; | ||
user = await ctx.database.getUser(id, -1, Array.from(infoFields)); | ||
if (!user) | ||
return session.$send('未找到用户。'); | ||
return '未找到用户。'; | ||
if (+user.name === id) { | ||
@@ -54,3 +54,3 @@ output.push(`${id} 的权限为 ${user.authority} 级。`); | ||
} | ||
return session.$send(output.join('\n')); | ||
return output.join('\n'); | ||
}); | ||
@@ -57,0 +57,0 @@ } |
@@ -20,5 +20,5 @@ "use strict"; | ||
if (!output.length) | ||
return session.$send('你今日没有调用过消耗次数的指令。'); | ||
return '你今日没有调用过消耗次数的指令。'; | ||
output.unshift('你今日各指令的调用次数为:'); | ||
return session.$send(output.join('\n')); | ||
return output.join('\n'); | ||
}); | ||
@@ -25,0 +25,0 @@ } |
{ | ||
"name": "koishi-plugin-common", | ||
"description": "Common plugins for Koishi", | ||
"version": "3.0.0-beta.2", | ||
"version": "3.0.0-beta.3", | ||
"main": "dist/index.js", | ||
@@ -38,5 +38,5 @@ "typings": "dist/index.d.ts", | ||
"axios": "^0.19.2", | ||
"koishi-core": "^2.0.0-beta.2", | ||
"koishi-core": "^2.0.0-beta.3", | ||
"koishi-utils": "^2.1.2" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
66233
35
848
Updatedkoishi-core@^2.0.0-beta.3