Skip to content

Commit cc34cb2

Browse files
authored
Merge pull request #437 from Zickles/woolwarsfix
getPlayers errors when player hasnt played wool wars
2 parents 5746336 + 10671e3 commit cc34cb2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+15163
-15162
lines changed

src/API/getAPIStatus.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const Rss = require('rss-parser');
2-
const Parser = new Rss();
3-
module.exports = async function (options) {
4-
const Status = require('../structures/APIStatus.js');
5-
const parsed = await Parser.parseURL('https://status.hypixel.net/history.rss');
6-
if (options.raw) return parsed;
7-
return new Status(parsed);
8-
};
1+
const Rss = require('rss-parser');
2+
const Parser = new Rss();
3+
module.exports = async function (options) {
4+
const Status = require('../structures/APIStatus.js');
5+
const parsed = await Parser.parseURL('https://status.hypixel.net/history.rss');
6+
if (options.raw) return parsed;
7+
return new Status(parsed);
8+
};

src/API/getBoosters.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
module.exports = async function () {
2-
const Booster = require('../structures/Boosters/Booster');
3-
const res = await this._makeRequest('/boosters');
4-
if (res.raw) return res;
5-
return res.boosters.length ? res.boosters.map((b) => new Booster(b)).reverse() : [];
6-
};
1+
module.exports = async function () {
2+
const Booster = require('../structures/Boosters/Booster');
3+
const res = await this._makeRequest('/boosters');
4+
if (res.raw) return res;
5+
return res.boosters.length ? res.boosters.map((b) => new Booster(b)).reverse() : [];
6+
};

src/API/getGameCounts.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const GameCounts = require('../structures/GameCounts');
2-
module.exports = async function () {
3-
const res = await this._makeRequest('/counts');
4-
if (res.raw) return res;
5-
return new GameCounts(res);
6-
};
1+
const GameCounts = require('../structures/GameCounts');
2+
module.exports = async function () {
3+
const res = await this._makeRequest('/counts');
4+
if (res.raw) return res;
5+
return new GameCounts(res);
6+
};

src/API/getGuild.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
const Errors = require('../Errors');
2-
const toUuid = require('../utils/toUuid');
3-
const isGuildID = require('../utils/isGuildID');
4-
module.exports = async function (searchParameter, query) {
5-
if (!query) throw new Error(Errors.NO_GUILD_QUERY);
6-
const Guild = require('../structures/Guild/Guild');
7-
if (searchParameter === 'id' && !isGuildID(query)) throw new Error(Errors.INVALID_GUILD_ID);
8-
const isPlayerQuery = searchParameter === 'player';
9-
if (isPlayerQuery) query = await toUuid(query);
10-
if (!['id', 'name', 'player'].includes(searchParameter)) throw new Error(Errors.INVALID_GUILD_SEARCH_PARAMETER);
11-
const res = await this._makeRequest(`/guild?${searchParameter}=${encodeURI(query)}`);
12-
if (res.raw) return res;
13-
if (!res.guild && searchParameter !== 'player') {
14-
throw new Error(Errors.GUILD_DOES_NOT_EXIST);
15-
}
16-
17-
return res.guild ? new Guild(res.guild, isPlayerQuery ? query : null) : null;
18-
};
1+
const Errors = require('../Errors');
2+
const toUuid = require('../utils/toUuid');
3+
const isGuildID = require('../utils/isGuildID');
4+
module.exports = async function (searchParameter, query) {
5+
if (!query) throw new Error(Errors.NO_GUILD_QUERY);
6+
const Guild = require('../structures/Guild/Guild');
7+
if (searchParameter === 'id' && !isGuildID(query)) throw new Error(Errors.INVALID_GUILD_ID);
8+
const isPlayerQuery = searchParameter === 'player';
9+
if (isPlayerQuery) query = await toUuid(query);
10+
if (!['id', 'name', 'player'].includes(searchParameter)) throw new Error(Errors.INVALID_GUILD_SEARCH_PARAMETER);
11+
const res = await this._makeRequest(`/guild?${searchParameter}=${encodeURI(query)}`);
12+
if (res.raw) return res;
13+
if (!res.guild && searchParameter !== 'player') {
14+
throw new Error(Errors.GUILD_DOES_NOT_EXIST);
15+
}
16+
17+
return res.guild ? new Guild(res.guild, isPlayerQuery ? query : null) : null;
18+
};

src/API/getLeaderboards.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
const Errors = require('../Errors');
2-
module.exports = async function () {
3-
const Leaderboard = require('../structures/Leaderboard');
4-
const res = await this._makeRequest('/leaderboards');
5-
if (res.raw) return res;
6-
if (!res.leaderboards) throw new Error(Errors.SOMETHING_WENT_WRONG.replace(/{cause}/, 'Try again.'));
7-
const lbnames = Object.create(require('../utils/Constants').leaderboardNames);
8-
// eslint-disable-next-line guard-for-in
9-
for (const name in lbnames) {
10-
lbnames[name] = res.leaderboards[lbnames[name]].length ? res.leaderboards[lbnames[name]].map((lb) => new Leaderboard(lb)) : [];
11-
}
12-
return lbnames;
13-
};
1+
const Errors = require('../Errors');
2+
module.exports = async function () {
3+
const Leaderboard = require('../structures/Leaderboard');
4+
const res = await this._makeRequest('/leaderboards');
5+
if (res.raw) return res;
6+
if (!res.leaderboards) throw new Error(Errors.SOMETHING_WENT_WRONG.replace(/{cause}/, 'Try again.'));
7+
const lbnames = Object.create(require('../utils/Constants').leaderboardNames);
8+
// eslint-disable-next-line guard-for-in
9+
for (const name in lbnames) {
10+
lbnames[name] = res.leaderboards[lbnames[name]].length ? res.leaderboards[lbnames[name]].map((lb) => new Leaderboard(lb)) : [];
11+
}
12+
return lbnames;
13+
};

src/API/getPlayer.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
const Errors = require('../Errors');
2-
const toUuid = require('../utils/toUuid');
3-
const getGuild = require('./getGuild');
4-
const getRecentGames = require('./getRecentGames');
5-
module.exports = async function (query, options = { guild: false, recentGames: false }) {
6-
if (!query) throw new Error(Errors.NO_NICKNAME_UUID);
7-
const Player = require('../structures/Player');
8-
query = await toUuid(query);
9-
const res = await this._makeRequest(`/player?uuid=${query}`);
10-
if (res.raw) return res;
11-
if (query && !res.player) throw new Error(Errors.PLAYER_HAS_NEVER_LOGGED);
12-
let guild = null;
13-
let recentGames = null;
14-
if (options.guild) {
15-
guild = getGuild.call(this, 'player', query);
16-
}
17-
if (options.recentGames) {
18-
recentGames = getRecentGames.call(this, query);
19-
}
20-
[guild, recentGames] = await Promise.all([guild, recentGames]);
21-
return new Player(res.player, { guild, recentGames });
22-
};
1+
const Errors = require('../Errors');
2+
const toUuid = require('../utils/toUuid');
3+
const getGuild = require('./getGuild');
4+
const getRecentGames = require('./getRecentGames');
5+
module.exports = async function (query, options = { guild: false, recentGames: false }) {
6+
if (!query) throw new Error(Errors.NO_NICKNAME_UUID);
7+
const Player = require('../structures/Player');
8+
query = await toUuid(query);
9+
const res = await this._makeRequest(`/player?uuid=${query}`);
10+
if (res.raw) return res;
11+
if (query && !res.player) throw new Error(Errors.PLAYER_HAS_NEVER_LOGGED);
12+
let guild = null;
13+
let recentGames = null;
14+
if (options.guild) {
15+
guild = getGuild.call(this, 'player', query);
16+
}
17+
if (options.recentGames) {
18+
recentGames = getRecentGames.call(this, query);
19+
}
20+
[guild, recentGames] = await Promise.all([guild, recentGames]);
21+
return new Player(res.player, { guild, recentGames });
22+
};

src/API/getRecentGames.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
const Errors = require('../Errors');
2-
const toUuid = require('../utils/toUuid');
3-
const day3 = 1000 * 60 * 60 * 24 * 3;
4-
module.exports = async function (query, playerData) {
5-
if (!query) throw new Error(Errors.NO_NICKNAME_UUID);
6-
const RecentGame = require('../structures/RecentGame');
7-
8-
query = await toUuid(query);
9-
10-
const res = await this._makeRequest(`/recentgames?uuid=${query}`);
11-
if (res.raw) return res;
12-
if (res.games === []) {
13-
if (!playerData) throw new Error(Errors.PLAYER_IS_INACTIVE);
14-
if (Date.now() - playerData.lastLogoutTimestamp < day3) throw new Error(Errors.PLAYER_DISABLED_ENDPOINT);
15-
throw new Error(Errors.PLAYER_IS_INACTIVE);
16-
}
17-
return res.games.map((x) => new RecentGame(x));
18-
};
1+
const Errors = require('../Errors');
2+
const toUuid = require('../utils/toUuid');
3+
const day3 = 1000 * 60 * 60 * 24 * 3;
4+
module.exports = async function (query, playerData) {
5+
if (!query) throw new Error(Errors.NO_NICKNAME_UUID);
6+
const RecentGame = require('../structures/RecentGame');
7+
8+
query = await toUuid(query);
9+
10+
const res = await this._makeRequest(`/recentgames?uuid=${query}`);
11+
if (res.raw) return res;
12+
if (res.games === []) {
13+
if (!playerData) throw new Error(Errors.PLAYER_IS_INACTIVE);
14+
if (Date.now() - playerData.lastLogoutTimestamp < day3) throw new Error(Errors.PLAYER_DISABLED_ENDPOINT);
15+
throw new Error(Errors.PLAYER_IS_INACTIVE);
16+
}
17+
return res.games.map((x) => new RecentGame(x));
18+
};

src/API/getServerInfo.js

Lines changed: 78 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,78 @@
1-
const net = require('net');
2-
const Errors = require('../Errors');
3-
const ServerInfo = require('../structures/ServerInfo');
4-
const varInt = require('../utils/varInt');
5-
const packetsToSend = ['1500E0050E6D632E6879706978656C2E6E657463DD01', '0100', '09010000000000000000'].map((x) => Buffer.from(x, 'hex')); // To avoid dependency hell, these are precompiled as hex.
6-
module.exports = async function (repeats) {
7-
if (repeats < 0 || typeof repeats !== 'number') repeats = 3;
8-
if (repeats > 10) repeats = 10;
9-
let aggregatedData = '';
10-
let dataLength = 0;
11-
return await new Promise((resolve, reject) => {
12-
const cli = net.createConnection(25565, 'mc.hypixel.net', () => {
13-
cli.write(packetsToSend[0]);
14-
cli.write(packetsToSend[1]);
15-
});
16-
cli.on('error', () => {
17-
reject(Errors.CONNECTION_ERROR);
18-
});
19-
cli.on('data', async (data) => {
20-
if (!aggregatedData) {
21-
const varIntBorder = data.findIndex((x) => x === 0x00) + 1;
22-
dataLength = varInt(
23-
data
24-
.toString('hex', 0, varIntBorder)
25-
.match(/(..)/g)
26-
.map((x) => parseInt(x, 16))
27-
);
28-
dataLength -= varIntBorder * 8;
29-
aggregatedData += data.toString('utf-8', 5);
30-
} else aggregatedData += data.toString('utf-8');
31-
32-
if (dataLength >= aggregatedData.length) return;
33-
cli.removeAllListeners('data'); // Remove this listener
34-
resolve(parseData(aggregatedData, await getPing(repeats, cli)));
35-
});
36-
});
37-
};
38-
/**
39-
* Gets hypixel's ping
40-
* @param {number} amount Amount of times to ping
41-
* @param {net.Socket} cli Socket connected to hypixel
42-
* @returns {number}
43-
*/
44-
async function getPing(amount, cli) {
45-
let pingSum = 0;
46-
for (let i = 0; i < amount; i++) {
47-
pingSum += await ping(cli);
48-
}
49-
cli.destroy();
50-
return Math.round(pingSum / amount);
51-
}
52-
/**
53-
* Pings hypixel
54-
* @param {net.Socket} cli Client socket, connected to hypixel.
55-
* @returns {number} Ping
56-
*/
57-
async function ping(cli) {
58-
await cli.write(packetsToSend[2]);
59-
const time = Date.now();
60-
return new Promise((resolve) => {
61-
cli.once('data', () => {
62-
resolve(Date.now() - time);
63-
});
64-
});
65-
}
66-
/**
67-
* Parses the MOTD & Server status
68-
* @param {string} stringJson Parseable JSON string
69-
* @param {number} ping Ping of the server
70-
* @returns {Object} Object
71-
*/
72-
function parseData(stringJson, ping) {
73-
try {
74-
return new ServerInfo(JSON.parse(stringJson), ping);
75-
} catch (e) {
76-
return undefined;
77-
}
78-
}
1+
const net = require('net');
2+
const Errors = require('../Errors');
3+
const ServerInfo = require('../structures/ServerInfo');
4+
const varInt = require('../utils/varInt');
5+
const packetsToSend = ['1500E0050E6D632E6879706978656C2E6E657463DD01', '0100', '09010000000000000000'].map((x) => Buffer.from(x, 'hex')); // To avoid dependency hell, these are precompiled as hex.
6+
module.exports = async function (repeats) {
7+
if (repeats < 0 || typeof repeats !== 'number') repeats = 3;
8+
if (repeats > 10) repeats = 10;
9+
let aggregatedData = '';
10+
let dataLength = 0;
11+
return await new Promise((resolve, reject) => {
12+
const cli = net.createConnection(25565, 'mc.hypixel.net', () => {
13+
cli.write(packetsToSend[0]);
14+
cli.write(packetsToSend[1]);
15+
});
16+
cli.on('error', () => {
17+
reject(Errors.CONNECTION_ERROR);
18+
});
19+
cli.on('data', async (data) => {
20+
if (!aggregatedData) {
21+
const varIntBorder = data.findIndex((x) => x === 0x00) + 1;
22+
dataLength = varInt(
23+
data
24+
.toString('hex', 0, varIntBorder)
25+
.match(/(..)/g)
26+
.map((x) => parseInt(x, 16))
27+
);
28+
dataLength -= varIntBorder * 8;
29+
aggregatedData += data.toString('utf-8', 5);
30+
} else aggregatedData += data.toString('utf-8');
31+
32+
if (dataLength >= aggregatedData.length) return;
33+
cli.removeAllListeners('data'); // Remove this listener
34+
resolve(parseData(aggregatedData, await getPing(repeats, cli)));
35+
});
36+
});
37+
};
38+
/**
39+
* Gets hypixel's ping
40+
* @param {number} amount Amount of times to ping
41+
* @param {net.Socket} cli Socket connected to hypixel
42+
* @returns {number}
43+
*/
44+
async function getPing(amount, cli) {
45+
let pingSum = 0;
46+
for (let i = 0; i < amount; i++) {
47+
pingSum += await ping(cli);
48+
}
49+
cli.destroy();
50+
return Math.round(pingSum / amount);
51+
}
52+
/**
53+
* Pings hypixel
54+
* @param {net.Socket} cli Client socket, connected to hypixel.
55+
* @returns {number} Ping
56+
*/
57+
async function ping(cli) {
58+
await cli.write(packetsToSend[2]);
59+
const time = Date.now();
60+
return new Promise((resolve) => {
61+
cli.once('data', () => {
62+
resolve(Date.now() - time);
63+
});
64+
});
65+
}
66+
/**
67+
* Parses the MOTD & Server status
68+
* @param {string} stringJson Parseable JSON string
69+
* @param {number} ping Ping of the server
70+
* @returns {Object} Object
71+
*/
72+
function parseData(stringJson, ping) {
73+
try {
74+
return new ServerInfo(JSON.parse(stringJson), ping);
75+
} catch (e) {
76+
return undefined;
77+
}
78+
}

src/API/getStatus.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const toUuid = require('../utils/toUuid');
2-
module.exports = async function (query) {
3-
const Status = require('../structures/Status');
4-
query = await toUuid(query);
5-
const res = await this._makeRequest(`/status?uuid=${query}`);
6-
if (res.raw) return res;
7-
return new Status(res.session);
8-
};
1+
const toUuid = require('../utils/toUuid');
2+
module.exports = async function (query) {
3+
const Status = require('../structures/Status');
4+
query = await toUuid(query);
5+
const res = await this._makeRequest(`/status?uuid=${query}`);
6+
if (res.raw) return res;
7+
return new Status(res.session);
8+
};

src/API/getWatchdogStats.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
module.exports = async function () {
2-
const WatchdogStats = require('../structures/Watchdog/Stats');
3-
const res = await this._makeRequest('/punishmentstats');
4-
if (res.raw) return res;
5-
return new WatchdogStats(res);
6-
};
1+
module.exports = async function () {
2+
const WatchdogStats = require('../structures/Watchdog/Stats');
3+
const res = await this._makeRequest('/punishmentstats');
4+
if (res.raw) return res;
5+
return new WatchdogStats(res);
6+
};

0 commit comments

Comments
 (0)