Skip to content

Commit

Permalink
feat(core): support groupId of send events in discord/kaiheila
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 30, 2021
1 parent a19f778 commit 8976f25
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 27 deletions.
8 changes: 4 additions & 4 deletions packages/adapter-discord/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ export class DiscordBot extends Bot<'discord'> {
return sentMessageId
}

async sendMessage(channelId: string, content: string) {
const session = this.createSession({ channelId, content })
async sendMessage(channelId: string, content: string, groupId?: string) {
const session = this.createSession({ channelId, content, groupId, subtype: groupId ? 'group' : 'private' })
if (await this.app.serial(session, 'before-send', session)) return

const chain = segment.parse(session.content)
Expand All @@ -155,10 +155,10 @@ export class DiscordBot extends Bot<'discord'> {
message_id: quote,
} : undefined

const sentMessageId = await this.sendFullMessage(`/channels/${channelId}/messages`, session.content, { message_reference })
session.messageId = await this.sendFullMessage(`/channels/${channelId}/messages`, session.content, { message_reference })

this.app.emit(session, 'send', session)
return session.messageId = sentMessageId
return session.messageId
}

async deleteMessage(channelId: string, messageId: string) {
Expand Down
10 changes: 4 additions & 6 deletions packages/adapter-kaiheila/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,17 @@ export class KaiheilaBot extends Bot {
return result.data
}

private _prepareHandle(channelId: string, content: string): SendHandle {
private _prepareHandle(channelId: string, content: string, groupId: string): SendHandle {
let path: string
const params = {} as KHL.MessageParams
const session = this.createSession({ channelId, content })
const session = this.createSession({ channelId, content, groupId })
if (channelId.length > 30) {
params.chatCode = channelId
session.subtype = 'private'
path = '/user-chat/create-msg'
} else {
params.targetId = channelId
session.subtype = 'group'
// FIXME this is incorrect but to workarournd ctx.group()
session.groupId = 'unknown'
path = '/message/create'
}
return [path, params, session]
Expand Down Expand Up @@ -189,8 +187,8 @@ export class KaiheilaBot extends Bot {
await flush()
}

async sendMessage(channelId: string, content: string) {
const handle = this._prepareHandle(channelId, content)
async sendMessage(channelId: string, content: string, groupId?: string) {
const handle = this._prepareHandle(channelId, content, groupId)
const [, params, session] = handle
if (await this.app.serial(session, 'before-send', session)) return

Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-core/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export interface Bot<P = Platform> extends BotOptions, UserBase {
getStatus(): Promise<Bot.Status>

// message
sendMessage(channelId: string, content: string): Promise<string>
sendMessage(channelId: string, content: string, groupId?: string): Promise<string>
sendPrivateMessage(userId: string, content: string): Promise<string>
getMessage(channelId: string, messageId: string): Promise<MessageInfo>
editMessage(channelId: string, messageId: string, content: string): Promise<void>
Expand Down Expand Up @@ -260,7 +260,7 @@ export class Bot<P extends Platform> {
for (let index = 0; index < channels.length; index++) {
if (index && delay) await sleep(delay)
try {
messageIds.push(await this.sendMessage(channels[index], content))
messageIds.push(await this.sendMessage(channels[index], content, 'unknown'))
} catch (error) {
this.app.logger('bot').warn(error)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/koishi-core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class Session<
return this.bot[Session.send](this, message)
}
if (!message) return
await this.bot.sendMessage(this.channelId, message)
await this.bot.sendMessage(this.channelId, message, this.groupId)
}

cancelQueued(delay = this.app.options.delay.cancel) {
Expand Down
8 changes: 4 additions & 4 deletions packages/koishi/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ interface Message {

process.on('message', (data: Message) => {
if (data.type === 'send') {
const { channelId, sid, message } = data.body
const { channelId, groupId, sid, message } = data.body
const bot = app.bots[sid]
bot.sendMessage(channelId, message)
bot.sendMessage(channelId, message, groupId)
}
})

Expand Down Expand Up @@ -162,12 +162,12 @@ exitCommand && app
.shortcut('关机', { prefix: true })
.shortcut('重启', { prefix: true, options: { restart: true } })
.action(async ({ options, session }) => {
const { channelId, sid } = session
const { channelId, groupId, sid } = session
if (!options.restart) {
await session.send(template('deamon.exiting')).catch(noop)
process.exit()
}
process.send({ type: 'queue', body: { channelId, sid, message: template('deamon.restarted') } })
process.send({ type: 'queue', body: { channelId, groupId, sid, message: template('deamon.restarted') } })
await session.send(template('deamon.restarting')).catch(noop)
process.exit(114)
})
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-chat/client/chat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ function handleClick(message: Message) {
function handleSend(content: string) {
if (!activeMessage.value) return
pinned.value = false
const { platform, selfId, channelId } = activeMessage.value
const { platform, selfId, channelId, groupId } = activeMessage.value
const { token, id } = user.value
send('chat', { token, id, content, platform, selfId, channelId })
send('chat', { token, id, content, platform, selfId, channelId, groupId })
}
function onClickQuote(id: string) {
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-chat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ export function apply(ctx: Context, options: Config = {}) {
ctx.webui.global.maxMessages = options.maxMessages
ctx.webui.addEntry(resolve(__dirname, filename))

ctx.webui.addListener('chat', async function ({ id, token, content, platform, selfId, channelId }) {
ctx.webui.addListener('chat', async function ({ id, token, content, platform, selfId, channelId, groupId }) {
const user = await this.validate(id, token, ['name', 'authority'])
if (!user) return
if (user.authority < 4) return this.send('unauthorized')
content = await ctx.transformAssets(content)
ctx.bots[`${platform}:${selfId}`]?.sendMessage(channelId, content)
ctx.bots[`${platform}:${selfId}`]?.sendMessage(channelId, content, groupId)
})

ctx.on('connect', () => {
Expand Down
10 changes: 5 additions & 5 deletions packages/plugin-common/src/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function echo(ctx: Context) {
if (options.user) {
await bot.sendPrivateMessage(id, message)
} else {
await bot.sendMessage(id, message)
await bot.sendMessage(id, message, 'unknown')
}
return
}
Expand All @@ -133,7 +133,7 @@ export function echo(ctx: Context) {
}

export function feedback(ctx: Context, operators: string[]) {
type FeedbackData = [sid: string, channelId: string]
type FeedbackData = [sid: string, channelId: string, groupId: string]
const feedbacks: Record<number, FeedbackData> = {}

ctx.command('common/feedback <message:text>', '发送反馈信息给作者')
Expand All @@ -144,7 +144,7 @@ export function feedback(ctx: Context, operators: string[]) {
const nickname = name === '' + userId ? userId : `${name} (${userId})`
const message = template('common.feedback-receive', nickname, text)
const delay = ctx.app.options.delay.broadcast
const data: FeedbackData = [session.sid, session.channelId]
const data: FeedbackData = [session.sid, session.channelId, session.groupId]
for (let index = 0; index < operators.length; ++index) {
if (index && delay) await sleep(delay)
const [platform, userId] = Argv.parsePid(operators[index])
Expand All @@ -159,7 +159,7 @@ export function feedback(ctx: Context, operators: string[]) {
if (!parsed.content || !quote) return next()
const data = feedbacks[quote.messageId]
if (!data) return next()
return ctx.bots[data[0]].sendMessage(data[1], parsed.content)
return ctx.bots[data[0]].sendMessage(data[1], parsed.content, data[2])
})
}

Expand Down Expand Up @@ -212,7 +212,7 @@ export function relay(ctx: Context, relays: RelayOptions[]) {
const bot = ctx.getBot(platform, selfId)
if (!session.parsed.content) return
const content = template('common.relay', session.username, session.parsed.content)
const id = await bot.sendMessage(channelId, content)
const id = await bot.sendMessage(channelId, content, 'unknown')
relayMap[id] = { source: destination, destination: session.cid, selfId: session.selfId, lifespan }
setTimeout(() => delete relayMap[id], lifespan)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-monitor/src/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class Daemon {
}
if (app.bail('monitor/before-send', info, group)) return
for (const message of messages) {
await bot.sendMessage(channelId, message)
await bot.sendMessage(channelId, message, 'unknown')
}
})
}
Expand Down

0 comments on commit 8976f25

Please sign in to comment.