-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosu.js
198 lines (175 loc) · 6.56 KB
/
osu.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import plugin from '../../lib/plugins/plugin.js'
import _ from 'lodash'
import { Client } from 'osu-web.js'
import fs from "fs"
//自行填写id,secret
const id = "app_id"
const secret = "app_secret"
if (!fs.existsSync("./data/osuc/token.txt")) {
fs.mkdirSync("./data/osuc")
fs.writeFileSync("./data/osuc/token.txt","")
}
if (!fs.existsSync("./data/osuc/users.json")) {
fs.writeFileSync("./data/osuc/users.json","{}")
}
const regUser = /^(\!|!)ou(.*)/
const regBind = /^(\!|!)ob(.*)/
const regPPTest = /^(\!|!)ap(.*)/
async function getFormattedTime(sec) {
const hours = Math.floor(sec / 3600)
const minutes = Math.floor((sec % 3600) / 60)
let durationText = ''
if (hours >= 1) {
durationText += `${hours}h`
}
if (minutes >= 1) {
durationText += `${minutes}m`
}
return durationText
}
async function getToken(){
const headers = {
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
}
const body = `client_id=${id}&client_secret=${secret}&grant_type=client_credentials&scope=public`
let token = await fetch("https://osu.ppy.sh/oauth/token",{
method: "POST",
headers,
body: body,
})
token = await token.json()
fs.writeFileSync("./data/osuc/token.txt",token.access_token)
return token.access_token
}
async function fetchUser(c,e) {
let client = new Client(fs.readFileSync("./data/osuc/token.txt"))
try {
return await client.users.getUser(c, {
urlParams: {
mode: 'osu'
}
})
} catch (err) {
console.log(err)
if (err.response1.status == 404) { return e.reply("查无此人") }
if (err.response1.status != 401) { return e.reply("未知错误: " + err.response1.status) }
e.reply("token貌似过期,重新获取...")
client.setAccessToken(await getToken())
return await client.users.getUser(c, {
urlParams: {
mode: 'osu'
}
})
}
}
export class osu extends plugin {
constructor () {
super({
name: 'osu',
dsc: 'none',
event: 'message',
priority: -1,
rule: [
{
reg: regUser,
fnc: 'getUser'
},
{
reg: regBind,
fnc: 'bindUser'
},
{
reg: regPPTest,
fnc: 'PPpTest'
}
]
})
}
async bindUser(e){
const users = JSON.parse(fs.readFileSync("./data/osuc/users.json"))
let c = await (await e.msg.replace(/^(\!|!)ob/,"")).replace(/ /g,"")
if(c == ""){ return true }
users[e.sender?.user_id] = c
fs.writeFileSync("./data/osuc/users.json",JSON.stringify(users))
e.reply(`${e.sender?.user_id} => ${c}`,true)
return true
}
async getUser(e){
const users = JSON.parse(fs.readFileSync("./data/osuc/users.json"))
let c = await (await e.msg.replace(/^(\!|!)ou/, "")).replace(/ /g, "")
if (c == "") {
c = users[e.sender?.user_id]
} else {
c = `@` + c
}
if(c == undefined){ return e.reply("你未绑定,输入 !ob [ID] 进行绑定", true)}
c = await fetchUser(c,e)
let message = [`[${c.country_code}]${c.username}`]
logger.info(c)
if (c.is_supporter) { message[0] += "[❤]" }
c = c.statistics
logger.info(JSON.stringify(c))
message[0] += ` - Lv.${c.level.current}\n`
message.push(`全球#${c.global_rank} 地区#${c.country_rank}\n---\n`)
message.push(`表现分: ${c.pp.toFixed(2)}pp | 准度: ${c.hit_accuracy.toFixed(2)}%\n`)
message.push(`最大连击: ${c.maximum_combo} | 命中次数: ${(c.total_hits / 1000).toFixed(2)}k\n`)
message.push(`总分(R/T): ${(c.ranked_score / 1000000).toFixed(2)}M / ${(c.total_score / 1000000).toFixed(2)}M\n`)
message.push(`游玩次数: ${c.play_count} | 游玩时间: ${await getFormattedTime(c.play_time)}\n`)
e.reply(message)
return true
}
async PPpTest(e){
const users = JSON.parse(fs.readFileSync("./data/osuc/users.json"))
let client = new Client(fs.readFileSync("./data/osuc/token.txt"))
let pp = await (await e.msg.replace(/^(\!|!)ap/, "")).replace(/ /g, "")
if (pp == "") { return e.reply("参数不足") }
let c = users[e.sender?.user_id]
if(c == undefined){ return e.reply("你未绑定,输入 !ob [ID] 进行绑定", true)}
try {
c = await client.users.getUserScores(c, 'best', {
query: {
mode: 'osu',
limit: 114514
}
})
} catch (err) {
console.log(err)
if (err.response1.status == 404) { return e.reply("查无此人") }
if (err.response1.status != 401) { return e.reply("未知错误: " + err.response1.status) }
e.reply("token貌似过期,重新获取...")
client.setAccessToken(await getToken())
c = await client.users.getUserScores(c, 'best', {
query: {
mode: 'osu',
limit: 114514
}
})
}
const c2 = await fetchUser(users[e.sender?.user_id],e)
let totalpp = 0
let totalorg = 0
let pos = 0
let stat = false
for (let key of Object.keys(c)) {
totalorg += c[key].weight.pp
}
for (let key of Object.keys(c)) {
if (!stat && pp >= c[key].pp) {
c.splice(key, 0, { pp: parseFloat(pp) })
pos = key
stat = true
} else {
c[key] = { pp: c[key].pp }
}
}
const prisepp = c2.statistics.pp - totalorg
for (let key of Object.keys(c)) {
totalpp += parseFloat(c[key].pp) * Math.pow(0.95, parseInt(key))
}
let rankNow = parseInt(await (await fetch(`https://osudaily.net/data/getPPRank.php?t=pp&v=${totalpp + prisepp}&m=0`)).text())
let rankPre = parseInt(await (await fetch(`https://osudaily.net/data/getPPRank.php?t=pp&v=${totalorg + prisepp}&m=0`)).text())
e.reply((pp * Math.pow(0.95, pos)).toFixed(2) + `pp 个人#` + (parseInt(pos) + 1) + ` (` + (Math.pow(0.95, pos)).toFixed(2) + `)\n\n总PP: ` + (totalpp + prisepp).toFixed(2) + ` (` + c2.statistics.pp.toFixed(2) + `+` + (totalpp + prisepp - c2.statistics.pp).toFixed(2) + `)\n全球#` + rankNow + ` (#${rankPre}+${rankPre - rankNow})`)
return true
}
}