add 活跃统计

This commit is contained in:
theluyuan 2022-06-03 21:57:28 +08:00
parent fb2d9ae159
commit dc8fea04b6
7 changed files with 91 additions and 18 deletions

3
bin/cache/index.js vendored Normal file
View File

@ -0,0 +1,3 @@
import NodeCache from "node-cache"
export const myCache = new NodeCache({ stdTTL: 3600, checkperiod: 60 });

View File

@ -1,6 +1,13 @@
import { myCache } from "../cache/index.js";
import { client } from "./base.js"; import { client } from "./base.js";
export async function getRole(guildId){ export async function getRole(guildId) {
let { data } = await client.roleApi.roles(guildId); if(!myCache.get("roleList")){
return data let { data } = await client.roleApi.roles(guildId);
myCache.set("roleList",data)
return data
}else{
console.log("缓存命中")
return myCache.get("roleList")
}
} }

View File

@ -20,7 +20,8 @@ export function query(sql){
const c = await connection() const c = await connection()
c.query(sql,(err,data)=>{ c.query(sql,(err,data)=>{
if(err){ if(err){
throw new Error("select error") // throw new Error("sql error",err)
console.log(err)
}else { }else {
c.destroy() c.destroy()
res(data) res(data)

View File

@ -19,6 +19,3 @@ export async function isTable(name){
} }
return false return false
} }
console.log(await isTable("aaa"))

View File

@ -8,6 +8,7 @@
"memory-cache": "^0.2.0", "memory-cache": "^0.2.0",
"mitt": "^3.0.0", "mitt": "^3.0.0",
"mysql": "^2.18.1", "mysql": "^2.18.1",
"node-cache": "^5.1.2",
"qq-guild-bot": "^2.9.4" "qq-guild-bot": "^2.9.4"
}, },
"scripts": { "scripts": {

View File

@ -1,35 +1,84 @@
import dayjs from "dayjs" import dayjs from "dayjs"
import { getRole } from "../../bin/channel-core/tools.js" import { getRole } from "../../bin/channel-core/tools.js"
import { onMessageCreate } from "../../bin/message/index.js" import { onMessageCreate } from "../../bin/message/index.js"
import { query } from "../../bin/mysql/base.js"
function init(){ function init(){
} }
async function activeCount(userid){
const sql = `select count(*) from active where date_sub(curdate(), INTERVAL 30 DAY) <= date(activedate);`
const sql2 = `select * from active where date_sub(curdate(), INTERVAL 30 DAY) <= date(activedate);`
async function updateActive({member,author,guild_id,content}){ 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') const jointime = dayjs(member.joined_at).format('YYYY-MM-DD HH:mm:ss')
// console.log(time.format('YYYY-MM-DD HH:mm:ss')) // console.log(time.format('YYYY-MM-DD HH:mm:ss'))
const nickname = member.nick const nickname = member.nick
const roles = [] const roles = JSON.stringify(member.roles)
const avatar = author.avatar const avatar = author.avatar
const id = author.id const userid = author.id
const bot = author.bot const bot = author.bot ? 1 : 0
const username = author.username const username = author.username
// const roleList = await getRole(guild_id)
// const content = content
// console.log(roleList)
const info = { const info = {
jointime, jointime,
nickname, nickname,
avatar, avatar,
id, userid,
bot, bot,
username, username,
content content,
roles,
date: dayjs().format("YYYY-MM-DD")
} }
console.log(info)
// 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)
} }
@ -41,4 +90,7 @@ function createMessage({msg}){
console.log("收到消息",msg) console.log("收到消息",msg)
updateActive(msg) updateActive(msg)
} }
onMessageCreate(createMessage) onMessageCreate(createMessage)
const res = await activeCount()
console.log(res)

View File

@ -20,6 +20,11 @@ bignumber.js@9.0.0:
resolved "https://registry.npmmirror.com/bignumber.js/-/bignumber.js-9.0.0.tgz#805880f84a329b5eac6e7cb6f8274b6d82bdf075" resolved "https://registry.npmmirror.com/bignumber.js/-/bignumber.js-9.0.0.tgz#805880f84a329b5eac6e7cb6f8274b6d82bdf075"
integrity sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A== integrity sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A==
clone@2.x:
version "2.1.2"
resolved "https://registry.npmmirror.com/clone/-/clone-2.1.2.tgz#1b7f4b9f591f1e8f83670401600345a02887435f"
integrity sha512-3Pe/CF1Nn94hyhIYpjtiLhdCoEoz0DqQ+988E9gmeEdQZlojxnOb74wctFyuwWQHzqyf9X7C7MG8juUpqBJT8w==
combined-stream@^1.0.8: combined-stream@^1.0.8:
version "1.0.8" version "1.0.8"
resolved "https://registry.npmmirror.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" resolved "https://registry.npmmirror.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
@ -103,6 +108,13 @@ mysql@^2.18.1:
safe-buffer "5.1.2" safe-buffer "5.1.2"
sqlstring "2.3.1" sqlstring "2.3.1"
node-cache@^5.1.2:
version "5.1.2"
resolved "https://registry.npmmirror.com/node-cache/-/node-cache-5.1.2.tgz#f264dc2ccad0a780e76253a694e9fd0ed19c398d"
integrity sha512-t1QzWwnk4sjLWaQAS8CHgOJ+RAfmHpxFWmc36IWTiWHQfs0w5JDMBS1b1ZxQteo0vVVuWJvIUKHDkkeK7vIGCg==
dependencies:
clone "2.x"
process-nextick-args@~2.0.0: process-nextick-args@~2.0.0:
version "2.0.1" version "2.0.1"
resolved "https://registry.npmmirror.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2" resolved "https://registry.npmmirror.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2"