💩 优化代码

This commit is contained in:
yeyang
2023-04-19 12:11:49 +08:00
parent c8526b6a71
commit 4607b580cf
6 changed files with 49 additions and 82 deletions

View File

@@ -142,7 +142,7 @@ Bot.on('notice.group', async (e) => {
}
// 禁言 (这里仅处理机器人被禁言)
case 'ban': {
const forbiddenTime = common.getsecondformat(e.duration)
const forbiddenTime = common.formatTime(e.duration, 'default')
if (!Config.getGroup(e.group_id).botBeenBanned) return false

View File

@@ -49,13 +49,13 @@ export class NewState extends plugin {
// 头像
portrait: `https://q1.qlogo.cn/g?b=qq&s=0&nk=${(e.bot ?? Bot).uin}`,
// 运行时间
runTime: Formatting(Date.now() / 1000 - (e.bot ?? Bot).stat.start_time),
runTime: common.formatTime(Date.now() / 1000 - (e.bot ?? Bot).stat.start_time, 'dd天hh:mm:ss'),
// 日历
calendar: moment().format('YYYY-MM-DD HH:mm:ss'),
// 昵称
nickname: (e.bot ?? Bot).nickname,
// 系统运行时间
systime: Formatting(os.uptime()),
systime: common.formatTime(os.uptime(), 'dd天hh:mm:ss'),
// 收
recv: (e.bot ?? Bot).stat.recv_msg_cnt,
// 发
@@ -97,21 +97,3 @@ export class NewState extends plugin {
interval = false
}
}
/**
* @description: 格式化时间
* @param {Number} time 秒数
* @param {boolean} yes 是否补零
* @return {String} 天:?时:分:秒
*/
function Formatting (time, repair = true) {
let times = common.getSecond(time, repair)
let { second, minute, hour, day } = times
if (day > 0) {
return day + '天 ' + hour + ':' + minute + ':' + second
} else {
return hour + ':' + minute + ':' + second
}
}

View File

@@ -143,7 +143,7 @@ class Config {
watcher.on('change', path => {
delete this.config[key]
if (typeof Bot == 'undefined') return
logger.mark(`[椰奶修改配置文件][${type}][${name}]`)
logger.mark(`[yenai-plugin][修改配置文件][${type}][${name}]`)
if (this[`change_${name}`]) {
this[`change_${name}`]()
}

View File

@@ -58,23 +58,56 @@ export default new class newCommon {
}
/**
* 将给定的秒数转换为日,时,分和秒,并返回一个对象。
*
* @param {number} time - 要转换的秒
* @param {boolean} repair - 是否修复不足 10 的时,分,秒数
* @returns {{day: number, hour: (string|number), minute: (string|number), second: (string|number)}} - 包含转换结果的对象
* 格式化时间
* @param {number} time - 时间戳,以秒为单位
* @param {string|function} format - 时间格式,'default'为默认格式,'dd'表示天数,'hh'表示小时数,'mm'表示分钟数,'ss'表示秒数,也可以传入自定义函
* @param {boolean} [repair=true] - 是否在小时数、分钟数、秒数小于10时补0
* @returns {(string|object)} 根据format参数返回相应的时间格式字符串或者时间对象{day, hour, minute, second}
*/
getSecond (time, repair) {
formatTime (time, format, repair = true) {
const second = parseInt(time % 60)
const minute = parseInt((time / 60) % 60)
const hour = parseInt((time / (60 * 60)) % 24)
const day = parseInt(time / (24 * 60 * 60))
return {
const timeObj = {
day,
hour: repair && hour < 10 ? `0${hour}` : hour,
minute: repair && minute < 10 ? `0${minute}` : minute,
second: repair && second < 10 ? `0${second}` : second
}
if (format == 'default') {
let result = ''
if (day > 0) {
result += `${day}`
}
if (hour > 0) {
result += `${timeObj.hour}小时`
}
if (minute > 0) {
result += `${timeObj.minute}`
}
if (second > 0) {
result += `${timeObj.second}`
}
return result
}
if (typeof format === 'string') {
format = format
.replaceAll('dd', day)
.replaceAll('hh', timeObj.hour)
.replaceAll('mm', timeObj.minute)
.replaceAll('ss', timeObj.second)
return format
}
if (typeof format === 'function') {
return format(timeObj)
}
return timeObj
}
/**
@@ -104,7 +137,7 @@ export default new class newCommon {
anony
} = {}) {
let forwardMsg = []
if (_.isEmpty(message)) throw Error('[椰奶sendforwardMsg][Error]发送的转发消息不能为空')
if (_.isEmpty(message)) throw Error('[yenai-plugin][sendforwardMsg][Error]发送的转发消息不能为空')
let add = (msg) => forwardMsg.push(
{
message: msg,
@@ -300,28 +333,6 @@ export default new class newCommon {
} else return ck
}
/** 默认秒转换格式 */
getsecondformat (value) {
let time = this.getSecond(value)
let { second, minute, hour, day } = time
// 处理返回消息
let result = ''
if (second != 0) {
result = parseInt(second) + '秒'
}
if (minute > 0) {
result = parseInt(minute) + '分' + result
}
if (hour > 0) {
result = parseInt(hour) + '小时' + result
}
if (day > 0) {
result = parseInt(day) + '天' + result
}
return result
}
/**
* @description: 使用JS将数字从汉字形式转化为阿拉伯形式
* @param {string} s_123

View File

@@ -49,7 +49,7 @@ export default class {
`\n昵称:${item.card || item.nickname}\n`,
`QQ${item.user_id}\n`,
`群身份:${ROLE_MAP[item.role]}\n`,
`禁言剩余时间:${common.getsecondformat(time - (Date.now() / 1000))}\n`,
`禁言剩余时间:${common.formatTime(time - (Date.now() / 1000, 'default'))}\n`,
`禁言到期时间:${new Date(time * 1000).toLocaleString()}`
]
})

View File

@@ -130,7 +130,7 @@ export default new class setu {
over = (this.temp[userId] - present)
}
if (over <= 0) return false
return this.Secondformat(over)
return common.formatTime(over, 'default', false)
}
/**
@@ -222,10 +222,10 @@ export default new class setu {
data.r18 = isopen ? 1 : 0
}
if (Data.writeJSON(`setu${groupID ? '' : '_s'}.json`, data, this.root)) {
logger.mark(`[椰奶R18][${groupID ? '群聊' : '私聊'}]已${isopen ? '开启' : '关闭'}${groupID}的涩涩模式`)
logger.mark(`[yenai-plugin][R18][${groupID ? '群聊' : '私聊'}]已${isopen ? '开启' : '关闭'}${groupID}的涩涩模式`)
return true
} else {
logger.mark(`[椰奶R18][${groupID ? '群聊' : '私聊'}]设置失败`)
logger.mark(`[yenai-plugin][R18][${groupID ? '群聊' : '私聊'}]设置失败`)
return false
}
}
@@ -243,30 +243,4 @@ export default new class setu {
if (!e.isGroup) delete set.recall
return set
}
/**
* @description: 格式化秒
* @param {Number} value 秒
* @return {String}
*/
Secondformat (value) {
let time = common.getSecond(value)
let { second, minute, hour, day } = time
// 处理返回消息
let result = ''
if (second != 0) {
result = parseInt(second) + '秒'
}
if (minute > 0) {
result = parseInt(minute) + '分' + result
}
if (hour > 0) {
result = parseInt(hour) + '小时' + result
}
if (day > 0) {
result = parseInt(day) + '天' + result
}
return result
}
}()