Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix attachments is not resolve and support resolve string intents #672

Merged
merged 9 commits into from
May 10, 2022
6 changes: 3 additions & 3 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",
NWYLZW marked this conversation as resolved.
Show resolved Hide resolved
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand Down Expand Up @@ -33,7 +33,7 @@
"koishi": "^4.6.2"
},
"dependencies": {
"@qq-guild-sdk/core": "^1.1.4",
"@qq-guild-sdk/core": "^2.0.1",
"qface": "^1.2.0"
}
}
}
5 changes: 4 additions & 1 deletion plugins/adapter/qqguild/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ export const AdapterConfig: Schema<AdapterConfig> = Schema.intersect([
Adapter.WebSocketClient.Config,
])

type Intents = keyof typeof QQGuild.Bot.Intents

export interface BotConfig extends Bot.BaseConfig, QQGuild.Bot.AppConfig {
indents: number
intents: number | Intents | Intents[]
shigma marked this conversation as resolved.
Show resolved Hide resolved
}

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(),
NWYLZW marked this conversation as resolved.
Show resolved Hide resolved
}),
])

Expand Down
21 changes: 19 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,19 +23,35 @@ 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,
NWYLZW marked this conversation as resolved.
Show resolved Hide resolved
)
return new Session(bot, session)
}

export class WebSocketClient extends Adapter<BotConfig, AdapterConfig> {
static schema = BotConfig

resolveIntents(intents: BotConfig['intents']): number {
if (typeof intents === 'number') {
return intents
}
Array.isArray(intents) || (intents = [intents])
NWYLZW marked this conversation as resolved.
Show resolved Hide resolved
return intents.reduce((sum, intent) => sum | QQGuild.Bot.Intents[intent], 0)
}

async connect(bot: QQGuildBot) {
Object.assign(bot, await bot.getSelf())
bot.resolve()
await bot.$innerBot.startClient(bot.config.indents)
if (bot.config.intents === undefined) {
throw new Error('intents is required')
}
NWYLZW marked this conversation as resolved.
Show resolved Hide resolved
await bot.$innerBot.startClient(this.resolveIntents(bot.config.intents))
bot.$innerBot.on('ready', bot.resolve)
bot.$innerBot.on('message', msg => {
const session = createSession(bot, msg)
Expand Down