前言

放入一个空js文件中,该文件放入[云崽根目录]/plugins/example文件夹下
其中,${process.cwd()}代表云崽根目录
repoPath是仓库克隆路径,你的图片仓库会克隆到该目录下
repoUrl是Git仓库地址,以.git结尾
imgFolder是图片文件夹路径,如果你在git仓库中将图片放到某个文件夹中,请在repoPath的基础上追加路径,直到存放图片的文件夹,且你不需要用/结尾该路径
git仓库示例

random.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import fs from 'fs'
import path from 'path'
import { execSync } from 'child_process'

const repoPath = `${process.cwd()}/resources/group/Pic` // 仓库克隆路径
const repoUrl = 'https://gitee.com/Elvin-Apocalys/xxx.git' // Git仓库地址
const imgFolder = `${process.cwd()}/resources/group/Pic/sjqy` // 图片文件夹路径

export class randomPic extends plugin {
constructor() {
super({
name: '随机群友图',
dsc: '发送随机群友图片/更新图库',
event: 'message',
priority: -1000,
rule: [
{
reg: '^#?随机群友$',
fnc: 'sendRandomPic'
},
{
reg: '^#?随机群友更新$',
fnc: 'updatePicRepo'
}
]
})
}

/** 发送随机图片 */
async sendRandomPic(e) {
if(e.group_id == 654830026)
{
e.reply("喵喵喵?")
return
}
if (!fs.existsSync(imgFolder)) {
e.reply('图片文件夹不存在,正在尝试自动克隆仓库...\n请稍后使用 #随机群友更新 手动更新')
try {
e.reply('首次使用,正在克隆图片仓库...')
execSync(`git clone ${repoUrl} ${repoPath}`, { stdio: 'inherit' })
e.reply('图片库初始化完成!已自动拉取最新图片')
} catch (err) {
console.error('克隆失败:', err)
e.reply('初始化失败:' + err.message + '\n请确保:\n1. 已安装git\n2. 网络正常\n3. 仓库地址有效')
}
return true
}

const files = fs.readdirSync(imgFolder).filter(file => /\.(jpg|jpeg|png|gif|webp)$/i.test(file))
if (files.length === 0) {
e.reply('图片文件夹是空的哦~ 请使用 #随机群友更新 拉取图片')
return true
}

const randomIndex = Math.floor(Math.random() * files.length)
const selectedImage = path.join(imgFolder, files[randomIndex])
await e.reply(segment.image(`file://${selectedImage}`))
}

/** 更新图片仓库 */
async updatePicRepo(e) {
try {
if (!fs.existsSync(imgFolder)) {
await this.initRepo(e)
return
}

e.reply('正在拉取最新图片库,请稍候...')

// 进入仓库目录并执行git pull
execSync(`cd ${repoPath} && git pull`, { stdio: 'inherit' })

// 检查更新后的文件数量
const files = fs.readdirSync(imgFolder).filter(file => /\.(jpg|jpeg|png|gif|webp)$/i.test(file))
e.reply(`更新成功!当前图库共有 ${files.length} 张图片`)
} catch (err) {
console.error('更新失败:', err)
e.reply('更新失败:' + err.message)
}
}

/** 初始化仓库 */
async initRepo(e) {
try {
e.reply('首次使用,正在克隆图片仓库...')
execSync(`git clone ${repoUrl} ${repoPath}`, { stdio: 'inherit' })
e.reply('图片库初始化完成!已自动拉取最新图片')
} catch (err) {
console.error('克隆失败:', err)
e.reply('初始化失败:' + err.message + '\n请确保:\n1. 已安装git\n2. 网络正常\n3. 仓库地址有效')
}
}
}

图片来源于网络,侵删