-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdau.js
147 lines (137 loc) · 4.96 KB
/
dau.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import fs from 'fs'
import yaml from 'yaml'
const groupPath = './plugins/example/group_id.yaml'
const userPath = './plugins/example/user_id.yaml'
const calcDays = 30 //计算天数
const days = 7 //显示天数
const sbtxOnly = true //仅统计官方机器人数据
let data = ''
if (fs.existsSync(userPath))
data = fs.readFileSync(userPath, 'utf8');
else{
fs.writeFileSync(userPath, data, 'utf8');
}
let user_list = yaml.parse(data) || {};
if (fs.existsSync(groupPath))
data = fs.readFileSync(groupPath, 'utf8');
else{
fs.writeFileSync(groupPath, data, 'utf8');
}
let group_list = yaml.parse(data) || {};
export class dau extends plugin {
constructor() {
super({
name: "dau",
dsc: "dau",
event: "message",
priority: -1000005,
rule: [
{
reg: "^#?(qqbot)?dau$",
fnc: "dau_read",
},
{
reg: "",
fnc: "dau_write",
log: false
},
{
reg: "^#?清理(过期)?(qqbot)?dau$",
fnc: "dau_cleanup",
},
{
reg: "^#?(查看)?主用户$",
fnc: "mainUserId",
},
]
})
this.task = {
name: '清理过期dau',
fnc: () => this.dau_cleanup(),
cron: `0 3 * * *`
}
}
async dau_cleanup() {
const today = new Date().toLocaleDateString()
for (const date in user_list) {
if (user_list[date]){
if(date != today){
user_list[date] = user_list[date].length || user_list[date]
group_list[date] = group_list[date].length || group_list[date]
}else{
user_list[date] = user_list[date]
group_list[date] = group_list[date]
}
}
}
fs.writeFileSync(userPath, yaml.stringify(user_list), 'utf8')
fs.writeFileSync(groupPath, yaml.stringify(group_list), 'utf8')
try {
await this.reply('清理完成')
}catch(error){}
return
}
async dau_write (e){
if(sbtxOnly && e.adapter != 'QQBot' && e.adapter != 'QQGuild')
return false
else {
const today = new Date().toLocaleDateString(); // 获取今天的日期
if (!user_list[today]) {
user_list[today] = [];
}
if (!group_list[today]) {
group_list[today] = [];
}
let yamlString
if (!user_list[today].includes(e.user_id)) {
user_list[today].push(e.user_id);
yamlString = yaml.stringify(user_list);
fs.writeFileSync(userPath, yamlString, 'utf8');
}
let group_id
group_id = e.guild_id || e.group_id
console.log(group_id)
if (!group_list[today].includes(group_id)) {
group_list[today].push(group_id);
yamlString = yaml.stringify(group_list);
fs.writeFileSync(groupPath, yamlString, 'utf8');
}
return false
}
}
async dau_read (e){
let user_sum = 0;
let group_sum = 0;
let day = 0;
const today = new Date().getTime()
const xDaysAgo = []
const displayDays = []
for(let i = calcDays; i >= 0; i--) {
const dayNow = new Date(today - i * 24 * 60 * 60 * 1000).toLocaleDateString()
xDaysAgo.push(dayNow)
if(i < days)
displayDays.push(dayNow)
}
const dayNow = new Date(today).toLocaleDateString()
let userCounts = {};
for (const date of xDaysAgo) {
if (user_list[date]){
if(displayDays.includes(date))
userCounts[date] = `${user_list[date].length || user_list[date]}人 ${group_list[date].length || group_list[date]}群`;
if(date != dayNow){
user_sum += user_list[date].length || user_list[date]
group_sum += group_list[date].length || group_list[date]
day++
}
}
}
e.reply(`${yaml.stringify(userCounts)}\n${day}日平均:${Math.floor(user_sum/day) || 0}人 ${Math.floor(group_sum/day) || 0}群`)
this.dau_write(e)
}
mainUserId (e){
if(e.mainUserId === undefined)
this.reply(`尚未绑定到主用户或已是主用户。\n\n如需开始绑定流程,请向 已经绑定您的ck等信息的机器人 或使用 已经绑定您的ck等信息的QQ 发送 #绑定用户\n如需绑定uid等信息,请直接发送 #扫码登录 或 #原神绑定123456789`)
else
this.reply(`主用户:${e.mainUserId}`)
}
}