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

refa: MessageEncoder related changes #20

Merged
merged 4 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
13 changes: 3 additions & 10 deletions src/bot/base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bot, Context, Fragment, Schema, Universal } from 'koishi'
import { Bot, Context, Schema, Universal } from 'koishi'
import * as OneBot from '../utils'
import { OneBotMessageEncoder } from './message'
import { OneBotMessageEncoder, PRIVATE_PFX } from './message'

export class BaseBot<C extends Context = Context, T extends BaseBot.Config = BaseBot.Config> extends Bot<C, T> {
static MessageEncoder = OneBotMessageEncoder
Expand All @@ -9,15 +9,8 @@ export class BaseBot<C extends Context = Context, T extends BaseBot.Config = Bas
public parent?: BaseBot
public internal: OneBot.Internal

sendMessage(channelId: string, fragment: Fragment, guildId?: string, options?: Universal.SendOptions) {
if (!this.parent && !channelId.startsWith('private:')) {
guildId = channelId
}
return super.sendMessage(channelId, fragment, guildId, options)
}

async createDirectChannel(userId: string) {
return { id: 'private:' + userId, type: Universal.Channel.Type.DIRECT }
return { id: `${PRIVATE_PFX}${userId}`, type: Universal.Channel.Type.DIRECT }
}

async getMessage(channelId: string, messageId: string) {
Expand Down
26 changes: 20 additions & 6 deletions src/bot/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,30 @@ class State {
constructor(public type: 'message' | 'forward' | 'reply') {}
}

export const PRIVATE_PFX = 'private:'
lgc2333 marked this conversation as resolved.
Show resolved Hide resolved

export class OneBotMessageEncoder<C extends Context = Context> extends MessageEncoder<C, BaseBot<C>> {
stack: State[] = [new State('message')]
children: CQCode[] = []

override async prepare(): Promise<void> {
super.prepare()
const { event: { channel } } = this.session
if (!channel.type) {
channel.type = channel.id.startsWith(PRIVATE_PFX)
? Universal.Channel.Type.DIRECT
: Universal.Channel.Type.TEXT
}
this.session.isDirect = channel.type === Universal.Channel.Type.DIRECT
Copy link
Member

Choose a reason for hiding this comment

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

这个要设吗,core 里面的 isDirect 的 setter 和这个作用一样吧

Copy link
Contributor Author

Choose a reason for hiding this comment

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

不知道,il让这样改的

Copy link
Member

Choose a reason for hiding this comment

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

这个要设吗,core 里面的 isDirect 的 setter 和这个作用一样吧

在哪里

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

isDirect 是 accessor,已经设置了 type 的情况下不应该手动设置。

应当仅同步 type 本身。

lgc2333 marked this conversation as resolved.
Show resolved Hide resolved
if (!this.session.isDirect) this.guildId = this.channelId
}

async forward() {
if (!this.stack[0].children.length) return
const session = this.bot.session()
session.messageId = this.guildId
? '' + await this.bot.internal.sendGroupForwardMsg(this.guildId, this.stack[0].children)
: '' + await this.bot.internal.sendPrivateForwardMsg(this.channelId.slice(8), this.stack[0].children)
session.messageId = this.session.event.channel.type === Universal.Channel.Type.DIRECT
Copy link
Member

@XxLittleCxX XxLittleCxX Feb 21, 2024

Choose a reason for hiding this comment

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

同上

? '' + await this.bot.internal.sendPrivateForwardMsg(this.channelId.slice(PRIVATE_PFX.length), this.stack[0].children)
: '' + await this.bot.internal.sendGroupForwardMsg(this.channelId, this.stack[0].children)
session.userId = this.bot.selfId
session.channelId = this.session.channelId
session.guildId = this.session.guildId
Expand Down Expand Up @@ -81,9 +95,9 @@ export class OneBotMessageEncoder<C extends Context = Context> extends MessageEn
const session = this.bot.session()
session.messageId = this.bot.parent
? '' + await this.bot.internal.sendGuildChannelMsg(this.guildId, this.channelId, this.children)
: this.guildId
? '' + await this.bot.internal.sendGroupMsg(this.guildId, this.children)
: '' + await this.bot.internal.sendPrivateMsg(this.channelId.slice(8), this.children)
: this.session.event.channel.type === Universal.Channel.Type.DIRECT
shigma marked this conversation as resolved.
Show resolved Hide resolved
? '' + await this.bot.internal.sendPrivateMsg(this.channelId.slice(PRIVATE_PFX.length), this.children)
: '' + await this.bot.internal.sendGroupMsg(this.channelId, this.children)
session.userId = this.bot.selfId
session.channelId = this.session.channelId
session.guildId = this.session.guildId
Expand Down