koishi-plugin-common
Advanced tools
Comparing version 4.0.0-beta.8 to 4.0.0-beta.9
@@ -104,6 +104,6 @@ var __create = Object.create; | ||
if (options.target) { | ||
const id = session.$bot.parseUser(options.target); | ||
const id = session.bot.parseUser(options.target); | ||
if (!id) | ||
return "请指定正确的目标。"; | ||
const {database} = session.$app; | ||
const {database} = session.app; | ||
const data = await database.getUser(session.platform, id, [...fields]); | ||
@@ -114,3 +114,3 @@ if (!data) | ||
target = await session.observeUser(fields); | ||
} else if (session.$user.authority <= data.authority) { | ||
} else if (session.user.authority <= data.authority) { | ||
return import_koishi_utils3.template("internal.low-authority"); | ||
@@ -141,6 +141,6 @@ } else { | ||
if (options.target) { | ||
const id = session.$bot.parseChannel(options.target); | ||
const id = session.bot.parseChannel(options.target); | ||
if (!id) | ||
return "请指定正确的目标。"; | ||
const {database} = session.$app; | ||
const {database} = session.app; | ||
const data = await session.getChannel(id, "", [...fields]); | ||
@@ -169,5 +169,5 @@ if (!data) | ||
ctx.command("common/callme [name:text]", "修改自己的称呼").userFields(["id", "name"]).shortcut("叫我", {prefix: true, fuzzy: true, greedy: true}).action(async ({session}, name2) => { | ||
const {$user} = session; | ||
const {user} = session; | ||
if (!name2) { | ||
if ($user.name) { | ||
if (user.name) { | ||
return import_koishi_utils3.template("callme.current", session.$username); | ||
@@ -177,3 +177,3 @@ } else { | ||
} | ||
} else if (name2 === $user.name) { | ||
} else if (name2 === user.name) { | ||
return import_koishi_utils3.template("callme.unchanged"); | ||
@@ -189,4 +189,4 @@ } else if (!(name2 = name2.trim())) { | ||
try { | ||
$user.name = name2; | ||
await $user._update(); | ||
user.name = name2; | ||
await user._update(); | ||
return import_koishi_utils3.template("callme.updated", session.$username); | ||
@@ -230,3 +230,3 @@ } catch (error) { | ||
return "参数错误。"; | ||
if (authority >= session.$user.authority) | ||
if (authority >= session.user.authority) | ||
return import_koishi_utils3.template("internal.low-authority"); | ||
@@ -298,3 +298,3 @@ await ctx.database.createUser(session.platform, target[session.platform], {authority}); | ||
ctx.command("channel/assign [bot]", "受理者账号", {authority: 4}).channelFields(["assignee"]).adminChannel(async ({session, target}, value) => { | ||
const assignee = value ? session.$bot.parseUser(value) : session.selfId; | ||
const assignee = value ? session.bot.parseUser(value) : session.selfId; | ||
if (!assignee) | ||
@@ -317,2 +317,3 @@ return "参数错误。"; | ||
var import_koishi_utils3 = __toModule(require("koishi-utils")); | ||
var textSegmentTypes = ["text", "header", "section"]; | ||
var cqTypes = { | ||
@@ -330,3 +331,4 @@ face: "表情", | ||
json: "JSON", | ||
xml: "XML" | ||
xml: "XML", | ||
card: "卡片消息" | ||
}; | ||
@@ -377,9 +379,7 @@ function getDeps(template) { | ||
on("content", async (session) => { | ||
if (!session.content) | ||
console.log(JSON.stringify(session)); | ||
const codes = import_koishi_utils3.CQCode.build(session.content.split("\n", 1)[0]); | ||
const codes = import_koishi_utils3.segment.parse(session.content.split("\n", 1)[0]); | ||
let output = ""; | ||
for (const code of codes) { | ||
if (typeof code === "string") { | ||
output += import_koishi_utils3.CQCode.unescape(code); | ||
if (textSegmentTypes.includes(code.type)) { | ||
output += import_koishi_utils3.segment.unescape(code.data.content); | ||
} else if (code.type === "at") { | ||
@@ -392,7 +392,7 @@ if (code.data.type === "all") { | ||
if (!userMap[id] || timestamp - userMap[id][1] >= refreshUserName) { | ||
userMap[id] = [getUserName(session.$bot, session.groupId, code.data.qq), timestamp]; | ||
userMap[id] = [getUserName(session.bot, session.groupId, code.data.qq), timestamp]; | ||
} | ||
output += "@" + await userMap[id][0]; | ||
} else { | ||
output += "@" + session.$bot.username; | ||
output += "@" + session.bot.username; | ||
} | ||
@@ -404,3 +404,3 @@ } else if (code.type === "share" || code.type === "location") { | ||
} else { | ||
output += `[${cqTypes[code.type]}]`; | ||
output += `[${cqTypes[code.type] || "未知"}]`; | ||
} | ||
@@ -418,15 +418,14 @@ } | ||
if (!channelMap[cid] || timestamp - channelMap[cid][1] >= refreshChannelName) { | ||
channelMap[cid] = [getChannelName(session.$bot, session.channelId), timestamp]; | ||
channelMap[cid] = [getChannelName(session.bot, session.channelId), timestamp]; | ||
} | ||
return await channelMap[cid][0]; | ||
}); | ||
function handleMessage(deps, template, session) { | ||
async function handleMessage(deps, template, session) { | ||
const params = import_koishi_utils3.pick(session, ["platform", "channelId", "groupId", "userId", "selfId"]); | ||
if (session.type === "message") | ||
userMap[session.uid] = [session.author.username, Date.now()]; | ||
Object.assign(params, import_koishi_utils3.pick(session.author, ["username", "nickname"])); | ||
Promise.all(deps.map(async (key) => { | ||
await Promise.all(deps.map(async (key) => { | ||
const callback = tasks[key]; | ||
params[key] || (params[key] = await callback(session)); | ||
})).then(() => logger.debug(import_koishi_utils3.interpolate(template, params))); | ||
})); | ||
logger.debug(import_koishi_utils3.interpolate(template, params)); | ||
} | ||
@@ -440,4 +439,14 @@ ctx.intersect((session) => { | ||
}).plugin((ctx2) => { | ||
ctx2.on("message", handleMessage.bind(null, receiveDeps, formatReceive)); | ||
ctx2.before("send", handleMessage.bind(null, sendDeps, formatSend)); | ||
ctx2.on("message", async (session) => { | ||
if (session.subtype === "group") { | ||
const {assignee} = await session.observeChannel(["assignee"]); | ||
if (assignee !== session.selfId) | ||
return; | ||
} | ||
userMap[session.uid] = [session.author.username, Date.now()]; | ||
handleMessage(receiveDeps, formatReceive, session); | ||
}); | ||
ctx2.before("send", (session) => { | ||
handleMessage(sendDeps, formatSend, session); | ||
}); | ||
}); | ||
@@ -518,11 +527,11 @@ } | ||
const result = await getHandleResult(options.onFriendRequest, session); | ||
return session.$bot.handleFriendRequest(session.messageId, ...result); | ||
return session.bot.handleFriendRequest(session.messageId, ...result); | ||
}); | ||
ctx.on("group-member-request", async (session) => { | ||
const result = await getHandleResult(options.onGroupMemberRequest, session); | ||
return session.$bot.handleGroupRequest(session.messageId, ...result); | ||
return session.bot.handleGroupRequest(session.messageId, ...result); | ||
}); | ||
ctx.on("group-request", async (session) => { | ||
const result = await getHandleResult(options.onGroupRequest, session); | ||
return session.$bot.handleGroupRequest(session.messageId, ...result); | ||
return session.bot.handleGroupRequest(session.messageId, ...result); | ||
}); | ||
@@ -570,8 +579,8 @@ const {respondents = []} = options; | ||
if (options.unescape) { | ||
message = import_koishi_utils2.CQCode.unescape(message); | ||
message = import_koishi_utils2.segment.unescape(message); | ||
} | ||
if (options.forceAnonymous) { | ||
message = import_koishi_utils2.CQCode("anonymous") + message; | ||
message = import_koishi_utils2.segment("anonymous") + message; | ||
} else if (options.anonymous) { | ||
message = import_koishi_utils2.CQCode("anonymous", {ignore: true}) + message; | ||
message = import_koishi_utils2.segment("anonymous", {ignore: true}) + message; | ||
} | ||
@@ -588,3 +597,3 @@ return message; | ||
${text}`; | ||
const id = await session.$bot.sendPrivateMessage(config.operator, message); | ||
const id = await session.bot.sendPrivateMessage(config.operator, message); | ||
interactions[id] = userId; | ||
@@ -594,9 +603,9 @@ return "反馈信息发送成功!"; | ||
ctx.middleware((session, next) => { | ||
const {$reply, $parsed} = session; | ||
if (!$parsed || !$reply) | ||
const {reply, parsed} = session; | ||
if (!parsed.content || !reply) | ||
return next(); | ||
const userId = interactions[$reply.messageId]; | ||
const userId = interactions[reply.messageId]; | ||
if (!userId) | ||
return next(); | ||
return session.$bot.sendPrivateMessage(userId, $parsed); | ||
return session.bot.sendPrivateMessage(userId, parsed.content); | ||
}); | ||
@@ -614,3 +623,3 @@ dbctx.command("common/broadcast <message:text>", "全服广播", {authority: 4}).option("forced", "-f 无视 silent 标签进行广播").option("only", "-o 仅向当前 Bot 负责的群进行广播").action(async ({options, session}, message) => { | ||
} | ||
await session.$bot.broadcast(groups.map((g) => g.id.slice(session.platform["length"] + 1)), message); | ||
await session.bot.broadcast(groups.map((g) => g.id.slice(session.platform["length"] + 1)), message); | ||
}); | ||
@@ -640,11 +649,11 @@ dbctx.command("common/contextify <message:text>", "在特定上下文中触发指令", {authority: 3}).alias("ctxf").userFields(["authority"]).option("user", "-u [id] 使用私聊上下文").option("group", "-g [id] 使用群聊上下文").option("member", "-m [id] 使用当前群/讨论组成员上下文").option("type", "-t [type] 确定发送信息的子类型").usage([ | ||
newSession.subtype = "private"; | ||
delete newSession.$channel; | ||
delete newSession.channel; | ||
} else if (options.group !== session.groupId) { | ||
newSession.groupId = options.group; | ||
newSession.subtype = "group"; | ||
delete newSession.$channel; | ||
delete newSession.channel; | ||
await newSession.observeChannel(import_koishi_core.Channel.fields); | ||
} | ||
if (options.user) { | ||
const id = session.$bot.parseUser(options.user); | ||
const id = session.bot.parseUser(options.user); | ||
if (!id) | ||
@@ -654,5 +663,5 @@ return "未指定目标。"; | ||
newSession.author.userId = id; | ||
delete newSession.$user; | ||
delete newSession.user; | ||
const user = await newSession.observeUser(import_koishi_core.User.fields); | ||
if (session.$user.authority <= user.authority) { | ||
if (session.user.authority <= user.authority) { | ||
return "权限不足。"; | ||
@@ -662,6 +671,6 @@ } | ||
if (options.group) { | ||
const info = await session.$bot.getGroupMember(newSession.groupId, newSession.userId).catch(() => ({})); | ||
const info = await session.bot.getGroupMember(newSession.groupId, newSession.userId).catch(() => ({})); | ||
Object.assign(newSession.author, info); | ||
} else if (options.user) { | ||
const info = await session.$bot.getUser(newSession.userId).catch(() => ({})); | ||
const info = await session.bot.getUser(newSession.userId).catch(() => ({})); | ||
Object.assign(newSession.author, info); | ||
@@ -668,0 +677,0 @@ } |
{ | ||
"name": "koishi-plugin-common", | ||
"description": "Common plugins for Koishi", | ||
"version": "4.0.0-beta.8", | ||
"version": "4.0.0-beta.9", | ||
"main": "dist/index.js", | ||
@@ -33,8 +33,8 @@ "typings": "dist/index.d.ts", | ||
"peerDependencies": { | ||
"koishi-core": "^3.0.0-beta.9", | ||
"koishi-utils": "^3.2.5" | ||
"koishi-core": "^3.0.0-beta.10", | ||
"koishi-utils": "^3.2.6" | ||
}, | ||
"devDependencies": { | ||
"koishi-test-utils": "^6.0.0-beta.2" | ||
"koishi-test-utils": "^6.0.0-beta.3" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
74711
752