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

feat(satori): drop legacy encoder.guildId #216

Merged
merged 3 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions adapters/discord/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,18 @@ export class DiscordMessageEncoder<C extends Context = Context> extends MessageE
}
} else {
// quote
let replyId = attrs.id, channelId = this.channelId
let replyId = attrs.id, guildId = this.session.guildId, channelId = this.channelId
if (this.stack[0].type === 'forward' && this.stack[0].fakeMessageMap[attrs.id]?.length >= 1) {
// quote to fake message, eg. 1st message has id (in channel or thread), later message quote to it
replyId = this.stack[0].fakeMessageMap[attrs.id][0].id
guildId = this.stack[0].fakeMessageMap[attrs.id][0].guild.id
channelId = this.stack[0].fakeMessageMap[attrs.id][0].channel.id
}
const quote = await this.bot.getMessage(channelId, replyId)
this.addition.embeds = [{
description: [
sanitize(parse(quote.elements.filter(v => v.type === 'text').join('')).slice(0, 30)),
`<t:${Math.ceil(quote.timestamp / 1000)}:R> [[ ↑ ]](https://discord.com/channels/${this.guildId}/${channelId}/${replyId})`,
`<t:${Math.ceil(quote.timestamp / 1000)}:R> [[ ↑ ]](https://discord.com/channels/${guildId}/${channelId}/${replyId})`,
Comment on lines -343 to +351
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里需要同时有 guildIdchannelId 才行吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那这里大概可以单独给 quote 加个属性,用来填充或覆盖 guildIdchannelId?直接调用 sendMessage 的时候没有接收的 session

].join('\n\n'),
author: {
name: quote.user.name,
Expand Down
2 changes: 2 additions & 0 deletions adapters/kook/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ export class KookMessageEncoder<C extends Context = Context> extends MessageEnco

async prepare() {
if (isDirectChannel(this.session.channelId)) {
this.session.isDirect = true
this.params.chat_code = this.session.channelId
this.path = '/user-chat/create-msg'
} else {
this.session.isDirect = false
this.params.target_id = this.session.channelId
this.path = '/message/create'
}
Expand Down
11 changes: 6 additions & 5 deletions adapters/telegram/src/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Dict, h, MessageEncoder, Universal } from '@satorijs/satori'
import { Context, Dict, h, MessageEncoder } from '@satorijs/satori'
import FormData from 'form-data'
import { TelegramBot } from './bot'
import * as Telegram from './utils'
Expand Down Expand Up @@ -35,11 +35,12 @@ export class TelegramMessageEncoder<C extends Context = Context> extends Message
private mode: RenderMode = 'default'
private rows: Telegram.InlineKeyboardButton[][] = []

constructor(bot: TelegramBot<C>, channelId: string, guildId?: string, options?: Universal.SendOptions) {
super(bot, channelId, guildId, options)
const chat_id = guildId || channelId
async prepare() {
const chat_id = this.session.guildId || this.channelId
this.payload = { chat_id, parse_mode: 'html', caption: '' }
if (guildId && channelId !== guildId) this.payload.message_thread_id = +channelId
if (this.session.guildId && this.channelId !== this.session.guildId) {
this.payload.message_thread_id = +this.channelId
}
}

async addResult(result: Telegram.Message) {
Expand Down
2 changes: 1 addition & 1 deletion adapters/zulip/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class ZulipMessageEncoder<C extends Context = Context> extends MessageEnc
} else if (type === 'quote') {
const quoteMsg = await this.bot.internal.getMessage(attrs.id)
const suffix = '/near/' + encodeHashComponent(attrs.id)
const path = by_stream_topic_url(Number(this.guildId), this.channelId) + suffix
const path = by_stream_topic_url(+this.session.guildId, this.channelId) + suffix

this.buffer = `@_**${quoteMsg.message.sender_full_name}|${quoteMsg.message.sender_id}** [Said](${path}):\n`
+ '```quote\n' + quoteMsg.raw_content + '\n```\n\n' + this.buffer
Expand Down
12 changes: 5 additions & 7 deletions packages/core/src/message.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Bot } from './bot'
import { Channel, Message, SendOptions } from '@satorijs/protocol'
import { Message, SendOptions } from '@satorijs/protocol'
import h from '@satorijs/element'
import { Context } from '.'

Expand All @@ -14,9 +14,9 @@ export abstract class MessageEncoder<C extends Context = Context, B extends Bot<
public results: Message[] = []
public session: C[typeof Context.session]

constructor(public bot: B, public channelId: string, public guildId?: string, public options: SendOptions = {}) { }
constructor(public bot: B, public channelId: string, public guildId?: string, public options: SendOptions = {}) {}

async prepare() { }
async prepare() {}

abstract flush(): Promise<void>
abstract visit(element: h): Promise<void>
Expand All @@ -31,12 +31,10 @@ export abstract class MessageEncoder<C extends Context = Context, B extends Bot<
}

async send(content: h.Fragment) {
const isDirect = this.options.session?.isDirect ?? !this.guildId
this.session = this.bot.session({
type: 'send',
channel: { id: this.channelId, type: isDirect ? Channel.Type.DIRECT : Channel.Type.TEXT },
guild: { id: this.guildId },
subtype: isDirect ? 'private' : 'group',
channel: { id: this.channelId, ...this.options.session?.event.channel },
guild: this.options.session?.event.guild,
})
for (const key in this.options.session || {}) {
if (key === 'id' || key === 'event') continue
Expand Down