import dayjs from "dayjs" import { 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 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频道id,content消息内容} * * @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) // const res = await activeCount('384023542260794903') // console.log(res) init()