diff --git a/bin/cache/index.js b/bin/cache/index.js new file mode 100644 index 0000000..490415d --- /dev/null +++ b/bin/cache/index.js @@ -0,0 +1,3 @@ +import NodeCache from "node-cache" +export const myCache = new NodeCache({ stdTTL: 3600, checkperiod: 60 }); + diff --git a/bin/channel-core/tools.js b/bin/channel-core/tools.js index df82654..1a917b6 100644 --- a/bin/channel-core/tools.js +++ b/bin/channel-core/tools.js @@ -1,6 +1,13 @@ +import { myCache } from "../cache/index.js"; import { client } from "./base.js"; -export async function getRole(guildId){ - let { data } = await client.roleApi.roles(guildId); - return data +export async function getRole(guildId) { + if(!myCache.get("roleList")){ + let { data } = await client.roleApi.roles(guildId); + myCache.set("roleList",data) + return data + }else{ + console.log("缓存命中") + return myCache.get("roleList") + } } \ No newline at end of file diff --git a/bin/mysql/base.js b/bin/mysql/base.js index a706275..978b72f 100644 --- a/bin/mysql/base.js +++ b/bin/mysql/base.js @@ -20,7 +20,8 @@ export function query(sql){ const c = await connection() c.query(sql,(err,data)=>{ if(err){ - throw new Error("select error") + // throw new Error("sql error",err) + console.log(err) }else { c.destroy() res(data) diff --git a/bin/mysql/index.js b/bin/mysql/index.js index f9ef696..f36dd6f 100644 --- a/bin/mysql/index.js +++ b/bin/mysql/index.js @@ -19,6 +19,3 @@ export async function isTable(name){ } return false } - - -console.log(await isTable("aaa")) \ No newline at end of file diff --git a/package.json b/package.json index d132eea..91c3f5c 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "memory-cache": "^0.2.0", "mitt": "^3.0.0", "mysql": "^2.18.1", + "node-cache": "^5.1.2", "qq-guild-bot": "^2.9.4" }, "scripts": { diff --git a/plugins/activeStatistics/index.js b/plugins/activeStatistics/index.js index b3c8a98..8db291b 100644 --- a/plugins/activeStatistics/index.js +++ b/plugins/activeStatistics/index.js @@ -1,35 +1,84 @@ 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" 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频道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 = [] + const roles = JSON.stringify(member.roles) const avatar = author.avatar - const id = author.id - const bot = author.bot + const userid = author.id + const bot = author.bot ? 1 : 0 const username = author.username - // const roleList = await getRole(guild_id) - // const content = content - // console.log(roleList) const info = { jointime, nickname, avatar, - id, + userid, bot, 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) updateActive(msg) } -onMessageCreate(createMessage) \ No newline at end of file +onMessageCreate(createMessage) + +const res = await activeCount() +console.log(res) \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 12f0da7..e38de33 100644 --- a/yarn.lock +++ b/yarn.lock @@ -20,6 +20,11 @@ bignumber.js@9.0.0: resolved "https://registry.npmmirror.com/bignumber.js/-/bignumber.js-9.0.0.tgz#805880f84a329b5eac6e7cb6f8274b6d82bdf075" 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: version "1.0.8" 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" 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: version "2.0.1" resolved "https://registry.npmmirror.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2"