2023-03-04 14:30:13 +08:00
|
|
|
import plugin from '../../../lib/plugins/plugin.js'
|
2023-07-26 05:23:14 +08:00
|
|
|
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 {
|
2023-07-26 05:08:48 +08:00
|
|
|
constructor(e) {
|
2023-03-04 14:30:13 +08:00
|
|
|
super({
|
|
|
|
name: '别名设置',
|
|
|
|
dsc: '角色别名设置',
|
|
|
|
event: 'message',
|
|
|
|
priority: 600,
|
|
|
|
rule: [
|
|
|
|
{
|
2023-07-26 05:08:48 +08:00
|
|
|
reg: '^#(星铁)?(设置|配置)(.*)(别名|昵称)$',
|
2023-03-04 14:30:13 +08:00
|
|
|
fnc: 'abbr'
|
|
|
|
},
|
|
|
|
{
|
2023-07-26 05:08:48 +08:00
|
|
|
reg: '^#(星铁)?删除(别名|昵称)(.*)$',
|
2023-03-04 14:30:13 +08:00
|
|
|
fnc: 'delAbbr'
|
|
|
|
},
|
|
|
|
{
|
2023-07-26 05:08:48 +08:00
|
|
|
reg: '^#(星铁)?(.*)(别名|昵称)$',
|
2023-03-04 14:30:13 +08:00
|
|
|
fnc: 'abbrList'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2023-07-26 05:08:48 +08:00
|
|
|
this.isSr = false
|
2023-03-04 14:30:13 +08:00
|
|
|
this.file = './plugins/genshin/config/role.name.yaml'
|
|
|
|
}
|
|
|
|
|
2023-07-26 05:08:48 +08:00
|
|
|
async init() {
|
2023-03-04 14:30:13 +08:00
|
|
|
if (!fs.existsSync(this.file)) {
|
|
|
|
fs.writeFileSync(this.file, `神里绫华:
|
|
|
|
- 龟龟
|
|
|
|
- 小乌龟`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 05:08:48 +08:00
|
|
|
async abbr() {
|
2023-03-04 14:30:13 +08:00
|
|
|
if (!await this.checkAuth()) return
|
2023-07-26 05:08:48 +08:00
|
|
|
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
|
2023-07-26 05:08:48 +08:00
|
|
|
this.isSr = this.e.isSr
|
2023-03-04 14:30:13 +08:00
|
|
|
this.setContext('setAbbr')
|
|
|
|
|
|
|
|
await this.reply(`请发送${role.alias}别名,多个用空格隔开`)
|
|
|
|
}
|
|
|
|
|
2023-07-26 05:08:48 +08:00
|
|
|
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) {
|
2023-04-21 13:51:59 +08:00
|
|
|
if (!this.e.bot.gml.has(this.e.group_id)) {
|
2023-03-04 14:30:13 +08:00
|
|
|
return false
|
|
|
|
}
|
2023-04-21 13:51:59 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-26 05:08:48 +08:00
|
|
|
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) {
|
2023-07-26 05:08:48 +08:00
|
|
|
name = name.replace(/#|星铁|设置|配置|别名|昵称/g, '')
|
2023-03-04 14:30:13 +08:00
|
|
|
if (!name) continue
|
|
|
|
/** 重复添加 */
|
2023-07-26 05:08:48 +08:00
|
|
|
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)
|
|
|
|
|
2023-07-26 05:08:48 +08:00
|
|
|
gsCfg[this.isSr ? 'sr_nameID' : 'nameID'] = false
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
|
|
await this.reply(`设置别名成功:${ret.join('、')}`)
|
|
|
|
}
|
|
|
|
|
2023-07-26 05:08:48 +08:00
|
|
|
save(data) {
|
2023-03-04 14:30:13 +08:00
|
|
|
data = YAML.stringify(data)
|
|
|
|
fs.writeFileSync(this.file, data)
|
|
|
|
}
|
|
|
|
|
2023-07-26 05:08:48 +08:00
|
|
|
async delAbbr() {
|
2023-07-26 05:23:14 +08:00
|
|
|
let role = gsCfg.getRole(this.e.msg, '#|删除|别名|昵称')
|
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}`)
|
|
|
|
}
|
|
|
|
|
2023-07-26 05:08:48 +08:00
|
|
|
async abbrList() {
|
|
|
|
let role = gsCfg.getRole(this.e.msg, '#|星铁|别名|昵称', this.e.isSr)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
|
|
if (!role) return false
|
|
|
|
|
2023-07-26 05:08:48 +08:00
|
|
|
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
|
|
|
|
msg.push(`${num}.${list[i]}\n`)
|
|
|
|
}
|
|
|
|
|
|
|
|
let title = `${role.name}别名,${list.length}个`
|
|
|
|
|
2023-07-26 05:23:14 +08:00
|
|
|
msg = await common.makeForwardMsg(this.e, msg, title)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
|
|
await this.e.reply(msg)
|
|
|
|
}
|
|
|
|
|
2023-07-26 05:08:48 +08:00
|
|
|
async makeForwardMsg(qq, title, msg) {
|
2023-04-21 13:51:59 +08:00
|
|
|
let nickname = this.e.bot.nickname
|
2023-03-04 14:30:13 +08:00
|
|
|
if (this.e.isGroup) {
|
2023-04-21 13:51:59 +08:00
|
|
|
let info = await this.e.bot.getGroupMemberInfo(this.e.group_id, qq)
|
2023-03-04 14:30:13 +08:00
|
|
|
nickname = info.card ?? info.nickname
|
|
|
|
}
|
|
|
|
let userInfo = {
|
2023-04-21 13:51:59 +08:00
|
|
|
user_id: this.e.bot.uin,
|
2023-03-04 14:30:13 +08:00
|
|
|
nickname
|
|
|
|
}
|
|
|
|
|
|
|
|
let forwardMsg = [
|
|
|
|
{
|
|
|
|
...userInfo,
|
|
|
|
message: title
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...userInfo,
|
|
|
|
message: msg
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
/** 制作转发内容 */
|
|
|
|
if (this.e.isGroup) {
|
|
|
|
forwardMsg = await this.e.group.makeForwardMsg(forwardMsg)
|
|
|
|
} else {
|
|
|
|
forwardMsg = await this.e.friend.makeForwardMsg(forwardMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 处理描述 */
|
|
|
|
forwardMsg.data = forwardMsg.data
|
|
|
|
.replace(/\n/g, '')
|
|
|
|
.replace(/<title color="#777777" size="26">(.+?)<\/title>/g, '___')
|
|
|
|
.replace(/___+/, `<title color="#777777" size="26">${title}</title>`)
|
|
|
|
|
|
|
|
return forwardMsg
|
|
|
|
}
|
|
|
|
}
|