Skip to content

Commit

Permalink
feat(core): drop legacy encoder.guildId
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 30, 2023
1 parent 69cf597 commit 8aaf9c0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
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})`,
].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

0 comments on commit 8aaf9c0

Please sign in to comment.