Skip to content

Commit

Permalink
fix(qqguild): fix attachments break session dispatch, fix #622 (#672)
Browse files Browse the repository at this point in the history
  • Loading branch information
NWYLZW authored May 10, 2022
1 parent 6f5b959 commit 6a19380
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
4 changes: 2 additions & 2 deletions plugins/adapter/qqguild/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@koishijs/plugin-adapter-qqguild",
"description": "QQ Guild Adapter for Koishi",
"version": "1.1.2",
"version": "2.0.0",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand Down Expand Up @@ -33,7 +33,7 @@
"koishi": "^4.6.3"
},
"dependencies": {
"@qq-guild-sdk/core": "^1.1.4",
"@qq-guild-sdk/core": "^2.0.1",
"qface": "^1.2.0"
}
}
10 changes: 7 additions & 3 deletions plugins/adapter/qqguild/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Guild as GGuild, User as GUser } from '@qq-guild-sdk/core/dist/common'
import { Adapter, Bot, Schema } from 'koishi'
import { Guild as GGuild, User as GUser } from '@qq-guild-sdk/core'
import * as QQGuild from '@qq-guild-sdk/core'
import { Adapter, Bot, Schema } from 'koishi'

export interface AdapterConfig extends Adapter.WebSocketClient.Config, Omit<QQGuild.Bot.Options, 'app'> {}

Expand All @@ -13,15 +13,19 @@ export const AdapterConfig: Schema<AdapterConfig> = Schema.intersect([
Adapter.WebSocketClient.Config,
])

const Intents = QQGuild.Bot.Intents
type Intents = keyof typeof Intents

export interface BotConfig extends Bot.BaseConfig, QQGuild.Bot.AppConfig {
indents: number
intents: number
}

export const BotConfig = Schema.intersect([
Schema.object({
id: Schema.string().description('机器人 id。').required(),
key: Schema.string().description('机器人 key。').role('secret').required(),
token: Schema.string().description('机器人令牌。').role('secret').required(),
intents: Schema.number().description('需要订阅的机器人事件。').default(Intents.PUBLIC_GUILD_MESSAGES),
}),
])

Expand Down
8 changes: 6 additions & 2 deletions plugins/adapter/qqguild/src/ws.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Message } from '@qq-guild-sdk/core'
import * as QQGuild from '@qq-guild-sdk/core'
import { Logger, segment } from '@koishijs/utils'
import { Adapter, Session } from 'koishi'
import { QQGuildBot } from './bot'
Expand All @@ -22,9 +23,12 @@ const createSession = (bot: QQGuildBot, msg: Message) => {
session.guildId = msg.guildId
session.channelId = msg.channelId
session.subtype = 'group'
session.content = msg.content
session.content = (msg.content ?? '')
.replace(/<@!(.+)>/, (_, $1) => segment.at($1))
.replace(/<#(.+)>/, (_, $1) => segment.sharp($1))
session.content = (msg as any as { attachments: any[] }).attachments
.filter(({ contentType }) => contentType.startsWith('image'))
.reduce((content, attachment) => content + segment.image(attachment.url), session.content)
return new Session(bot, session)
}

Expand All @@ -34,7 +38,7 @@ export class WebSocketClient extends Adapter<BotConfig, AdapterConfig> {
async connect(bot: QQGuildBot) {
Object.assign(bot, await bot.getSelf())
bot.resolve()
await bot.$innerBot.startClient(bot.config.indents)
await bot.$innerBot.startClient(bot.config.intents)
bot.$innerBot.on('ready', bot.resolve)
bot.$innerBot.on('message', msg => {
const session = createSession(bot, msg)
Expand Down

0 comments on commit 6a19380

Please sign in to comment.