koishi-plugin-common
Advanced tools
Comparing version 2.1.3 to 2.1.4
@@ -5,2 +5,2 @@ import { Context } from 'koishi-core'; | ||
} | ||
export default function apply(ctx: Context, options?: BroadcastOptions): void; | ||
export default function apply(ctx: Context, config?: BroadcastOptions): void; |
@@ -8,6 +8,21 @@ "use strict"; | ||
}; | ||
function apply(ctx, options = {}) { | ||
options = { ...defaultOptions, ...options }; | ||
function apply(ctx, config = {}) { | ||
config = { ...defaultOptions, ...config }; | ||
async function broadcast(selfId, groups, message) { | ||
const { sender } = koishi_core_1.appMap[selfId]; | ||
for (let index = 0; index < groups.length; index++) { | ||
if (index) | ||
await koishi_utils_1.sleep(config.broadcastInterval); | ||
sender.sendGroupMsgAsync(groups[index], message); | ||
} | ||
} | ||
ctx.command('broadcast <message...>', '全服广播', { authority: 4 }) | ||
.action(async (_, message) => { | ||
.option('-o, --only', '仅向当前 Bot 负责的群进行广播') | ||
.action(async ({ options, meta }, message) => { | ||
if (!message) | ||
return meta.$send('请输入要发送的文本。'); | ||
if (options.only) { | ||
const groups = await ctx.database.getAllGroups(['id'], [ctx.app.selfId]); | ||
return broadcast(ctx.app.selfId, groups.map(g => g.id), message); | ||
} | ||
const groups = await ctx.database.getAllGroups(['id', 'assignee']); | ||
@@ -23,13 +38,5 @@ const assignMap = {}; | ||
} | ||
Object.keys(assignMap).forEach(async (id) => { | ||
const groups = assignMap[id]; | ||
const { sender } = koishi_core_1.appMap[id]; | ||
for (let index = 0; index < groups.length; index++) { | ||
if (index) | ||
await koishi_utils_1.sleep(options.broadcastInterval); | ||
sender.sendGroupMsgAsync(groups[index], message); | ||
} | ||
}); | ||
return Promise.all(Object.keys(assignMap).map(id => broadcast(id, assignMap[id], message))); | ||
}); | ||
} | ||
exports.default = apply; |
import { Context } from 'koishi-core'; | ||
export default function apply(ctx: Context): void; | ||
export declare const GLOBAL_HELP_PROLOGUE = "\u5F53\u524D\u53EF\u7528\u7684\u6307\u4EE4\u6709\uFF08\u62EC\u53F7\u5185\u4E3A\u5BF9\u5E94\u7684\u6700\u4F4E\u6743\u9650\u7B49\u7EA7\uFF0C\u6807\u6709\u661F\u53F7\u7684\u8868\u793A\u542B\u6709\u5B50\u6307\u4EE4\uFF09\uFF1A"; | ||
export declare const GLOBAL_HELP_EPILOGUE: string; |
@@ -46,3 +46,3 @@ "use strict"; | ||
} | ||
function getCommandList(context, meta, parent, expand) { | ||
function getCommandList(prefix, context, meta, parent, expand) { | ||
let commands = getCommands(context, meta, parent); | ||
@@ -60,5 +60,9 @@ if (!expand) { | ||
} | ||
let hasSubcommand = false; | ||
const output = commands.map(({ name, config, children }) => { | ||
if (children.length) | ||
hasSubcommand = true; | ||
return ` ${name} (${config.authority}${children.length ? '*' : ''}) ${config.description}`; | ||
}); | ||
output.unshift(`${prefix}(括号内为对应的最低权限等级${hasSubcommand ? ',标有星号的表示含有子指令' : ''}):`); | ||
if (expand) | ||
@@ -68,14 +72,9 @@ output.push('注:部分指令组已展开,故不再显示。'); | ||
} | ||
exports.GLOBAL_HELP_PROLOGUE = '当前可用的指令有(括号内为对应的最低权限等级,标有星号的表示含有子指令):'; | ||
exports.GLOBAL_HELP_EPILOGUE = [ | ||
'群聊普通指令可以通过“@我+指令名”的方式进行触发。', | ||
'私聊或全局指令则不需要添加上述前缀,直接输入指令名即可触发。', | ||
'输入“全局指令”查看全部可用的全局指令。', | ||
'输入“帮助+指令名”查看特定指令的语法和使用示例。', | ||
].join('\n'); | ||
function showGlobalHelp(context, meta, config) { | ||
return meta.$send([ | ||
exports.GLOBAL_HELP_PROLOGUE, | ||
...getCommandList(context, meta, null, config.expand), | ||
exports.GLOBAL_HELP_EPILOGUE, | ||
...getCommandList('当前可用的指令有', context, meta, null, config.expand), | ||
'群聊普通指令可以通过“@我+指令名”的方式进行触发。', | ||
'私聊或全局指令则不需要添加上述前缀,直接输入指令名即可触发。', | ||
'输入“全局指令”查看全部可用的全局指令。', | ||
'输入“帮助+指令名”查看特定指令的语法和使用示例。', | ||
].join('\n')); | ||
@@ -135,5 +134,5 @@ } | ||
if (command.children.length) { | ||
output.push('可用的子指令有(括号内为对应的最低权限等级,标有星号的表示含有子指令):', ...getCommandList(command.context, meta, command, config.expand)); | ||
output.push(...getCommandList('可用的子指令有', command.context, meta, command, config.expand)); | ||
} | ||
return meta.$send(output.join('\n')); | ||
} |
{ | ||
"name": "koishi-plugin-common", | ||
"description": "Common plugins for Koishi", | ||
"version": "2.1.3", | ||
"version": "2.1.4", | ||
"main": "dist/index.js", | ||
@@ -31,9 +31,9 @@ "typings": "dist/index.d.ts", | ||
"devDependencies": { | ||
"koishi-database-memory": "^1.1.2", | ||
"koishi-test-utils": "^3.1.1" | ||
"koishi-database-memory": "^1.1.3", | ||
"koishi-test-utils": "^3.1.2" | ||
}, | ||
"dependencies": { | ||
"koishi-core": "^1.9.0", | ||
"koishi-utils": "^1.0.3" | ||
"koishi-core": "^1.10.0", | ||
"koishi-utils": "^1.0.4" | ||
} | ||
} |
@@ -12,7 +12,23 @@ import { Context, appMap } from 'koishi-core' | ||
export default function apply (ctx: Context, options: BroadcastOptions = {}) { | ||
options = { ...defaultOptions, ...options } | ||
export default function apply (ctx: Context, config: BroadcastOptions = {}) { | ||
config = { ...defaultOptions, ...config } | ||
async function broadcast (selfId: string | number, groups: number[], message: string) { | ||
const { sender } = appMap[selfId] | ||
for (let index = 0; index < groups.length; index++) { | ||
if (index) await sleep(config.broadcastInterval) | ||
sender.sendGroupMsgAsync(groups[index], message) | ||
} | ||
} | ||
ctx.command('broadcast <message...>', '全服广播', { authority: 4 }) | ||
.action(async (_, message: string) => { | ||
.option('-o, --only', '仅向当前 Bot 负责的群进行广播') | ||
.action(async ({ options, meta }, message) => { | ||
if (!message) return meta.$send('请输入要发送的文本。') | ||
if (options.only) { | ||
const groups = await ctx.database.getAllGroups(['id'], [ctx.app.selfId]) | ||
return broadcast(ctx.app.selfId, groups.map(g => g.id), message) | ||
} | ||
const groups = await ctx.database.getAllGroups(['id', 'assignee']) | ||
@@ -27,11 +43,4 @@ const assignMap: Record<number, number[]> = {} | ||
} | ||
Object.keys(assignMap).forEach(async (id: any) => { | ||
const groups = assignMap[id] | ||
const { sender } = appMap[id] | ||
for (let index = 0; index < groups.length; index++) { | ||
if (index) await sleep(options.broadcastInterval) | ||
sender.sendGroupMsgAsync(groups[index], message) | ||
} | ||
}) | ||
return Promise.all(Object.keys(assignMap).map(id => broadcast(id, assignMap[id], message))) | ||
}) | ||
} |
@@ -45,3 +45,3 @@ import { Context, Command, UserData, Meta, getUsage } from 'koishi-core' | ||
function getCommandList (context: Context, meta: Meta<'message'>, parent: Command, expand: boolean) { | ||
function getCommandList (prefix: string, context: Context, meta: Meta<'message'>, parent: Command, expand: boolean) { | ||
let commands = getCommands(context, meta, parent) | ||
@@ -58,5 +58,8 @@ if (!expand) { | ||
} | ||
let hasSubcommand = false | ||
const output = commands.map(({ name, config, children }) => { | ||
if (children.length) hasSubcommand = true | ||
return ` ${name} (${config.authority}${children.length ? '*' : ''}) ${config.description}` | ||
}) | ||
output.unshift(`${prefix}(括号内为对应的最低权限等级${hasSubcommand ? ',标有星号的表示含有子指令' : ''}):`) | ||
if (expand) output.push('注:部分指令组已展开,故不再显示。') | ||
@@ -66,15 +69,9 @@ return output | ||
export const GLOBAL_HELP_PROLOGUE = '当前可用的指令有(括号内为对应的最低权限等级,标有星号的表示含有子指令):' | ||
export const GLOBAL_HELP_EPILOGUE = [ | ||
'群聊普通指令可以通过“@我+指令名”的方式进行触发。', | ||
'私聊或全局指令则不需要添加上述前缀,直接输入指令名即可触发。', | ||
'输入“全局指令”查看全部可用的全局指令。', | ||
'输入“帮助+指令名”查看特定指令的语法和使用示例。', | ||
].join('\n') | ||
function showGlobalHelp (context: Context, meta: Meta<'message'>, config: any) { | ||
return meta.$send([ | ||
GLOBAL_HELP_PROLOGUE, | ||
...getCommandList(context, meta, null, config.expand), | ||
GLOBAL_HELP_EPILOGUE, | ||
...getCommandList('当前可用的指令有', context, meta, null, config.expand), | ||
'群聊普通指令可以通过“@我+指令名”的方式进行触发。', | ||
'私聊或全局指令则不需要添加上述前缀,直接输入指令名即可触发。', | ||
'输入“全局指令”查看全部可用的全局指令。', | ||
'输入“帮助+指令名”查看特定指令的语法和使用示例。', | ||
].join('\n')) | ||
@@ -143,6 +140,3 @@ } | ||
if (command.children.length) { | ||
output.push( | ||
'可用的子指令有(括号内为对应的最低权限等级,标有星号的表示含有子指令):', | ||
...getCommandList(command.context, meta, command, config.expand), | ||
) | ||
output.push(...getCommandList('可用的子指令有', command.context, meta, command, config.expand)) | ||
} | ||
@@ -149,0 +143,0 @@ |
import { App } from 'koishi-test-utils' | ||
import { sleep } from 'koishi-utils' | ||
import { createUser } from 'koishi-core' | ||
import { authorize, AuthorizeOptions } from '../src' | ||
import { authorize } from '../src' | ||
import 'koishi-database-memory' | ||
@@ -18,3 +18,3 @@ | ||
app.plugin(authorize) | ||
app.plugin<AuthorizeOptions>(authorize, { | ||
app.plugin(authorize, { | ||
authorizeUser: { | ||
@@ -43,3 +43,3 @@ 123: 2, | ||
app.plugin<AuthorizeOptions>(authorize, { | ||
app.plugin(authorize, { | ||
authorizeGroup: { | ||
@@ -73,3 +73,3 @@ 456: 2, | ||
app.plugin<AuthorizeOptions>(authorize, { | ||
app.plugin(authorize, { | ||
authorizeGroup: { | ||
@@ -98,3 +98,3 @@ 456: { admin: 2, owner: 3 }, | ||
app.plugin<AuthorizeOptions>(authorize, { | ||
app.plugin(authorize, { | ||
authorizeGroup: { | ||
@@ -145,3 +145,3 @@ 456: 2, | ||
app.plugin<AuthorizeOptions>(authorize, { | ||
app.plugin(authorize, { | ||
authorizeGroup: { | ||
@@ -189,3 +189,3 @@ 456: { admin: 2 }, | ||
app.plugin<AuthorizeOptions>(authorize, { | ||
app.plugin(authorize, { | ||
authorizeUser: { | ||
@@ -192,0 +192,0 @@ 123: 2, |
@@ -1,33 +0,39 @@ | ||
import { MockedApp } from 'koishi-test-utils' | ||
import { sleep } from 'koishi-utils' | ||
import { MockedApp, BASE_SELF_ID } from 'koishi-test-utils' | ||
import { startAll, stopAll } from 'koishi-core' | ||
import { broadcast } from '../src' | ||
import 'koishi-database-memory' | ||
const app = new MockedApp({ database: { memory: {} } }) | ||
const app1 = new MockedApp({ database: { memory: {} } }) | ||
const app2 = new MockedApp({ database: { memory: {} }, selfId: BASE_SELF_ID + 1 }) | ||
app.plugin(broadcast) | ||
app1.plugin(broadcast) | ||
jest.useFakeTimers() | ||
beforeAll(async () => { | ||
await app.start() | ||
await app.database.getUser(123, 4) | ||
await app.database.getGroup(321, app.selfId) | ||
await app.database.getGroup(654, app.selfId) | ||
await startAll() | ||
await app1.database.getUser(123, 4) | ||
await app1.database.getGroup(321, app1.selfId) | ||
await app1.database.getGroup(654, app1.selfId) | ||
await app2.database.getGroup(987, app2.selfId) | ||
}) | ||
afterAll(() => app.stop()) | ||
afterAll(() => stopAll()) | ||
async function nextTick () { | ||
jest.advanceTimersToNextTimer() | ||
jest.useRealTimers() | ||
await sleep(0) | ||
jest.useFakeTimers() | ||
} | ||
test('basic support', async () => { | ||
await app.receiveMessage('user', 'broadcast foo bar', 123) | ||
app.shouldHaveLastRequest('send_group_msg', { message: 'foo bar', groupId: 321 }) | ||
await nextTick() | ||
app.shouldHaveLastRequest('send_group_msg', { message: 'foo bar', groupId: 654 }) | ||
await app1.receiveMessage('user', 'broadcast foo bar', 123) | ||
app1.shouldHaveLastRequests([ | ||
['send_group_msg', { message: 'foo bar', groupId: 321 }], | ||
['send_group_msg', { message: 'foo bar', groupId: 654 }], | ||
]) | ||
app2.shouldHaveLastRequests([ | ||
['send_group_msg', { message: 'foo bar', groupId: 987 }], | ||
]) | ||
}) | ||
test('self only', async () => { | ||
await app1.receiveMessage('user', 'broadcast -o foo bar', 123) | ||
app1.shouldHaveLastRequests([ | ||
['send_group_msg', { message: 'foo bar', groupId: 321 }], | ||
['send_group_msg', { message: 'foo bar', groupId: 654 }], | ||
]) | ||
app2.shouldHaveNoRequests() | ||
}) |
import { MockedApp } from 'koishi-test-utils' | ||
import { sleep } from 'koishi-utils' | ||
import { requestHandler, HandlerOptions } from '../src' | ||
import { requestHandler } from '../src' | ||
import 'koishi-database-memory' | ||
@@ -11,3 +11,3 @@ | ||
app = new MockedApp() | ||
app.plugin<HandlerOptions>(requestHandler) | ||
app.plugin(requestHandler) | ||
await app.start() | ||
@@ -38,3 +38,3 @@ }) | ||
app = new MockedApp() | ||
app.plugin<HandlerOptions>(requestHandler, { | ||
app.plugin(requestHandler, { | ||
handleFriend: 'foo', | ||
@@ -69,3 +69,3 @@ handleGroupAdd: 'bar', | ||
app = new MockedApp() | ||
app.plugin<HandlerOptions>(requestHandler, { | ||
app.plugin(requestHandler, { | ||
handleFriend: false, | ||
@@ -100,3 +100,3 @@ handleGroupAdd: false, | ||
app = new MockedApp() | ||
app.plugin<HandlerOptions>(requestHandler, { | ||
app.plugin(requestHandler, { | ||
handleFriend: () => true, | ||
@@ -103,0 +103,0 @@ handleGroupAdd: () => true, |
@@ -20,2 +20,3 @@ import { Session, MockedApp, utils } from 'koishi-test-utils' | ||
app.command('bar.baz', 'command with alias and shortcut') | ||
.usage(() => 'usage text') | ||
.alias('baz-alias') | ||
@@ -22,0 +23,0 @@ .shortcut('baz-shortcut') |
@@ -7,3 +7,3 @@ import { MockedApp } from 'koishi-test-utils' | ||
const app = new MockedApp() | ||
app.plugin<common.Config>(common) | ||
app.plugin(common) | ||
@@ -22,3 +22,3 @@ expect(app._commandMap.admin).toBeFalsy() | ||
const app = new MockedApp({ database: { memory: {} } }) | ||
app.plugin<common.Config>(common) | ||
app.plugin(common) | ||
@@ -38,3 +38,3 @@ expect(app._commandMap.admin).toBeTruthy() | ||
app.plugin<common.Config>(common, { | ||
app.plugin(common, { | ||
admin: false, | ||
@@ -41,0 +41,0 @@ broadcast: false, |
import { MockedApp, Session } from 'koishi-test-utils' | ||
import { info, registerUserInfo, InfoOptions } from '../src' | ||
import { info, registerUserInfo } from '../src' | ||
import 'koishi-database-memory' | ||
@@ -39,3 +39,3 @@ | ||
test('getUserName', async () => { | ||
app.plugin<InfoOptions>(info, { | ||
app.plugin(info, { | ||
getUserName (user, meta) { | ||
@@ -42,0 +42,0 @@ if (user.id !== meta.userId) return 'bar' |
import { MockedApp } from 'koishi-test-utils' | ||
import { repeater, RepeaterOptions } from '../src' | ||
import { repeater } from '../src' | ||
import 'koishi-database-memory' | ||
@@ -9,3 +9,3 @@ | ||
app.plugin<RepeaterOptions>(repeater, { | ||
app.plugin(repeater, { | ||
repeat: (repeated, times) => !repeated && times >= 2, | ||
@@ -27,3 +27,3 @@ interrupt: false, | ||
app.plugin<RepeaterOptions>(repeater, { | ||
app.plugin(repeater, { | ||
repeat: (_, times) => times >= 2, | ||
@@ -45,3 +45,3 @@ interrupt: (_, times) => times >= 4, | ||
app.plugin<RepeaterOptions>(repeater, { | ||
app.plugin(repeater, { | ||
repeat: (_, times) => times >= 2, | ||
@@ -63,3 +63,3 @@ interrupt: false, | ||
app.plugin<RepeaterOptions>(repeater, { | ||
app.plugin(repeater, { | ||
repeat: (_, times) => times >= 2, | ||
@@ -66,0 +66,0 @@ interrupt: false, |
import { MockedApp } from 'koishi-test-utils' | ||
import { respondent, Respondent } from '../src' | ||
import { respondent } from '../src' | ||
@@ -9,3 +9,3 @@ const app = new MockedApp() | ||
app.plugin(respondent) | ||
app.plugin<Respondent[]>(respondent, [{ | ||
app.plugin(respondent, [{ | ||
match: '挖坑一时爽', | ||
@@ -12,0 +12,0 @@ reply: '填坑火葬场', |
import { MockedApp } from 'koishi-test-utils' | ||
import { Meta } from 'koishi-core' | ||
import { sleep } from 'koishi-utils' | ||
import { welcome, WelcomeMessage } from '../src' | ||
import { welcome } from '../src' | ||
import 'koishi-database-memory' | ||
@@ -26,3 +26,3 @@ | ||
const app = new MockedApp({ database: { memory: {} } }) | ||
app.plugin<WelcomeMessage>(welcome, 'welcome') | ||
app.plugin(welcome, 'welcome') | ||
@@ -29,0 +29,0 @@ await app.start() |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2542
307445
Updatedkoishi-core@^1.10.0
Updatedkoishi-utils@^1.0.4