koishi-plugin-beatsaber-bot
Version:
一个 用于 BeatSaber 的 koishi Bot 插件
60 lines (59 loc) • 2.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.bsRequest = void 0;
const bsRequest = (ctx, cfg) => {
const http = ctx.http;
let host = cfg.beatSaverHost ?? "https://api.beatsaver.com";
if (host.endsWith("/")) {
host = host.substring(0, host.length - 1);
}
const url = (path) => {
if (!path.startsWith("/")) {
path = "/" + path;
}
return host + path;
};
const getBSMapperById = async (userId) => http.get(url(`/users/id/${userId}`));
const getLatestMaps = async (pageSize = 5) => http.get(url(`/maps/latest?sort=FIRST_PUBLISHED&pageSize=${pageSize}`)).then(res => res.docs);
const searchMapByKeyword = async (key) => http.get(url(`/search/text/0?q=${key}`)).then(res => res.docs);
const searchMapById = async (id) => http.get(url(`/maps/id/${id}`));
const getAlertsStats = async (accessToken) => http.get(url(`/alerts/stats`), { headers: { Authorization: `Bearer ${accessToken}` } });
const getAlertsByPage = async (accessToken, type, page) => http.get(url(`/alerts/${type}/${page}`), { headers: { Authorization: `Bearer ${accessToken}` } });
const getUnreadAlertsByPage = (accessToken, page) => getAlertsByPage(accessToken, 'unread', page);
const getReadAlertsByPage = (accessToken, page) => getAlertsByPage(accessToken, 'read', page);
const refreshOAuthToken = async (refreshToken) => {
const form = new FormData();
form.append("client_id", cfg.bsOauthClientId);
form.append("client_secret", cfg.bsOauthClientSecret);
form.append("grant_type", "refresh_token");
form.append("refresh_token", refreshToken);
const res = await fetch("https://beatsaver.com/api/oauth2/token", {
method: "POST",
body: form,
}).then(res => res.json());
if (!res.access_token) {
throw new Error("refreshToken failed");
}
return res;
};
//
const getTokenInfo = async (accessToken) => {
return ctx.http.get(url(`/oauth2/identity`), {
headers: {
Authorization: `Bearer ${accessToken}`
}
});
};
const getMapsByHashes = async (hashes) => http.get(url(`/maps/hash/${hashes.join(",")}`));
return {
refreshOAuthToken,
getBSMapperById,
getLatestMaps,
searchMapByKeyword,
searchMapById,
getUnreadAlertsByPage,
getTokenInfo,
getMapsByHashes
};
};
exports.bsRequest = bsRequest;