koishi-plugin-common
Advanced tools
Comparing version 2.1.5 to 2.1.6
@@ -10,11 +10,12 @@ "use strict"; | ||
config = { ...defaultOptions, ...config }; | ||
async function broadcast(selfId, groups, message) { | ||
async function broadcast(selfId, groupIds, message) { | ||
const { sender } = koishi_core_1.appMap[selfId]; | ||
for (let index = 0; index < groups.length; index++) { | ||
for (let index = 0; index < groupIds.length; index++) { | ||
if (index) | ||
await koishi_utils_1.sleep(config.broadcastInterval); | ||
sender.sendGroupMsgAsync(groups[index], message); | ||
sender.sendGroupMsgAsync(groupIds[index], message); | ||
} | ||
} | ||
ctx.command('broadcast <message...>', '全服广播', { authority: 4 }) | ||
.option('-f, --forced', '无视 noEmit 标签进行广播') | ||
.option('-o, --only', '仅向当前 Bot 负责的群进行广播') | ||
@@ -25,8 +26,13 @@ .action(async ({ options, meta }, message) => { | ||
if (options.only) { | ||
const groups = await ctx.database.getAllGroups(['id'], [ctx.app.selfId]); | ||
let groups = await ctx.database.getAllGroups(['id', 'flag'], [ctx.app.selfId]); | ||
if (!options.forced) { | ||
groups = groups.filter(g => !(g.flag & koishi_core_1.GroupFlag.noEmit)); | ||
} | ||
return broadcast(ctx.app.selfId, groups.map(g => g.id), message); | ||
} | ||
const groups = await ctx.database.getAllGroups(['id', 'assignee']); | ||
const groups = await ctx.database.getAllGroups(['id', 'assignee', 'flag']); | ||
const assignMap = {}; | ||
for (const { id, assignee } of groups) { | ||
for (const { id, assignee, flag } of groups) { | ||
if (!options.forced && (flag & koishi_core_1.GroupFlag.noEmit)) | ||
continue; | ||
if (!assignMap[assignee]) { | ||
@@ -33,0 +39,0 @@ assignMap[assignee] = [id]; |
@@ -6,2 +6,4 @@ "use strict"; | ||
ctx.receiver.on('group-increase', async (meta) => { | ||
if (meta.userId === ctx.app.selfId) | ||
return; | ||
if (ctx.database) { | ||
@@ -8,0 +10,0 @@ const group = await ctx.database.getGroup(meta.groupId, 0, ['assignee']); |
{ | ||
"name": "koishi-plugin-common", | ||
"description": "Common plugins for Koishi", | ||
"version": "2.1.5", | ||
"version": "2.1.6", | ||
"main": "dist/index.js", | ||
@@ -31,9 +31,9 @@ "typings": "dist/index.d.ts", | ||
"devDependencies": { | ||
"koishi-database-memory": "^1.1.4", | ||
"koishi-test-utils": "^3.1.3" | ||
"koishi-database-memory": "^1.1.5", | ||
"koishi-test-utils": "^3.1.4" | ||
}, | ||
"dependencies": { | ||
"koishi-core": "^1.10.1", | ||
"koishi-core": "^1.11.0", | ||
"koishi-utils": "^1.0.4" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
import { Context, appMap } from 'koishi-core' | ||
import { Context, appMap, GroupFlag } from 'koishi-core' | ||
import { sleep } from 'koishi-utils' | ||
@@ -15,7 +15,7 @@ | ||
async function broadcast (selfId: string | number, groups: number[], message: string) { | ||
async function broadcast (selfId: string | number, groupIds: number[], message: string) { | ||
const { sender } = appMap[selfId] | ||
for (let index = 0; index < groups.length; index++) { | ||
for (let index = 0; index < groupIds.length; index++) { | ||
if (index) await sleep(config.broadcastInterval) | ||
sender.sendGroupMsgAsync(groups[index], message) | ||
sender.sendGroupMsgAsync(groupIds[index], message) | ||
} | ||
@@ -25,2 +25,3 @@ } | ||
ctx.command('broadcast <message...>', '全服广播', { authority: 4 }) | ||
.option('-f, --forced', '无视 noEmit 标签进行广播') | ||
.option('-o, --only', '仅向当前 Bot 负责的群进行广播') | ||
@@ -31,9 +32,13 @@ .action(async ({ options, meta }, message) => { | ||
if (options.only) { | ||
const groups = await ctx.database.getAllGroups(['id'], [ctx.app.selfId]) | ||
let groups = await ctx.database.getAllGroups(['id', 'flag'], [ctx.app.selfId]) | ||
if (!options.forced) { | ||
groups = groups.filter(g => !(g.flag & GroupFlag.noEmit)) | ||
} | ||
return broadcast(ctx.app.selfId, groups.map(g => g.id), message) | ||
} | ||
const groups = await ctx.database.getAllGroups(['id', 'assignee']) | ||
const groups = await ctx.database.getAllGroups(['id', 'assignee', 'flag']) | ||
const assignMap: Record<number, number[]> = {} | ||
for (const { id, assignee } of groups) { | ||
for (const { id, assignee, flag } of groups) { | ||
if (!options.forced && (flag & GroupFlag.noEmit)) continue | ||
if (!assignMap[assignee]) { | ||
@@ -40,0 +45,0 @@ assignMap[assignee] = [id] |
@@ -9,2 +9,3 @@ import { Meta, Context } from 'koishi-core' | ||
ctx.receiver.on('group-increase', async (meta) => { | ||
if (meta.userId === ctx.app.selfId) return | ||
if (ctx.database) { | ||
@@ -11,0 +12,0 @@ const group = await ctx.database.getGroup(meta.groupId, 0, ['assignee']) |
@@ -1,3 +0,3 @@ | ||
import { MockedApp, BASE_SELF_ID } from 'koishi-test-utils' | ||
import { startAll, stopAll } from 'koishi-core' | ||
import { MockedApp, BASE_SELF_ID, utils } from 'koishi-test-utils' | ||
import { startAll, stopAll, GroupFlag } from 'koishi-core' | ||
import { broadcast } from '../src' | ||
@@ -16,2 +16,3 @@ import 'koishi-database-memory' | ||
await app1.database.getGroup(654, app1.selfId) | ||
await app1.database.setGroup(654, { flag: GroupFlag.noEmit }) | ||
await app2.database.getGroup(987, app2.selfId) | ||
@@ -22,7 +23,20 @@ }) | ||
utils.sleep.mockResolvedValue() | ||
beforeEach(() => utils.sleep.mockClear()) | ||
test('check message', async () => { | ||
await app1.receiveMessage('user', 'broadcast', 123) | ||
expect(utils.sleep).toBeCalledTimes(0) | ||
app1.shouldHaveLastRequests([ | ||
['send_private_msg', { message: '请输入要发送的文本。', userId: 123 }], | ||
]) | ||
app2.shouldHaveNoRequests() | ||
}) | ||
test('basic support', async () => { | ||
await app1.receiveMessage('user', 'broadcast foo bar', 123) | ||
expect(utils.sleep).toBeCalledTimes(0) | ||
app1.shouldHaveLastRequests([ | ||
['send_group_msg', { message: 'foo bar', groupId: 321 }], | ||
['send_group_msg', { message: 'foo bar', groupId: 654 }], | ||
]) | ||
@@ -36,7 +50,30 @@ app2.shouldHaveLastRequests([ | ||
await app1.receiveMessage('user', 'broadcast -o foo bar', 123) | ||
expect(utils.sleep).toBeCalledTimes(0) | ||
app1.shouldHaveLastRequests([ | ||
['send_group_msg', { message: 'foo bar', groupId: 321 }], | ||
]) | ||
app2.shouldHaveNoRequests() | ||
}) | ||
test('force emit', async () => { | ||
await app1.receiveMessage('user', 'broadcast -f foo bar', 123) | ||
expect(utils.sleep).toBeCalledTimes(1) | ||
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 }], | ||
]) | ||
app2.shouldHaveNoRequests() | ||
}) | ||
test('self only & force emit', async () => { | ||
await app1.receiveMessage('user', 'broadcast -of foo bar', 123) | ||
expect(utils.sleep).toBeCalledTimes(1) | ||
app1.shouldHaveLastRequests([ | ||
['send_group_msg', { message: 'foo bar', groupId: 321 }], | ||
['send_group_msg', { message: 'foo bar', groupId: 654 }], | ||
]) | ||
app2.shouldHaveNoRequests() | ||
}) |
Sorry, the diff of this file is not supported yet
309997
2600
Updatedkoishi-core@^1.11.0