2022-06-02 15:45:35 +08:00
import dayjs from "dayjs"
import { getRole } from "../../bin/channel-core/tools.js"
2022-06-02 00:50:48 +08:00
import { onMessageCreate } from "../../bin/message/index.js"
2022-06-03 21:57:28 +08:00
import { query } from "../../bin/mysql/base.js"
2022-06-07 15:41:07 +08:00
import { addouter } from "../../bin/server/base.js"
2022-06-02 00:50:48 +08:00
2022-06-07 15:41:07 +08:00
async function getlist ( ) {
const list = await query ( "select * from active" )
return list
}
2022-06-02 15:45:35 +08:00
function init ( ) {
2022-06-07 15:41:07 +08:00
addouter ( "/activelist" , async ( req , res ) => {
const list = await getlist ( )
const r = {
code : "0" ,
data : list
}
res . send ( r )
} )
2022-06-02 15:45:35 +08:00
}
2022-06-07 15:41:07 +08:00
2022-06-03 21:57:28 +08:00
async function activeCount ( userid ) {
2022-06-03 22:00:22 +08:00
const sql = ` select count(*) from active where date_sub(curdate(), INTERVAL 30 DAY) <= date(activedate) and userid=' ${ userid } '; `
const sql2 = ` select * from active where date_sub(curdate(), INTERVAL 30 DAY) <= date(activedate) and userid=' ${ userid } '; `
2022-06-02 15:45:35 +08:00
2022-06-03 21:57:28 +08:00
const count = ( await query ( sql ) ) [ 0 ] [ "count(*)" ]
const list = await query ( sql2 )
return {
count ,
list
}
}
/ * *
*
* @ param { String } userid 用户id
* @ returns Boolean true 活跃过 false 未活跃
* /
async function isActive ( userid ) {
const sql = ` select * from active where userid=' ${ userid } ' and activedate=' ${ dayjs ( ) . format ( "YYYY-MM-DD" ) } ' `
console . log ( sql )
const res = await query ( sql )
console . log ( res )
if ( res . length == 0 ) {
return false
} else {
return true
}
}
/ * *
*
* @ param { object } param0 { member当前频道用户信息对象 , author用户频道信息对象 , guild _id频道id , content消息内容 }
*
* @ returns null
* /
async function updateActive ( { member , author , guild _id , content } ) { 5
2022-06-02 16:32:47 +08:00
const jointime = dayjs ( member . joined _at ) . format ( 'YYYY-MM-DD HH:mm:ss' )
2022-06-02 15:45:35 +08:00
// console.log(time.format('YYYY-MM-DD HH:mm:ss'))
const nickname = member . nick
2022-06-03 21:57:28 +08:00
const roles = JSON . stringify ( member . roles )
2022-06-02 15:45:35 +08:00
const avatar = author . avatar
2022-06-03 21:57:28 +08:00
const userid = author . id
const bot = author . bot ? 1 : 0
2022-06-02 15:45:35 +08:00
const username = author . username
2022-06-02 16:32:47 +08:00
const info = {
jointime ,
nickname ,
avatar ,
2022-06-03 21:57:28 +08:00
userid ,
2022-06-02 16:32:47 +08:00
bot ,
username ,
2022-06-03 21:57:28 +08:00
content ,
roles ,
date : dayjs ( ) . format ( "YYYY-MM-DD" )
}
// console.log(info)
const isactive = await isActive ( info . userid )
if ( isactive ) {
console . log ( "今日活跃 跳过" )
return ;
}
const sql = ` insert into active value(null,' ${ info . nickname } ',' ${ info . avatar } ',' ${ info . userid } ', ${ info . bot } ,' ${ info . username } ',' ${ roles } ',' ${ info . jointime } ',' ${ info . date } ') `
try {
await query ( sql )
} catch {
console . log ( "activeStatistics inset error" )
2022-06-02 16:32:47 +08:00
}
2022-06-03 21:57:28 +08:00
// console.log(res)
2022-06-02 15:45:35 +08:00
}
2022-06-02 00:50:48 +08:00
/ * *
* 收到消息处理函数
* @ param { Object } msg 消息对象
* /
2022-06-02 15:45:35 +08:00
function createMessage ( { msg } ) {
2022-06-02 00:50:48 +08:00
console . log ( "收到消息" , msg )
2022-06-02 16:32:47 +08:00
updateActive ( msg )
2022-06-02 00:50:48 +08:00
}
2022-06-03 21:57:28 +08:00
onMessageCreate ( createMessage )
2022-06-07 15:41:07 +08:00
// const res = await activeCount('384023542260794903')
// console.log(res)
init ( )