From 7775d6600817d3c23516e3c7acb67ea550c8a341 Mon Sep 17 00:00:00 2001 From: Jerryplusy Date: Sat, 4 Oct 2025 15:37:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E6=AC=A2=E8=BF=8E=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/help.js | 0 apps/welcome-set.js | 87 ++++++++++++++++++++++++++++++++++++++++++++ apps/welcome.js | 31 ++++++++++++++++ config/newcomer.json | 1 + lib/yunzai/utils.js | 35 ++++++++++++++++++ 5 files changed, 154 insertions(+) create mode 100644 apps/help.js create mode 100644 apps/welcome-set.js create mode 100644 apps/welcome.js create mode 100644 config/newcomer.json create mode 100644 lib/yunzai/utils.js diff --git a/apps/help.js b/apps/help.js new file mode 100644 index 0000000..e69de29 diff --git a/apps/welcome-set.js b/apps/welcome-set.js new file mode 100644 index 0000000..8ca8702 --- /dev/null +++ b/apps/welcome-set.js @@ -0,0 +1,87 @@ +import configControl from '../lib/config/configControl.js'; +import YunzaiUtils from '../lib/yunzai/utils.js'; + +export class welcomeNewcomerSetting extends plugin { + constructor() { + super({ + name: 'welcome-newcomer-set', + dsc: '新人入群欢迎设置', + event: 'message.group', + priority: -1000, + rule: [ + { + reg: '^#设置欢迎(文案|图片)([\\s\\S]*)$', + fnc: 'setWelcome', + }, + { + reg: '^#查看欢迎$', + fnc: 'viewWelcome', + }, + { + reg: '^#清除欢迎$', + fnc: 'clearWelcome', + }, + ], + }); + } + + /** + * 设置欢迎语 + * @param e + * @returns {Promise} + */ + async setWelcome(e) { + if (!(e.isMaster || e.group?.is_owner || e.group?.is_admin)) { + return e.reply('只有群主或管理员可以设置欢迎消息哦..', true); + } + const groupId = e.group_id; + const type = e.msg.includes('文案') ? 'text' : 'image'; + const allCfg = configControl.get('newcomer') || {}; + const cfg = allCfg[groupId] || {}; + + if (type === 'text') { + const text = e.msg.replace(/^#设置欢迎文案/, '').trim(); + if (!text) return e.reply('请在命令后输入欢迎文案..', true); + cfg.text = text; + allCfg[groupId] = cfg; + await configControl.set('newcomer', allCfg); + return e.reply(`欢迎文案设置成功:\n${text}..`, true); + } + + if (type === 'image') { + const imgs = await YunzaiUtils.getImages(e, 1); + if (!imgs?.length) return e.reply('未检测到图片..', true); + cfg.image = imgs[0]; + allCfg[groupId] = cfg; + await configControl.set('newcomer', allCfg); + return e.reply('欢迎图片设置成功!', true); + } + } + + async viewWelcome(e) { + const groupId = e.group_id; + const allCfg = configControl.get('newcomer') || {}; + const cfg = allCfg[groupId]; + + if (!cfg) return e.reply('该群尚未设置欢迎内容..', true); + + const msg = [`当前欢迎: `]; + if (cfg.text) msg.push(cfg.text); + if (cfg.image) msg.push(segment.image(cfg.image)); + await e.reply(msg); + } + + async clearWelcome(e) { + if (!(e.isMaster || e.group?.is_owner || e.group?.is_admin)) { + return e.reply('只有群主或管理员可以清除设置哦..', true); + } + + const groupId = e.group_id; + const allCfg = configControl.get('newcomer') || {}; + + if (!allCfg[groupId]) return e.reply('该群没有设置欢迎消息..', true); + delete allCfg[groupId]; + await configControl.set('newcomer', allCfg); + return e.reply(`已清除群${groupId}的欢迎设置..`, true); + } +} diff --git a/apps/welcome.js b/apps/welcome.js new file mode 100644 index 0000000..d07d329 --- /dev/null +++ b/apps/welcome.js @@ -0,0 +1,31 @@ +import configControl from '../lib/config/configControl.js'; + +export class welcomeNewcomer extends plugin { + constructor() { + super({ + name: 'welcome-newcomer', + dsc: '新人入群欢迎', + event: 'notice.group.increase', + priority: -1000, + }); + } + + /** + * 新人入群欢迎 + * @returns {Promise} + */ + async accept(e) { + if (e.user_id === e.self_id) return; + const groupId = e.group_id; + const cdKey = `Yz:newcomers:${groupId}`; + if (await redis.get(cdKey)) return; + await redis.set(cdKey, '1', { EX: 30 }); + const allCfg = configControl.get('newcomer') || {}; + const cfg = allCfg[groupId] || {}; + const msgList = [segment.at(e.user_id)]; + if (cfg.text) msgList.push(cfg.text); + if (cfg.image) msgList.push(segment.image(cfg.image)); + if (!cfg.text && !cfg.image) msgList.push('欢迎新人~!'); + await e.reply(msgList); + } +} diff --git a/config/newcomer.json b/config/newcomer.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/config/newcomer.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/lib/yunzai/utils.js b/lib/yunzai/utils.js new file mode 100644 index 0000000..9f9e850 --- /dev/null +++ b/lib/yunzai/utils.js @@ -0,0 +1,35 @@ +export default class YunzaiUtils { + /** + * 获取消息中的图片 + * @param e + * @param limit 限制 + * @returns {Promise<*[]>} + */ + static async getImages(e, limit = 1) { + let imgUrls = []; + const me = `https://q1.qlogo.cn/g?b=qq&s=640&nk=${e.user_id}`; + //消息中的图片 + if (e.source || e.reply_id) { + let reply; + if (e.getReply) reply = await e.getReply(); + else { + const history = await (e.isGroup ? e.group : e.friend).getChatHistory( + e.isGroup ? e.source.seq : e.source.time, + 1 + ); + reply = history?.pop()?.message; + } + if (reply) imgUrls = reply.filter((m) => m.type === 'image').map((m) => m.url); + } + + //当前消息中的图片 + if (!imgUrls.length && e.message) { + imgUrls = e.message.filter((m) => m.type === 'image').map((m) => m.url); + } + + //没图时用头像 + if (!imgUrls.length) imgUrls = [me]; + imgUrls = imgUrls.slice(0, limit); + return imgUrls; + } +}