import path = require ( 'path' ) ; import fs = require ( 'fs-extra' ) ; import JSON5 from 'json5' ; import YAML = require ( 'yaml' ) ; import { CQWebSocket , CQWebSocketOption , APIResponse } from 'cq-websocket' ; import { bot } from '../index' ; import { printTime } from './timeHelp' ; import { IS_DEBUG } from '@/config' ; import { CQMessage , CQLog , MemberInfo } from '@/models' ; import { globalCache } from '@/db' ; author CaoMeiYouRen
 *  date export param { string } dirname returns { CQWebSocketOption } export function getCQWebSocketOption ( dirname ? ) { let setting = { } as CQWebSocketOption ; if ( dirname ) { if ( fs . existsSync ( path . join ( dirname , './setting.yaml' ) ) ) { setting = YAML . parse ( fs . readFileSync ( path . join ( dirname , './setting.yaml' ) , 'utf8' ) ) ; } else if ( fs . existsSync ( path . join ( dirname , './setting.yml' ) ) ) { setting = YAML . parse ( fs . readFileSync ( path . join ( dirname , './setting.yml' ) , 'utf8' ) ) ; } else if ( fs . existsSync ( path . join ( dirname , './setting.jsonc' ) ) ) { setting = JSON5 . parse ( fs . readFileSync ( path . join ( dirname , './setting.jsonc' ) , 'utf8' ) ) ; } else if ( fs . existsSync ( path . join ( dirname , './setting.json' ) ) ) { setting = JSON5 . parse ( fs . readFileSync ( path . join ( dirname , './setting.json' ) , 'utf8' ) ) ; } } else if ( fs . existsSync ( 'setting.yaml' ) ) { setting = YAML . parse ( fs . readFileSync ( 'setting.yaml' , 'utf8' ) ) ; } else if ( fs . existsSync ( 'setting.yml' ) ) { setting = YAML . parse ( fs . readFileSync ( 'setting.yml' , 'utf8' ) ) ; } else if ( fs . existsSync ( 'setting.jsonc' ) ) { setting = JSON5 . parse ( fs . readFileSync ( 'setting.jsonc' , 'utf8' ) ) ; } else if ( fs . existsSync ( 'setting.json' ) ) { setting = JSON5 . parse ( fs . readFileSync ( 'setting.json' , 'utf8' ) ) ; } return Object . assign ( { accessToken : '' , enableAPI : true , enableEvent : true , protocol : 'ws:' , host : '127.0.0.1' , port : 6700 , baseUrl : '' , qq : - 1 , reconnection : true , reconnectionAttempts : 1000 , reconnectionDelay : 1000 , fragmentOutgoingMessages : false , fragmentationThreshold : 16000 , tlsOptions : { } , requestOptions : { timeout : 10000 , } , } , setting ) ; } author CaoMeiYouRen
 *  date param { number } user_id param { ( string | CQMessage | Array < CQMessage > ) } message returns { Promise < number > } memberof export async function sendPrivateMsg ( user_id , message ) { if ( IS_DEBUG ) { printTime ( `[发送私聊消息] QQID:${ user_id } msg:${ JSON . stringify ( message ) }` , CQLog . LOG_DEBUG ) ; return 0 ; } try { const result = await bot ( 'send_private_msg' , { user_id , message , } ) ; printTime ( `[发送私聊消息] QQID:${ user_id } msg:${ JSON . stringify ( message ) } 执行结果:${ JSON . stringify ( result ) }` , CQLog . LOG_INFO_SEND ) ; if ( result [ 'status' ] === 'ok' ) { return result [ 'data' ] [ 'message_id' ] ; } if ( result [ 'retcode' ] > 0 ) { return - result [ 'retcode' ] - 1000 ; } return result [ 'retcode' ] ; } catch ( error ) { printTime ( '[cq-robot]请求 send_private_msg 发生错误' , CQLog . LOG_ERROR ) ; console . error ( error ) ; return - 1000 ; } } author CaoMeiYouRen
 *  date param { number } group_id param { ( string | CQMessage | Array < CQMessage > ) } message returns export async function sendGroupMsg ( group_id , message ) { if ( IS_DEBUG ) { printTime ( `[发送群消息] 群号:${ group_id } msg:${ JSON . stringify ( message ) }` , CQLog . LOG_DEBUG ) ; return 0 ; } try { const result = await bot ( 'send_group_msg' , { group_id , message , } ) ; printTime ( `[发送群消息] 群号:${ group_id } msg:${ JSON . stringify ( message ) } 执行结果:${ JSON . stringify ( result ) }` , CQLog . LOG_INFO_SEND ) ; if ( result [ 'status' ] === 'ok' ) { return result [ 'data' ] [ 'message_id' ] ; } if ( result [ 'retcode' ] > 0 ) { return - result [ 'retcode' ] - 1000 ; } return result [ 'retcode' ] ; } catch ( error ) { printTime ( '[cq-robot]请求 send_group_msg 发生错误' , CQLog . LOG_ERROR ) ; console . error ( error ) ; return - 1000 ; } } author CaoMeiYouRen
 *  date export param { ( string | CQMessage | CQMessage [ ] ) } message param { number } user_id param { number } group_id export async function sendMsg ( message , user_id , group_id ? ) { if ( group_id ) { return sendGroupMsg ( group_id , message ) ; } return sendPrivateMsg ( user_id , message ) ; } author CaoMeiYouRen
 *  date export param { number } group_id param { number } user_id export async function isGroupAdmin ( group_id , user_id ) { const key = `is-group-admin-${ group_id }-uid-${ user_id }` ; let role = await globalCache . get ( key ) || '' ; if ( ! role ) { const info = await getGroupMemberInfo ( group_id , user_id ) ; role = info . role ; await globalCache . set ( key , role , 3600 * 24 * 7 ) ; } return [ 'owner' , 'admin' ] . includes ( role ) ; } author CaoMeiYouRen
*  date param { number } group_id param { number } user_id param { boolean } no_cache returns { Promise < MemberInfo > } memberof export async function getGroupMemberInfo ( group_id , user_id , no_cache = false ) { if ( IS_DEBUG ) { const testMemberInfo = { group_id , user_id , nickname : '测试昵称' , card : '测试名片' , sex : 'male' , age : 0 , area : '中国' , join_time : Date . now ( ) - 24 * 60 * 60 * 1000 , last_sent_time : Date . now ( ) - 60 * 1000 , level : '萌新' , role : 'member' , unfriendly : false , title : '测试专属头衔' , title_expire_time : - 1 , card_changeable : true , } ; printTime ( `[取群成员信息] 本函数请在酷Q中测试 群号:${ group_id } QQID:${ user_id } 不使用缓存:${ no_cache } 返回:${ JSON . stringify ( testMemberInfo ) }` ) ; return testMemberInfo ; } try { const result = await bot ( 'get_group_member_info' , { group_id , user_id , no_cache , } ) ; if ( result [ 'status' ] === 'ok' ) { return result [ 'data' ] ; } return new MemberInfo ( ) ; } catch ( error ) { printTime ( '[cq-robot]请求 get_group_member_info 发生错误' , CQLog . LOG_ERROR ) ; console . error ( error ) ; return new MemberInfo ( ) ; } }