Skip to content

Commit

Permalink
feat(plugin-common): support broadcast -o
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 27, 2020
1 parent 17a6ee9 commit baf7f1f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
31 changes: 20 additions & 11 deletions packages/plugin-common/src/broadcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,27 @@ const defaultOptions: BroadcastOptions = {
broadcastInterval: 1000,
}

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'])
const assignMap: Record<number, number[]> = {}
for (const { id, assignee } of groups) {
Expand All @@ -23,13 +39,6 @@ export default function apply (ctx: Context, options: BroadcastOptions = {}) {
assignMap[assignee].push(id)
}
}
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)))
})
}
50 changes: 28 additions & 22 deletions packages/plugin-common/tests/broadcast.spec.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,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)

jest.useFakeTimers()
app1.plugin(broadcast)

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())

async function nextTick () {
jest.advanceTimersToNextTimer()
jest.useRealTimers()
await sleep(0)
jest.useFakeTimers()
}
afterAll(() => stopAll())

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()
})

0 comments on commit baf7f1f

Please sign in to comment.