Skip to content

Commit

Permalink
fix(koishi): fix broadcast error, fix #1418
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jul 17, 2024
1 parent 605b1b7 commit 10e359a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
4 changes: 3 additions & 1 deletion packages/core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ class KoishiDatabase {
getAssignedChannels: 'database.getAssignedChannels',
setChannel: 'database.setChannel',
createChannel: 'database.createChannel',
broadcast: 'broadcast',
broadcast: 'database.broadcast',
})

ctx.mixin('database', ['broadcast'] as never[])

ctx.model.extend('user', {
id: 'unsigned(8)',
name: { type: 'string', length: 255 },
Expand Down
28 changes: 15 additions & 13 deletions plugins/common/broadcast/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,28 @@ before(async () => {

describe('@koishijs/plugin-broadcast', () => {
it('basic support', async () => {
const send = jest.fn<Bot['sendMessage']>(async () => [])
app.bots.forEach(bot => bot.sendMessage = send)
const send1 = jest.method(app.bots.find(bot => bot.selfId === '514')!, 'sendMessage')
const send2 = jest.method(app.bots.find(bot => bot.selfId === '114')!, 'sendMessage')

await client.shouldReply('broadcast', '请输入要发送的文本。')
expect(send.mock.calls).to.have.length(0)
expect(send1.mock.calls).to.have.length(1)
send1.mock.resetCalls()

await client.shouldNotReply('broadcast foo')
expect(send.mock.calls).to.have.length(2)
expect(send.mock.calls[0].arguments[0]).to.equal('222')
expect(send.mock.calls[1].arguments[0]).to.equal('111')
send.mock.resetCalls()
expect(send1.mock.calls).to.have.length(1)
expect(send1.mock.calls[0].arguments[0]).to.equal('222')
expect(send2.mock.calls).to.have.length(1)
expect(send2.mock.calls[0].arguments[0]).to.equal('111')
send1.mock.resetCalls()

await client.shouldNotReply('broadcast -o foo')
expect(send.mock.calls).to.have.length(1)
expect(send.mock.calls[0].arguments[0]).to.equal('222')
send.mock.resetCalls()
expect(send1.mock.calls).to.have.length(1)
expect(send1.mock.calls[0].arguments[0]).to.equal('222')
send1.mock.resetCalls()

await client.shouldNotReply('broadcast -of foo')
expect(send.mock.calls).to.have.length(2)
expect(send.mock.calls[0].arguments[0]).to.equal('222')
expect(send.mock.calls[1].arguments[0]).to.equal('333')
expect(send1.mock.calls).to.have.length(2)
expect(send1.mock.calls[0].arguments[0]).to.equal('222')
expect(send1.mock.calls[1].arguments[0]).to.equal('333')
})
})

0 comments on commit 10e359a

Please sign in to comment.