Skip to content

Commit

Permalink
feat(satori): support before-send and send events
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 25, 2024
1 parent c464f59 commit d300ccc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 6 additions & 4 deletions adapters/satori/src/bot.ts
@@ -1,4 +1,4 @@
import { Bot, Context, h, Quester, Session, snakeCase, Universal } from '@satorijs/satori'
import { Bot, Context, h, Quester, snakeCase, Universal } from '@satorijs/satori'

export function transformKey(source: any, callback: (key: string) => string) {
if (!source || typeof source !== 'object') return source
Expand Down Expand Up @@ -38,12 +38,14 @@ for (const [key, method] of Object.entries(Universal.Methods)) {
const payload = {}
for (const [index, field] of method.fields.entries()) {
if (method.name === 'createMessage' && field.name === 'content') {
const session: Session = args[3]?.session ?? this.session({
const session = this.session({
type: 'send',
channel: { id: args[0], type: 0 },
...args[3]?.session?.event,
})
const elements = await session.transform(h.normalize(args[index]))
payload[field.name] = elements.join('')
session.elements = await session.transform(h.normalize(args[index]))
if (await session.app.serial(session, 'before-send', session, args[3] ?? {})) return
payload[field.name] = session.elements.join('')
} else {
payload[field.name] = transformKey(args[index], snakeCase)
}
Expand Down
4 changes: 4 additions & 0 deletions adapters/satori/src/ws.ts
Expand Up @@ -119,6 +119,10 @@ export class SatoriAdapter<C extends Context = Context> extends Adapter.WsClient
session.setInternal(parsed.body._type, parsed.body._data)
}
bot.dispatch(session)
// temporary solution for `send` event
if (type === 'message-created' && session.userId === selfId) {
session.app.emit(session, 'send', session)
}
}
})

Expand Down

0 comments on commit d300ccc

Please sign in to comment.