插件开发求助【API接口类】

目前的想法是这样的:一个 qq 群中有许多群文件夹,群文件夹中有许多群文件。

功能:事先设定哪些用户属于那些文件夹,如果有新的文件加入到该文件夹中,机器人私下提醒用户有新文件。{类似频道订阅功能,有新变动就提醒你}

例子:共有 3 个文件夹:测试 1,测试 2,测试 3。让机器人记住,测试 1,测试 2 是属于某一qq 用户的。

现在,测试 1 文件夹,测试 3 文件夹都有新的文件加入。机器人需要私下提醒用户:机器人提提你,【测试 1 文件夹】有新的文件,注意查收

细分为两个操作:

  1. 让用户登记哪些文件夹是属于他的,例如通过指令:ww 登记 【文件夹名称】,储存到后台数据库,让机器人记住。
  2. 通过自动扫描接口识别,检测新文件并提醒用户

现在主要的难点在于不知道有没有文件状态类的接口可以用,权限够不够。如果有的话,checkNewFile函数比较容易实现。

粗略概念代码:

import { Context, Schema } from 'koishi'

export const name = 'tangerine-filemanagement'

export interface Config {}

export const Config: Schema<Config> = Schema.object({})

export function apply(ctx: Context) {
  // 初始化数据库表
  ctx.model.extend('user_folder', {
    userId: 'string',
    folderName: 'string',
  })

  // 定义指令:ww登记
  ctx.command('ww登记 <folderName:text>', '登记文件夹关联')
    .action(async ({ session }, folderName) => {
      if (!folderName) return '请提供文件夹名称。'
      await ctx.model.create('user_folder', {
        userId: session.userId,
        folderName,
      })
      return `已成功登记文件夹 ${folderName}。`
    })

  // 检测新文件的定时任务
  setInterval(async () => {
    **// 假设这里有一个函数 checkNewFiles 可以检测新文件**
**    const newFiles = await checkNewFiles()**
    for (const folderName in newFiles) {
      // 查询与该文件夹关联的用户
      const users = await ctx.model.get('user_folder', { folderName })
      for (const user of users) {
        // 发送私聊提醒
        ctx.bot.sendPrivateMsg(user.userId, `机器人提提你,【${folderName}文件夹】有新的文件,注意查收`)
      }
    }
  }, 60 * 1000) // 每分钟检测一次

假设【checkNewFiles】:

// 用于存储每个文件夹的文件状态
const folderFileStatus: Record<string, Set<string>> = {};

// 模拟检测新文件的函数
async function checkNewFiles() {
    const newFiles: Record<string, boolean> = {};
    **// 假设这里有一个函数 getFolderFiles 可以获取群文件夹的文件信息**
**    const currentFolderFiles = await getFolderFiles();**

    for (const [folderName, files] of Object.entries(currentFolderFiles)) {
        const fileSet = new Set(files);
        if (!folderFileStatus[folderName]) {
            // 如果是第一次获取该文件夹的文件信息,记录当前状态
            folderFileStatus[folderName] = fileSet;
        } else {
            // 对比文件状态,判断是否有新文件
            const hasNewFiles = [...fileSet].some(file => !folderFileStatus[folderName].has(file));
            if (hasNewFiles) {
                newFiles[folderName] = true;
                // 更新文件状态
                folderFileStatus[folderName] = fileSet;
            }
        }
    }
    return newFiles;
}
2 个赞

onebot有内部API,感觉可以 onebot.getGroupFilesByFolder()

3 个赞

太感谢了!!这就去试试看o(╥﹏╥)o

2 个赞