koishi-plugin-beatsaber-bot
Version:
一个 用于 BeatSaber 的 koishi Bot 插件
65 lines (64 loc) • 2.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.blRequest = void 0;
const blRequest = (ctx, cfg) => {
const http = ctx.http;
let host = "https://api.beatleader.xyz";
if (host.endsWith("/")) {
host = host.substring(0, host.length - 1);
}
const url = (path) => {
if (!path.startsWith("/")) {
path = "/" + path;
}
return host + path;
};
const getPlayerScore = async (req) => http.get(url(`/score/${req.leaderboardContext}/${req.playerID}/${req.hash}/${req.diff}/${req.mode}`));
const getTokenInfo = async (accessToken) => {
return ctx.http.get(url(`/oauth2/identity`), {
headers: {
Authorization: `Bearer ${accessToken}`
}
});
};
const refreshOAuthToken = async (refreshToken) => {
const form = new URLSearchParams();
form.append("client_id", cfg.blOauthClientId);
form.append("client_secret", cfg.blOauthClientSecret);
form.append("grant_type", "refresh_token");
form.append("refresh_token", refreshToken);
const res = await fetch("https://beatsaver.com/api/oauth2/token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: form,
}).then(res => res.json());
if (!res.access_token) {
throw new Error("refreshToken failed");
}
return res;
};
const getPlayerInfo = async (accountId) => {
return ctx.http.get(url(`/player/${accountId}`));
};
const getPlayerScores = async (accountId) => {
return ctx.http.get(url(`/player/${accountId}/scores?count=24&sortBy=pp`));
};
const getPlayerPinnedScores = async (accountId) => {
return ctx.http.get(url(`/player/${accountId}/pinnedScores`));
};
const getBeatScore = async (scoreId) => {
return ctx.http.get(url(`/score/${scoreId}`));
};
return {
getPlayerScore,
getPlayerScores,
getPlayerInfo,
getPlayerPinnedScores,
getTokenInfo,
refreshOAuthToken,
getBeatScore
};
};
exports.blRequest = blRequest;