mirror of
https://github.com/Jerryplusy/crystelf-plugin.git
synced 2025-12-05 15:41:56 +00:00
feat:添加自定义欢迎功能
This commit is contained in:
parent
748c5f76b6
commit
7775d66008
0
apps/help.js
Normal file
0
apps/help.js
Normal file
87
apps/welcome-set.js
Normal file
87
apps/welcome-set.js
Normal file
@ -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<boolean|*>}
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
31
apps/welcome.js
Normal file
31
apps/welcome.js
Normal file
@ -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<void>}
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
1
config/newcomer.json
Normal file
1
config/newcomer.json
Normal file
@ -0,0 +1 @@
|
||||
{}
|
||||
35
lib/yunzai/utils.js
Normal file
35
lib/yunzai/utils.js
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user