UNPKG

discordbotlist

Version:

Integrate with discordbotlist.com and support voting rewards and promote your bot's stats on the website.

75 lines (74 loc) 3.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.postBotCommands = exports.fetchRecentVotes = exports.postBotStats = exports.route = exports.BASE_URL = void 0; const undici_1 = require("undici"); const internal_1 = require("../internal"); exports.BASE_URL = "https://discordbotlist.com/api/v1"; function route(path) { return `${exports.BASE_URL}${path}`; } exports.route = route; /** * Post bot stats to be displayed on the website. * @param apiKey API key generated by selecting "Generate token" in the Admin section of your bot's page. **Do not use your Discord bot token.** * @param botId ID of the bot to post stats for. * @param stats Stats as they should appear on the website. */ async function postBotStats(apiKey, botId, stats) { const res = await (0, undici_1.fetch)(route(`/bots/${botId}/stats`), { body: JSON.stringify(stats), headers: new undici_1.Headers({ Authorization: apiKey, "Content-Type": "application/json", }), method: "POST", }); if (!res.ok) throw new internal_1.DBLError(res.statusText, { response: res }); } exports.postBotStats = postBotStats; /** * Get recent votes (within the past 12 hours) for a bot. * @param apiKey API key generated by selecting "Generate token" in the Admin section of your bot's page. **Do not use your Discord bot token.** * @param botId ID of the bot to get recent votes for. */ async function fetchRecentVotes(apiKey, botId) { const res = await (0, undici_1.fetch)(route(`/bots/${botId}/upvotes`), { headers: new undici_1.Headers({ Authorization: apiKey, }), }); if (!res.ok) throw new internal_1.DBLError(res.statusText, { response: res }); const data = (await res.json()); return { total: data.total, votes: data.upvotes.map(vote => ({ avatar: vote.avatar, discriminator: vote.discriminator, id: vote.user_id, username: vote.username, time: new Date(vote.timestamp), })), }; } exports.fetchRecentVotes = fetchRecentVotes; /** * Post the slash commands your bot supports. These will be shown on your bot page. * @param apiKey API key generated by selecting "Generate token" in the Admin section of your bot's page. **Do not use your Discord bot token.** * @param botId ID of the bot to post commands for. * @param commands The commands to post, in the same format as they'd be sent to Discord. */ async function postBotCommands(apiKey, botId, commands) { const res = await (0, undici_1.fetch)(route(`/bots/${botId}/commands`), { body: JSON.stringify(commands), headers: new undici_1.Headers({ Authorization: apiKey, "Content-Type": "application/json", }), method: "POST", }); if (!res.ok) throw new internal_1.DBLError(res.statusText, { response: res }); } exports.postBotCommands = postBotCommands;