Skip to content

Commit

Permalink
fix(qqguild): escape elements
Browse files Browse the repository at this point in the history
  • Loading branch information
XxLittleCxX committed Sep 25, 2023
1 parent 891b2f8 commit 3e6692c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
5 changes: 5 additions & 0 deletions adapters/qqguild/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export class QQGuildBot extends Bot<QQGuildBot.Config> {
return user
}

async getUser(userId: string, guildId?: string): Promise<Universal.User> {
const { user } = await this.getGuildMember(guildId, userId)
return user
}

async getGuildList(next?: string) {
const guilds = await this.internal.getGuilds()
return { data: guilds.map(adaptGuild) }
Expand Down
3 changes: 2 additions & 1 deletion adapters/qqguild/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Dict, h, MessageEncoder } from '@satorijs/satori'
import { QQGuildBot } from './bot'
import FormData from 'form-data'
import { decodeMessage } from './utils'
import { escape } from '@satorijs/element'

export class QQGuildMessageEncoder extends MessageEncoder<QQGuildBot> {
private content: string = ''
Expand Down Expand Up @@ -106,7 +107,7 @@ export class QQGuildMessageEncoder extends MessageEncoder<QQGuildBot> {
async visit(element: h) {
const { type, attrs, children } = element
if (type === 'text') {
this.content += attrs.content
this.content += escape(attrs.content)
} else if (type === 'at') {
switch (attrs.type) {
case 'all':
Expand Down
8 changes: 6 additions & 2 deletions adapters/qqguild/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { h, Session, Universal } from '@satorijs/satori'
import * as QQGuild from './types'
import { QQGuildBot } from './bot'
import { unescape } from '@satorijs/element'

export const adaptGuild = (guild: QQGuild.Guild): Universal.Guild => ({
id: guild.id,
Expand Down Expand Up @@ -49,13 +50,16 @@ export async function decodeMessage(bot: QQGuildBot, msg: QQGuild.Message, sessi
}
session.isDirect = !!msg.direct_message
session.content = (msg.content ?? '')
.replace(/<@!(.+)>/, (_, $1) => h.at($1).toString())
.replace(/<#(.+)>/, (_, $1) => h.sharp($1).toString())
.replace(/<@!(\d+)>/g, (_, $1) => h.at($1).toString())
// .replace(/<#(.+)>/, (_, $1) => h.sharp($1).toString()) // not used?
const { attachments = [] } = msg
session.content = attachments
.filter(({ content_type }) => content_type.startsWith('image'))
.reduce((content, attachment) => content + h.image('https://' + attachment.url), session.content)
session.elements = h.parse(session.content)
session.elements = h.transform(session.elements, {
text: (attrs) => unescape(attrs.content),
})

if (msg.message_reference) {
session.quote = await bot.getMessage(msg.channel_id, msg.message_reference.message_id)
Expand Down

0 comments on commit 3e6692c

Please sign in to comment.