mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
chore: improve code quality
This commit is contained in:
parent
970f1a98ec
commit
04b2a323a7
@ -2,8 +2,9 @@ import fs from 'fs'
|
|||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
import { getConfigUtil } from '../config'
|
import { getConfigUtil } from '../config'
|
||||||
import { LOG_DIR } from '../globalVars'
|
import { LOG_DIR } from '../globalVars'
|
||||||
|
import { Dict } from 'cosmokit'
|
||||||
|
|
||||||
function truncateString(obj: any, maxLength = 500) {
|
function truncateString(obj: Dict | null, maxLength = 500) {
|
||||||
if (obj !== null && typeof obj === 'object') {
|
if (obj !== null && typeof obj === 'object') {
|
||||||
Object.keys(obj).forEach((key) => {
|
Object.keys(obj).forEach((key) => {
|
||||||
if (typeof obj[key] === 'string') {
|
if (typeof obj[key] === 'string') {
|
||||||
@ -22,7 +23,7 @@ function truncateString(obj: any, maxLength = 500) {
|
|||||||
|
|
||||||
export const logFileName = `llonebot-${new Date().toLocaleString('zh-CN')}.log`.replace(/\//g, '-').replace(/:/g, '-')
|
export const logFileName = `llonebot-${new Date().toLocaleString('zh-CN')}.log`.replace(/\//g, '-').replace(/:/g, '-')
|
||||||
|
|
||||||
export function log(...msg: any[]) {
|
export function log(...msg: unknown[]) {
|
||||||
if (!getConfigUtil().getConfig().log) {
|
if (!getConfigUtil().getConfig().log) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { QQLevel } from '@/ntqqapi/types'
|
import { QQLevel } from '@/ntqqapi/types'
|
||||||
|
import { Dict } from 'cosmokit'
|
||||||
|
|
||||||
export function isNumeric(str: string) {
|
export function isNumeric(str: string) {
|
||||||
return /^\d+$/.test(str)
|
return /^\d+$/.test(str)
|
||||||
@ -16,7 +17,7 @@ export function getBuildVersion(): number {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** 在保证老对象已有的属性不变化的情况下将新对象的属性复制到老对象 */
|
/** 在保证老对象已有的属性不变化的情况下将新对象的属性复制到老对象 */
|
||||||
export function mergeNewProperties(newObj: any, oldObj: any) {
|
export function mergeNewProperties(newObj: Dict, oldObj: Dict) {
|
||||||
Object.keys(newObj).forEach((key) => {
|
Object.keys(newObj).forEach((key) => {
|
||||||
// 如果老对象不存在当前属性,则直接复制
|
// 如果老对象不存在当前属性,则直接复制
|
||||||
if (!oldObj.hasOwnProperty(key)) {
|
if (!oldObj.hasOwnProperty(key)) {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { User, UserDetailInfoListenerArg } from '@/ntqqapi/types'
|
import { User, UserDetailInfoListenerArg } from '@/ntqqapi/types'
|
||||||
|
|
||||||
interface IProfileListener {
|
export interface IProfileListener {
|
||||||
onProfileSimpleChanged(...args: unknown[]): void
|
onProfileSimpleChanged(...args: unknown[]): void
|
||||||
|
|
||||||
onUserDetailInfoChanged(arg: UserDetailInfoListenerArg): void
|
onUserDetailInfoChanged(arg: UserDetailInfoListenerArg): void
|
||||||
@ -13,32 +13,3 @@ interface IProfileListener {
|
|||||||
|
|
||||||
onStrangerRemarkChanged(...args: unknown[]): void
|
onStrangerRemarkChanged(...args: unknown[]): void
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface NodeIKernelProfileListener extends IProfileListener {
|
|
||||||
new(listener: IProfileListener): NodeIKernelProfileListener
|
|
||||||
}
|
|
||||||
|
|
||||||
export class ProfileListener implements IProfileListener {
|
|
||||||
onUserDetailInfoChanged(arg: UserDetailInfoListenerArg): void {
|
|
||||||
|
|
||||||
}
|
|
||||||
onProfileSimpleChanged(...args: unknown[]) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
onProfileDetailInfoChanged(profile: User) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
onStatusUpdate(...args: unknown[]) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
onSelfStatusChanged(...args: unknown[]) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
onStrangerRemarkChanged(...args: unknown[]) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -9,10 +9,10 @@ export interface NodeIKernelProfileLikeService {
|
|||||||
setBuddyProfileLike(...args: unknown[]): { result: number, errMsg: string, succCounts: number }
|
setBuddyProfileLike(...args: unknown[]): { result: number, errMsg: string, succCounts: number }
|
||||||
|
|
||||||
getBuddyProfileLike(req: BuddyProfileLikeReq): Promise<GeneralCallResult & {
|
getBuddyProfileLike(req: BuddyProfileLikeReq): Promise<GeneralCallResult & {
|
||||||
'info': {
|
info: {
|
||||||
'userLikeInfos': Array<any>,
|
userLikeInfos: Array<unknown>,
|
||||||
'friendMaxVotes': number,
|
friendMaxVotes: number,
|
||||||
'start': number
|
start: number
|
||||||
}
|
}
|
||||||
}>
|
}>
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ interface Payload {
|
|||||||
export class MarkMsgAsRead extends BaseAction<Payload, null> {
|
export class MarkMsgAsRead extends BaseAction<Payload, null> {
|
||||||
actionName = ActionName.GoCQHTTP_MarkMsgAsRead
|
actionName = ActionName.GoCQHTTP_MarkMsgAsRead
|
||||||
|
|
||||||
protected async _handle(payload: Payload): Promise<null> {
|
protected async _handle() {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import { ActionName } from '../types'
|
|||||||
export default class GetGuildList extends BaseAction<null, null> {
|
export default class GetGuildList extends BaseAction<null, null> {
|
||||||
actionName = ActionName.GetGuildList
|
actionName = ActionName.GetGuildList
|
||||||
|
|
||||||
protected async _handle(payload: null): Promise<null> {
|
protected async _handle() {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,10 +6,10 @@ interface Payload {
|
|||||||
is_dismiss: boolean
|
is_dismiss: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class SetGroupLeave extends BaseAction<Payload, any> {
|
export default class SetGroupLeave extends BaseAction<Payload, void> {
|
||||||
actionName = ActionName.SetGroupLeave
|
actionName = ActionName.SetGroupLeave
|
||||||
|
|
||||||
protected async _handle(payload: Payload): Promise<any> {
|
protected async _handle(payload: Payload) {
|
||||||
try {
|
try {
|
||||||
await this.ctx.ntGroupApi.quitGroup(payload.group_id.toString())
|
await this.ctx.ntGroupApi.quitGroup(payload.group_id.toString())
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -5,13 +5,15 @@ import { getConfigUtil } from '@/common/config'
|
|||||||
|
|
||||||
export class GetConfigAction extends BaseAction<null, Config> {
|
export class GetConfigAction extends BaseAction<null, Config> {
|
||||||
actionName = ActionName.GetConfig
|
actionName = ActionName.GetConfig
|
||||||
protected async _handle(payload: null): Promise<Config> {
|
|
||||||
|
protected async _handle(): Promise<Config> {
|
||||||
return getConfigUtil().getConfig()
|
return getConfigUtil().getConfig()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SetConfigAction extends BaseAction<Config, void> {
|
export class SetConfigAction extends BaseAction<Config, void> {
|
||||||
actionName = ActionName.SetConfig
|
actionName = ActionName.SetConfig
|
||||||
|
|
||||||
protected async _handle(payload: Config): Promise<void> {
|
protected async _handle(payload: Config): Promise<void> {
|
||||||
getConfigUtil().setConfig(payload)
|
getConfigUtil().setConfig(payload)
|
||||||
}
|
}
|
||||||
|
@ -13,13 +13,14 @@ interface Payload {
|
|||||||
|
|
||||||
export default class GetEvent extends BaseAction<Payload, PostEventType[]> {
|
export default class GetEvent extends BaseAction<Payload, PostEventType[]> {
|
||||||
actionName = ActionName.GetEvent
|
actionName = ActionName.GetEvent
|
||||||
|
|
||||||
protected async _handle(payload: Payload): Promise<PostEventType[]> {
|
protected async _handle(payload: Payload): Promise<PostEventType[]> {
|
||||||
let key = ''
|
let key = ''
|
||||||
if (payload.key) {
|
if (payload.key) {
|
||||||
key = payload.key
|
key = payload.key
|
||||||
}
|
}
|
||||||
let timeout = parseInt(payload.timeout?.toString()) || 0
|
const timeout = parseInt(payload.timeout?.toString()) || 0
|
||||||
let evts = await getHttpEvent(key, timeout)
|
const evts = await getHttpEvent(key, timeout)
|
||||||
return evts
|
return evts
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ interface ReturnType {
|
|||||||
export default class CanSendRecord extends BaseAction<any, ReturnType> {
|
export default class CanSendRecord extends BaseAction<any, ReturnType> {
|
||||||
actionName = ActionName.CanSendRecord
|
actionName = ActionName.CanSendRecord
|
||||||
|
|
||||||
protected async _handle(payload: void): Promise<ReturnType> {
|
protected async _handle() {
|
||||||
return {
|
return {
|
||||||
yes: true,
|
yes: true,
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,10 @@ import { OB11Version } from '../../types'
|
|||||||
import { ActionName } from '../types'
|
import { ActionName } from '../types'
|
||||||
import { version } from '../../../version'
|
import { version } from '../../../version'
|
||||||
|
|
||||||
export default class GetVersionInfo extends BaseAction<any, OB11Version> {
|
export default class GetVersionInfo extends BaseAction<null, OB11Version> {
|
||||||
actionName = ActionName.GetVersionInfo
|
actionName = ActionName.GetVersionInfo
|
||||||
|
|
||||||
protected async _handle(payload: any): Promise<OB11Version> {
|
protected async _handle(): Promise<OB11Version> {
|
||||||
return {
|
return {
|
||||||
app_name: 'LLOneBot',
|
app_name: 'LLOneBot',
|
||||||
protocol_version: 'v11',
|
protocol_version: 'v11',
|
||||||
|
@ -33,8 +33,8 @@ export async function getHttpEvent(userKey: string, timeout = 0) {
|
|||||||
|
|
||||||
// 清除过时的user,5分钟没访问过的user将被删除
|
// 清除过时的user,5分钟没访问过的user将被删除
|
||||||
const now = Date.now()
|
const now = Date.now()
|
||||||
for (let key in httpUser) {
|
for (const key in httpUser) {
|
||||||
let user = httpUser[key]
|
const user = httpUser[key]
|
||||||
if (now - user.lastAccessTime > 1000 * 60 * 5) {
|
if (now - user.lastAccessTime > 1000 * 60 * 5) {
|
||||||
delete httpUser[key]
|
delete httpUser[key]
|
||||||
}
|
}
|
||||||
@ -55,7 +55,7 @@ export async function getHttpEvent(userKey: string, timeout = 0) {
|
|||||||
}
|
}
|
||||||
// 取数据
|
// 取数据
|
||||||
for (let i = 0; i < eventList.length; i++) {
|
for (let i = 0; i < eventList.length; i++) {
|
||||||
let evt = eventList[i]
|
const evt = eventList[i]
|
||||||
if (evt.seq > user.userSeq) {
|
if (evt.seq > user.userSeq) {
|
||||||
toRetEvent.push(evt.event)
|
toRetEvent.push(evt.event)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user