Skip to content

Commit

Permalink
feat(sandbox): support guild-member-* events
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jul 30, 2024
1 parent 14ec1b6 commit f63322a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
2 changes: 1 addition & 1 deletion plugins/sandbox/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@koishijs/plugin-sandbox",
"description": "Test Your Virtual Bot in Console",
"version": "3.3.1",
"version": "3.4.0",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand Down
6 changes: 2 additions & 4 deletions plugins/sandbox/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ export class SandboxBot<C extends Context = Context> extends Bot<C, SandboxBot.C

hidden = true
internal = {}
clients = new Set<Client>()

constructor(ctx: C, config: SandboxBot.Config) {
constructor(ctx: C, public client: Client, config: SandboxBot.Config) {
super(ctx, config)
this.selfId = config.selfId
this.platform = config.platform
this.user.name = 'koishi'
}

async request<T = any>(method: string, data = {}) {
const client = [...this.clients][0]
const nonce = Math.random().toString(36).slice(2)
return new Promise<T>((resolve, reject) => {
const dispose1 = this.ctx.on('sandbox/response', (nonce2, data) => {
Expand All @@ -38,7 +36,7 @@ export class SandboxBot<C extends Context = Context> extends Bot<C, SandboxBot.C
dispose2()
reject(new Error('timeout'))
}, Time.second * 5)
client.send({
this.client.send({
type: 'sandbox/request',
body: { method, data, nonce },
})
Expand Down
41 changes: 23 additions & 18 deletions plugins/sandbox/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { $, Context, Dict, Random, Schema, Universal, User } from 'koishi'
import { DataService } from '@koishijs/console'
import { Client, DataService } from '@koishijs/console'
import { resolve } from 'path'
import { SandboxBot } from './bot'
import zhCN from './locales/zh-CN.yml'
Expand Down Expand Up @@ -87,12 +87,16 @@ export function apply(ctx: Context, config: Config) {
}
}

ctx.console.addListener('sandbox/send-message', async function (platform, userId, channel, content, quote) {
const bot = bots[platform] ||= new SandboxBot(ctx, {
const ensureBot = (platform: string, client: Client) => {
// assert unique platform
return bots[platform] ||= new SandboxBot(ctx, client, {
platform,
selfId: 'koishi',
})
bot.clients.add(this)
}

ctx.console.addListener('sandbox/send-message', async function (platform, userId, channel, content, quote) {
const bot = ensureBot(platform, this)
const id = Random.id()
this.send({
type: 'sandbox/message',
Expand All @@ -110,11 +114,7 @@ export function apply(ctx: Context, config: Config) {
}, { authority: 4 })

ctx.console.addListener('sandbox/delete-message', async function (platform, userId, channel, messageId) {
const bot = bots[platform] ||= new SandboxBot(ctx, {
platform,
selfId: 'koishi',
})
bot.clients.add(this)
const bot = ensureBot(platform, this)
const session = bot.session(createEvent(userId, channel))
session.type = 'message-deleted'
session.messageId = messageId
Expand All @@ -132,6 +132,15 @@ export function apply(ctx: Context, config: Config) {
}, { authority: 4 })

ctx.console.addListener('sandbox/set-user', async function (platform, pid, data) {
const bot = ensureBot(platform, this)
const session = bot.session(createEvent(pid, '#'))
if (data) {
session.type = 'guild-member-added'
ctx.emit('guild-member-added', session)
} else {
session.type = 'guild-member-removed'
ctx.emit('guild-member-removed', session)
}
const database = ctx.get('database')
if (!database) return
const [binding] = await database.get('binding', { platform, pid }, ['aid'])
Expand All @@ -158,10 +167,8 @@ export function apply(ctx: Context, config: Config) {

ctx.on('console/connection', async (client) => {
if (ctx.console.clients[client.id]) return
for (const platform of Object.keys(bots)) {
const bot = bots[platform]
bot.clients.delete(client)
if (!bot.clients.size) {
for (const [platform, bot] of Object.entries(bots)) {
if (bot.client === client) {
delete bots[platform]
delete ctx.bots[bot.sid]
}
Expand All @@ -173,10 +180,8 @@ export function apply(ctx: Context, config: Config) {
ctx.intersect(session => session.platform.startsWith('sandbox:'))
.command('clear')
.action(({ session }) => {
for (const client of (session.bot as SandboxBot).clients) {
client.send({
type: 'sandbox/clear',
})
}
(session.bot as SandboxBot).client.send({
type: 'sandbox/clear',
})
})
}
22 changes: 10 additions & 12 deletions plugins/sandbox/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@ export class SandboxMessenger<C extends Context = Context> extends MessageEncode
const content = await h.transformAsync(this.buffer.trim(), this.rules)
const session = this.bot.session(this.session.event)
session.messageId = Random.id()
for (const client of this.bot.clients) {
client.send({
type: 'sandbox/message',
body: {
content,
user: 'Koishi',
channel: session.channelId,
id: session.messageId,
platform: session.platform,
},
})
}
this.bot.client.send({
type: 'sandbox/message',
body: {
content,
user: 'Koishi',
channel: session.channelId,
id: session.messageId,
platform: session.platform,
},
})
this.results.push(session.event.message)
this.buffer = ''
}
Expand Down

0 comments on commit f63322a

Please sign in to comment.