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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
use std::time::Duration;
use crate::{Context, Error};
use poise::serenity_prelude as serenity;
use reqwest::{header, ClientBuilder};
use serde::{Deserialize, Serialize};
#[derive(Deserialize)]
struct OsuTokenResponse {
pub access_token: String,
}
#[derive(Serialize)]
struct OsuTokenRequest {
pub client_id: u32,
pub client_secret: String,
pub grant_type: String,
pub scope: String,
}
/// This is kinda loose, and we should really be caching the osu token
///
/// Eh well, this *works* (sort of)
async fn setup_reqwest() -> Result<reqwest::Client, Error> {
let client_id = std::env::var("OSU_CLIENT_ID").unwrap();
let client_secret = std::env::var("OSU_CLIENT_SECRET").unwrap();
let token_req = OsuTokenRequest {
client_id: client_id.parse::<u32>().unwrap(),
client_secret,
grant_type: "client_credentials".into(),
scope: "public".into(),
};
let req = reqwest::Client::new()
.post("https://osu.ppy.sh/oauth/token")
.json(&token_req)
.send()
.await?
.json::<OsuTokenResponse>()
.await?;
let mut headers = header::HeaderMap::new();
headers.insert(
"Authorization",
header::HeaderValue::from_str(format!("Bearer {}", req.access_token).as_str()).unwrap(),
);
Ok(ClientBuilder::new()
.default_headers(headers)
.build()
.unwrap())
}
#[derive(Deserialize, Serialize, Clone, Debug)]
struct OsuUser {
pub username: String,
pub avatar_url: String,
pub country_code: String,
pub is_supporter: bool,
pub join_date: chrono::DateTime<chrono::Utc>,
pub statistics: OsuUserStats,
}
#[derive(Deserialize, Serialize, Clone, Debug)]
struct OsuUserStats {
pub global_rank: Option<u32>,
pub pp: f32,
pub hit_accuracy: Option<f32>,
pub grade_counts: OsuUserStatsGrades,
pub country_rank: Option<u32>,
}
#[derive(Deserialize, Serialize, Clone, Debug)]
struct OsuUserStatsGrades {
pub ss: u32,
pub s: u32,
pub a: u32,
}
/// Gets an osu profile by username
///
/// Usage:
/// ~osup <username>
/// Examples:
/// ~osup muirrum
#[poise::command(slash_command, prefix_command)]
pub async fn osup(
ctx: Context<'_>,
#[description = "The osu! username or ID to look up"] lookup: String,
) -> Result<(), Error> {
let client = setup_reqwest().await?;
let mut res = client
.get(format!(
"https://osu.ppy.sh/api/v2/users/{}?key=username",
lookup
))
.send()
.await?
.json::<OsuUser>()
.await?;
res.country_code = res.country_code.to_lowercase();
ctx.send(|m| {
m.embed(|e| {
e.title(format!("osu! Profile: {}", res.clone().username));
e.thumbnail(res.clone().avatar_url);
e.field(
"Ranks",
format!(
":map: #{}\n:flag_{}: #{}",
res.clone().statistics.global_rank.unwrap_or(0),
res.clone().country_code,
res.clone().statistics.country_rank.unwrap_or(0u32)
),
true,
);
e.field(
"Stats",
format!(
"**PP:** {}\n**Acc:** {}%",
res.clone().statistics.pp,
res.clone().statistics.hit_accuracy.unwrap_or(0.0)
),
false,
);
e
});
m
})
.await?;
Ok(())
}
#[derive(Deserialize, Debug, Clone)]
struct OsuBeatMap {
pub id: u32,
pub mode: String,
pub status: String,
pub version: String,
pub total_length: u32,
pub difficulty_rating: f32,
pub bpm: u32,
pub last_updated: chrono::DateTime<chrono::Utc>,
pub passcount: u32,
pub playcount: u32,
pub beatmapset: OsuBeatMapSet,
pub url: String,
}
#[derive(Deserialize, Debug, Clone)]
struct OsuBeatMapSet {
pub id: u32,
pub nsfw: bool,
pub title: String,
pub artist: String,
pub covers: OsuBeatMapSetCovers,
pub creator: String,
pub tags: String,
pub submitted_date: chrono::DateTime<chrono::Utc>,
}
#[derive(Deserialize, Debug, Clone)]
struct OsuBeatMapSetCovers {
#[serde(rename = "list@2x")]
pub list2: String,
}
/// Looks up an osu! beatmap by its ID
///
/// Usage:
/// ~osubm <id>
#[poise::command(slash_command, prefix_command)]
pub async fn osubm(
ctx: Context<'_>,
#[description = "The beatmap ID"] bm_id: u32,
) -> Result<(), Error> {
let client = setup_reqwest().await?;
let mut res = client
.get(format!("https://osu.ppy.sh/api/v2/beatmaps/{}", bm_id))
.send()
.await?
.json::<OsuBeatMap>()
.await?;
ctx.send(|m| {
m.embed(|e| {
e.title(format!(
"osu! Beatmap: {} by {}",
res.beatmapset.title, res.beatmapset.creator
));
e.image(res.beatmapset.covers.list2);
e.description(format!(
"**Link:** {}\n**Length:** {} **BPM:** {}\n**Difficulty:** {}:star:",
res.url, res.total_length, res.bpm, res.difficulty_rating
));
e.footer(|f| {
f.text(format!(
"BM ID {} | BM Set ID {}\nCreated {}",
res.id, res.beatmapset.id, res.beatmapset.submitted_date
));
f
});
e
});
m
})
.await?;
Ok(())
}
|