Skip to content

Commit

Permalink
fix(satori): fix message send and receive
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 19, 2024
1 parent 94d40c5 commit 8321ff6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions adapters/satori/src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bot, Context, h, Quester, snakeCase, Universal } from '@satorijs/satori'
import { Bot, Context, h, Quester, Session, 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 @@ -34,13 +34,18 @@ export class SatoriBot<C extends Context = Context> extends Bot<C, Universal.Log
}

for (const [key, method] of Object.entries(Universal.Methods)) {
SatoriBot.prototype[method.name] = function (this: SatoriBot, ...args: any[]) {
SatoriBot.prototype[method.name] = async function (this: SatoriBot, ...args: any[]) {
const payload = {}
for (const { name } of method.fields) {
if (name === 'content') {
payload[name] = h.normalize(args.shift()).join('')
for (const [index, field] of method.fields.entries()) {
if (method.name === 'createMessage' && field.name === 'content') {
const session: Session = args[3]?.session ?? this.session({
type: 'send',
channel: { id: args[0], type: 0 },
})
const elements = await session.transform(h.normalize(args[index]))
payload[field.name] = elements.join('')
} else {
payload[name] = transformKey(args.shift(), snakeCase)
payload[field.name] = transformKey(args[index], snakeCase)
}
}
this.logger.debug('[request]', key, payload)
Expand Down
2 changes: 1 addition & 1 deletion adapters/satori/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class SatoriAdapter<C extends Context = Context> extends Adapter.WsClient
return bot.dispose()
}
const session = bot.session(parsed.body)
if (parsed.body.message?.content) {
if (typeof parsed.body.message?.content === 'string') {
session.content = parsed.body.message.content
}
if (parsed.body._type && parsed.body.type !== 'internal') {
Expand Down

0 comments on commit 8321ff6

Please sign in to comment.