Skip to content

Commit 3b8d511

Browse files
committed
feat(server/bots): enhance server count validation by allowing a difference based on actual count
1 parent ee81354 commit 3b8d511

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

server/src/routes/bots/[id]/stats.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,30 @@ module.exports = {
1919
async (request, response) => {
2020
const { id, command_count, server_count } = matchedData(request);
2121

22-
if (!command_count && !server_count) return response.sendError('One of the following fields is required: command_count, server_count.', 400);
22+
if (command_count === undefined && server_count === undefined) return response.sendError('One of the following fields is required: command_count, server_count.', 400);
2323

2424
const apiKey = request.headers['authorization'];
2525
if (!apiKey) return response.sendError('Authorization header is required.', 401);
2626

27+
const decryptedApiKey = bot.getDecryptedApiKey(apiKey);
28+
if (!decryptedApiKey) return response.sendError('Invalid API key.', 401);
29+
2730
const botUser = client.users.cache.get(id) || await client.users.fetch(id).catch(() => null);
2831
if (!botUser) return response.sendError('Bot not found.', 404);
2932

3033
const bot = await Bot.findOne({ id });
3134
if (!bot) return response.sendError('Bot not found.', 404);
3235

33-
const decryptedApiKey = bot.getDecryptedApiKey(apiKey);
34-
if (!decryptedApiKey) return response.sendError('Invalid API key.', 401);
35-
36-
if (command_count) bot.command_count = { value: command_count, updatedAt: new Date() };
37-
if (server_count) {
36+
if (command_count !== undefined) bot.command_count = { value: command_count, updatedAt: new Date() };
37+
if (server_count !== undefined) {
3838
const approximate_guild_count_data = await getApproximateGuildCount(id).catch(() => null);
3939
if (!approximate_guild_count_data) return response.sendError('Could not fetch server count.', 500);
4040

41-
if (Math.abs(server_count - approximate_guild_count_data.approximate_guild_count) > config.maxServerCountDifference) return response.sendError(`The server count provided (${server_count}) is too far off from the actual server count. It cannot differ by more than ${config.maxServerCountDifference} from the actual server count, which is ${approximate_guild_count_data.approximate_guild_count}.`, 400);
41+
const actualGuildCount = approximate_guild_count_data.approximate_guild_count;
42+
const maxDifference = Math.max(config.maxServerCountDifference, Math.floor(actualGuildCount * 0.1));
43+
const countDifference = Math.abs(server_count - actualGuildCount);
44+
45+
if (countDifference > maxDifference) return response.sendError(`The server count provided (${server_count}) is too far off from the actual server count. It cannot differ by more than ${maxDifference} from the actual server count, which is ${actualGuildCount}.`, 400);
4246

4347
bot.server_count = { value: server_count, updatedAt: new Date() };
4448
}

server/src/utils/bots/getApproximateGuildCount.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ async function getApproximateGuildCount(botId) {
55
if (!process.env.DISCORD_BOT_GET_APPROXIMATE_GUILD_COUNT_API_SECRET) throw new Error('DISCORD_BOT_GET_APPROXIMATE_GUILD_COUNT_API_SECRET environment variable is missing');
66

77
try {
8+
89
const response = await axios.get(`${process.env.DISCORD_BOT_GET_APPROXIMATE_GUILD_COUNT_API_URL}/bots/${botId}`, {
910
headers: {
1011
'authorization': process.env.DISCORD_BOT_GET_APPROXIMATE_GUILD_COUNT_API_SECRET

0 commit comments

Comments
 (0)