qqbot/plugins/activeStatistics/index.js
2022-06-07 16:24:16 +08:00

141 lines
3.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import dayjs from "dayjs"
import { client } from "../../bin/channel-core/base.js"
import { getGuild, getRole } from "../../bin/channel-core/tools.js"
import { onMessageCreate } from "../../bin/message/index.js"
import { query } from "../../bin/mysql/base.js"
import { addouter } from "../../bin/server/base.js"
async function getActiveList(){
const sql = `select userid,count(*) from active where date_sub(curdate(), INTERVAL 30 DAY) <= date(activedate) GROUP BY userid`
const list = await query(sql)
console.log(list)
let user = []
for(let i of list){
if(i["count(*)"] > 2){
user.push(i.userid)
}
}
return user
}
async function setrole(){
const gid = await getGuild("web前端")
const list = await getActiveList()
console.log(list,24)
for(let i of list){
let { data } = await client.memberApi.memberAddRole(gid, "10868905", i);
}
console.log(list)
}
async function getlist(){
const list = await query("select * from active")
return list
}
function init(){
addouter("/activelist",async (req,res)=>{
const list = await getlist()
const r = {
code:"0",
data: list
}
res.send(r)
})
}
async function activeCount(userid){
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}';`
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频道idcontent消息内容}
*
* @returns null
*/
async function updateActive({member,author,guild_id,content}){5
const jointime = dayjs(member.joined_at).format('YYYY-MM-DD HH:mm:ss')
// console.log(time.format('YYYY-MM-DD HH:mm:ss'))
const nickname = member.nick
const roles = JSON.stringify(member.roles)
const avatar = author.avatar
const userid = author.id
const bot = author.bot ? 1 : 0
const username = author.username
const info = {
jointime,
nickname,
avatar,
userid,
bot,
username,
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")
}
// console.log(res)
}
/**
* 收到消息处理函数
* @param {Object} msg 消息对象
*/
function createMessage({msg}){
console.log("收到消息",msg)
updateActive(msg)
}
onMessageCreate(createMessage)
// getRole()
// const res = await activeCount('384023542260794903')
// console.log(res)
setrole()
init()