Miao-Yunzai/plugins/genshin/apps/abbrSet.js

170 lines
4.2 KiB
JavaScript
Raw Normal View History

2023-03-04 14:30:13 +08:00
import plugin from '../../../lib/plugins/plugin.js'
import common from '../../../lib/common/common.js'
2023-03-04 14:30:13 +08:00
import fs from 'node:fs'
import gsCfg from '../model/gsCfg.js'
import YAML from 'yaml'
import lodash from 'lodash'
export class abbrSet extends plugin {
constructor(e) {
2023-03-04 14:30:13 +08:00
super({
name: '别名设置',
dsc: '角色别名设置',
event: 'message',
priority: 600,
rule: [
{
reg: '^#(星铁)?(设置|配置)(.*)(别名|昵称)$',
2023-03-04 14:30:13 +08:00
fnc: 'abbr'
},
{
reg: '^#(星铁)?删除(别名|昵称)(.*)$',
2023-03-04 14:30:13 +08:00
fnc: 'delAbbr'
},
{
reg: '^#(星铁)?(.*)(别名|昵称)$',
2023-03-04 14:30:13 +08:00
fnc: 'abbrList'
}
]
})
this.isSr = false
2023-03-04 14:30:13 +08:00
this.file = './plugins/genshin/config/role.name.yaml'
}
async init() {
2023-03-04 14:30:13 +08:00
if (!fs.existsSync(this.file)) {
fs.writeFileSync(this.file, `神里绫华:
- 龟龟
- 小乌龟`)
}
}
async abbr() {
2023-03-04 14:30:13 +08:00
if (!await this.checkAuth()) return
let role = gsCfg.getRole(this.e.msg, '#|星铁|设置|配置|别名|昵称', this.e.isSr)
2023-03-04 14:30:13 +08:00
if (!role) return false
this.e.role = role
this.isSr = this.e.isSr
2023-03-04 14:30:13 +08:00
this.setContext('setAbbr')
await this.reply(`请发送${role.alias}别名,多个用空格隔开`)
}
async checkAuth() {
2023-03-04 14:30:13 +08:00
if (!this.e.isGroup && !this.e.isMaster) {
await this.reply('禁止私聊设置角色别名')
return false
}
let abbrSetAuth = gsCfg.getConfig('mys', 'set').abbrSetAuth
/** 所有人都允许 */
if (abbrSetAuth === 0) return true
/** 主人默认允许 */
if (this.e.isMaster) return true
/** 管理员 */
if (abbrSetAuth == 1) {
if (!this.e.bot.gml.has(this.e.group_id)) {
2023-03-04 14:30:13 +08:00
return false
}
if (!this.e.bot.gml.get(this.e.group_id).get(this.e.user_id)) {
2023-03-04 14:30:13 +08:00
return false
}
if (!this.e.member.is_admin) {
this.e.reply('暂无权限,只有管理员才能操作')
return false
}
}
return true
}
async setAbbr() {
2023-03-04 14:30:13 +08:00
if (!this.e.msg || this.e.at || this.e.img) {
await this.reply('设置错误:请发送正确内容')
return
}
let { setAbbr = {} } = this.getContext()
this.finish('setAbbr')
let role = setAbbr.role
let setName = this.e.msg.split(' ')
let nameArr = gsCfg.getConfig('role', 'name')
if (!nameArr[role.name]) {
nameArr[role.name] = []
}
let ret = []
for (let name of setName) {
name = name.replace(/#|星铁|设置|配置|别名|昵称/g, '')
2023-03-04 14:30:13 +08:00
if (!name) continue
/** 重复添加 */
if (nameArr[role.name].includes(name) || gsCfg.roleNameToID(name, this.isSr)) {
2023-03-04 14:30:13 +08:00
continue
}
nameArr[role.name].push(name)
ret.push(name)
}
if (ret.length <= 0) {
await this.reply('设置失败:别名错误或已存在')
return
}
this.save(nameArr)
gsCfg[this.isSr ? 'sr_nameID' : 'nameID'] = false
2023-03-04 14:30:13 +08:00
await this.reply(`设置别名成功:${ret.join('、')}`)
}
save(data) {
2023-03-04 14:30:13 +08:00
data = YAML.stringify(data)
fs.writeFileSync(this.file, data)
}
async delAbbr() {
let role = gsCfg.getRole(this.e.msg, '#|星铁|删除|别名|昵称', this.e.isSr)
2023-03-04 14:30:13 +08:00
if (!role) return false
let nameArr = gsCfg.getConfig('role', 'name')
if (!nameArr[role.name]) {
await this.reply('默认别名设置,不能删除!')
return true
}
nameArr[role.name] = nameArr[role.name].filter((v) => {
if (v == role.alias) return false
return v
})
this.save(nameArr)
await this.reply(`设置${role.name}别名成功:${role.alias}`)
}
async abbrList() {
let role = gsCfg.getRole(this.e.msg, '#|星铁|别名|昵称', this.e.isSr)
2023-03-04 14:30:13 +08:00
if (!role) return false
let name = gsCfg.getdefSet('role', this.e.isSr ? 'sr_name' : 'name')[role.roleId]
2023-03-04 14:30:13 +08:00
let nameUser = gsCfg.getConfig('role', 'name')[role.name] ?? []
let list = lodash.uniq([...name, ...nameUser])
let msg = []
for (let i in list) {
let num = Number(i) + 1
2023-07-27 04:19:02 +08:00
msg.push(`${num}.${list[i]}`)
2023-03-04 14:30:13 +08:00
}
msg = await common.makeForwardMsg(this.e, [msg.join("\n")], `${role.name}别名,${list.length}`)
2023-03-04 14:30:13 +08:00
await this.e.reply(msg)
}
}