Skip to content

fix: detect private messages #52

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/bot/base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bot, Context, Schema, Universal } from 'koishi'
import * as OneBot from '../utils'
import { OneBotMessageEncoder, PRIVATE_PFX } from './message'
import { OneBotMessageEncoder } from './message'

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

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

async getMessage(channelId: string, messageId: string) {
Expand Down
3 changes: 1 addition & 2 deletions src/bot/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Context, Dict, h, MessageEncoder, pick, Universal } from 'koishi'
import { BaseBot } from './base'
import { CQCode } from './cqcode'
import { fileURLToPath } from 'node:url'
import { PRIVATE_PFX } from '../utils'

export interface Author extends Universal.User {
time?: string | number
Expand All @@ -15,8 +16,6 @@ class State {
constructor(public type: 'message' | 'forward' | 'reply') { }
}

export const PRIVATE_PFX = 'private:'

export class OneBotMessageEncoder<C extends Context = Context> extends MessageEncoder<C, BaseBot<C>> {
stack: State[] = [new State('message')]
children: CQCode[] = []
Expand Down
8 changes: 5 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as qface from 'qface'
import { BaseBot, CQCode } from './bot'
import * as OneBot from './types'

export const PRIVATE_PFX = 'private:'

export * from './types'

export const decodeUser = (user: OneBot.AccountInfo): Universal.User => ({
Expand Down Expand Up @@ -121,10 +123,10 @@ export async function adaptMessage(
const decodeGuildChannelId = (data: OneBot.Message) => {
if (data.guild_id) {
return [data.guild_id, data.channel_id]
} else if (data.group_id) {
return [data.group_id.toString(), data.group_id.toString()]
} else if (data.message_type === 'private') {
return [undefined, PRIVATE_PFX + data.sender.user_id]
} else {
return [undefined, 'private:' + data.sender.user_id]
return [data.group_id.toString(), data.group_id.toString()]
}
}

Expand Down
Loading