Skip to content

Commit

Permalink
feat(qqguild): direct message capability
Browse files Browse the repository at this point in the history
  • Loading branch information
XxLittleCxX committed Sep 16, 2023
1 parent 806599d commit 1c99286
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
10 changes: 10 additions & 0 deletions adapters/qqguild/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ export class QQGuildBot extends Bot<QQGuildBot.Config> {
const r = await this.internal.getMessage(channelId, messageId)
return decodeMessage(this, r)
}

async deleteMessage(channelId: string, messageId: string) {
if (channelId.includes('_')) {
// direct message
const [guildId, _] = channelId.split('_')
await this.internal.deleteDM(guildId, messageId)
} else {
await this.internal.deleteMessage(channelId, messageId)
}
}
}

export namespace QQGuildBot {
Expand Down
9 changes: 9 additions & 0 deletions adapters/qqguild/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,13 @@ export class Internal {
async deleteReaction(channel_id: string, message_id: string, type: string, id: string) {
return this.http.delete(`/channels/${channel_id}/messages/${message_id}/reactions/${type}/${id}`)
}

async deleteMessage(channel_id: string, message_id: string) {
return this.http.delete(`/channels/${channel_id}/messages/${message_id}`)
}

async deleteDM(guild_id: string, message_id: string) {
// guild_id 是 createDMS 之后的 id
return this.http.delete(`/dms/${guild_id}/messages/${message_id}`)
}
}
13 changes: 12 additions & 1 deletion adapters/qqguild/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ export class QQGuildMessageEncoder extends MessageEncoder<QQGuildBot> {
private content: string = ''
private file: Buffer
private filename: string
reference: string
dms: QQGuild.DMS

async initDms() {
const dms = await this.bot.internal.createDMS(this.options.session.userId, this.session.guildId || this.options.session.guildId)
this.bot.ctx.logger('qqguild').debug(require('util').inspect(dms, false, null, true))
this.dms = dms
}

Expand All @@ -29,13 +31,16 @@ export class QQGuildMessageEncoder extends MessageEncoder<QQGuildBot> {
return
}
let endpoint = `/channels/${this.session.channelId}/messages`
let srcGuildId // directMsg
if (this.session.isDirect && !this.options?.session) {
// initiative send
endpoint = `/dms/${this.dms.guild_id}/messages`
srcGuildId = this.session.guildId
} else if (this.session.isDirect && this.options?.session) {
// @ts-ignore
const payload = this.options.session.qqguild.d as QQGuild.Message
endpoint = `/dms/${payload.guild_id}/messages`
srcGuildId = payload.src_guild_id
}
const form = new FormData()
form.append('content', this.content)
Expand All @@ -49,8 +54,14 @@ export class QQGuildMessageEncoder extends MessageEncoder<QQGuildBot> {
const r = await this.bot.http.post(endpoint, form, {
headers: form.getHeaders(),
})
this.bot.ctx.logger('qqguild').debug(require('util').inspect(r, false, null, true))
const session = this.bot.session()
await decodeMessage(this.bot, r, session)
if (this.session.isDirect) {
session.guildId = this.session.guildId
session.channelId = this.session.channelId
session.isDirect = true
}

// https://bot.q.qq.com/wiki/develop/api/gateway/direct_message.html#%E6%B3%A8%E6%84%8F
// session.guildId = this.session.guildId
Expand Down Expand Up @@ -88,8 +99,8 @@ export class QQGuildMessageEncoder extends MessageEncoder<QQGuildBot> {
} else if (type === 'sharp') {
this.content += `<#${attrs.id}>`
} else if (type === 'quote') {
this.reference = attrs.id
await this.flush()
// this.addition.reference = attrs.id
} else if (type === 'image' && attrs.url) {
await this.resolveFile(attrs)
await this.flush()
Expand Down
4 changes: 3 additions & 1 deletion adapters/qqguild/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export async function decodeMessage(bot: QQGuildBot, msg: QQGuild.Message, sessi
session.author = adaptUser(msg.author)
session.userId = author.id
if (msg.direct_message) {
session.guildId = msg.src_guild_id
// real guild id, dm's fake guild id
session.guildId = `${msg.src_guild_id}_${msg.guild_id}`
session.channelId = `${msg.guild_id}_${msg.channel_id}`
} else {
session.guildId = guild_id
session.channelId = channel_id
Expand Down

0 comments on commit 1c99286

Please sign in to comment.