erlc-api 2.3.4 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,52 +1,78 @@
1
- const { BASEURL , Vanity} = require("../../constants.js");
2
- module.exports = (serverToken) => {
3
- return new Promise(async (resolve, reject) => {
4
- try {
5
- const fetch = await import("node-fetch");
6
- const { config } = await import("../../erlc.js");
7
-
8
- const res = await fetch.default(`${BASEURL}/server`, {
9
- headers: {
10
- "Authorization": config?.globalToken,
11
- "Server-Key": serverToken,
12
- },
13
- });
14
-
15
- const data = await res.json().catch((err) => {
16
- return reject(err);
17
- });
18
-
19
- if (!res.ok) {
20
- return reject(data);
21
- }
22
- const getUsername = async (userId) => {
23
- const response = await fetch.default(`https://users.roblox.com/v1/users/${userId}`);
24
- const userData = await response.json();
25
- if (!response.ok) {
26
- throw new Error(`Error fetching username for ID: ${userId}`);
27
- }
28
- return userData.name;
29
- };
30
- try {
31
- const ownerUsername = await getUsername(data.OwnerId);
32
- const coOwnerUsernames = await Promise.all(data.CoOwnerIds.map(getUsername));
33
- const VanityURL = `${Vanity}${data.JoinKey}`
34
-
35
-
36
- data.OwnerUsername = ownerUsername;
37
- data.CoOwnerUsernames = coOwnerUsernames;
38
- data.VanityURL = VanityURL
39
-
40
- delete data.OwnerId;
41
- delete data.CoOwnerIds;
42
-
43
- resolve(data);
44
- } catch (error) {
45
- reject(error);
46
- }
47
-
48
- } catch (error) {
49
- reject(error);
50
- }
51
- });
1
+ const { BASEURL, Vanity } = require("../../constants.js");
2
+
3
+ module.exports = (serverToken) => {
4
+ return new Promise(async (resolve, reject) => {
5
+ try {
6
+ const fetch = await import("node-fetch");
7
+ const { config } = await import("../../erlc.js");
8
+
9
+ const res = await fetch.default(`${BASEURL}/server`, {
10
+ headers: {
11
+ "Authorization": config?.globalToken,
12
+ "Server-Key": serverToken,
13
+ },
14
+ });
15
+
16
+ const data = await res.json().catch((err) => {
17
+ return reject(err);
18
+ });
19
+
20
+ if (!res.ok) {
21
+ return reject(data);
22
+ }
23
+
24
+ const getUsername = async (userId) => {
25
+ try {
26
+ const response = await fetch.default(`https://users.roblox.com/v1/users/${userId}`);
27
+ const userData = await response.json();
28
+ if (!response.ok) {
29
+ console.warn(`Warning: Could not fetch username for ID: ${userId}`);
30
+ return `User:${userId}`; // Fallback format
31
+ }
32
+ return userData.name;
33
+ } catch (error) {
34
+ console.warn(`Warning: Error fetching username for ID: ${userId}`, error);
35
+ return `User:${userId}`; // Fallback format
36
+ }
37
+ };
38
+
39
+ try {
40
+ // Get owner username
41
+ const ownerUsername = await getUsername(data.OwnerId);
42
+
43
+ // Get co-owner usernames (handle empty array case)
44
+ const coOwnerUsernames = data.CoOwnerIds && data.CoOwnerIds.length > 0
45
+ ? await Promise.all(data.CoOwnerIds.map(getUsername))
46
+ : [];
47
+
48
+ // Create vanity URL
49
+ const vanityURL = `${Vanity}${data.JoinKey}`;
50
+
51
+ // Add the new properties to the response
52
+ const enhancedData = {
53
+ ...data,
54
+ OwnerUsername: ownerUsername,
55
+ CoOwnerUsernames: coOwnerUsernames,
56
+ VanityURL: vanityURL
57
+ };
58
+
59
+ // Remove the original ID properties as they're now converted to usernames
60
+ delete enhancedData.OwnerId;
61
+ delete enhancedData.CoOwnerIds;
62
+
63
+ resolve(enhancedData);
64
+ } catch (error) {
65
+ // If username fetching fails, still return the original data with IDs
66
+ console.warn('Warning: Could not fetch usernames, returning data with IDs:', error);
67
+ const fallbackData = {
68
+ ...data,
69
+ VanityURL: `${Vanity}${data.JoinKey}`
70
+ };
71
+ resolve(fallbackData);
72
+ }
73
+
74
+ } catch (error) {
75
+ reject(error);
76
+ }
77
+ });
52
78
  };
@@ -0,0 +1,54 @@
1
+ const { BASEURL } = require("../../constants.js");
2
+
3
+ /**
4
+ * Retrieves server staff information
5
+ * @param {string} serverToken - The server API key
6
+ * @returns {Promise<Object>} Promise that resolves to server staff object
7
+ */
8
+ module.exports = (serverToken) => {
9
+ return new Promise(async (resolve, reject) => {
10
+ // Input validation
11
+ if (!serverToken || typeof serverToken !== 'string') {
12
+ return reject(new Error('Server token is required and must be a string'));
13
+ }
14
+
15
+ try {
16
+ const fetch = await import("node-fetch");
17
+ const { config } = await import("../../erlc.js");
18
+
19
+ // Check if global token is configured
20
+ if (!config?.globalToken) {
21
+ return reject(new Error('Global token not configured. Please initialize the client first.'));
22
+ }
23
+
24
+ const res = await fetch.default(`${BASEURL}/server/staff`, {
25
+ headers: {
26
+ "Authorization": config.globalToken,
27
+ "Server-Key": serverToken,
28
+ },
29
+ timeout: 10000, // 10 second timeout
30
+ });
31
+
32
+ if (!res.ok) {
33
+ const errorData = await res.json().catch(() => ({ error: 'Unknown API error' }));
34
+ const error = new Error(`API Error: ${res.status} - ${errorData.error || res.statusText}`);
35
+ error.status = res.status;
36
+ error.data = errorData;
37
+ return reject(error);
38
+ }
39
+
40
+ const data = await res.json();
41
+ resolve(data || { CoOwners: [], Admins: {}, Mods: {} });
42
+
43
+ } catch (error) {
44
+ // Handle different types of errors
45
+ if (error.code === 'ENOTFOUND') {
46
+ reject(new Error('Network error: Unable to connect to ER:LC API'));
47
+ } else if (error.name === 'AbortError') {
48
+ reject(new Error('Request timeout: API took too long to respond'));
49
+ } else {
50
+ reject(error);
51
+ }
52
+ }
53
+ });
54
+ };
@@ -1,28 +1,54 @@
1
- const { BASEURL } = require("../../constants.js");
2
-
3
- module.exports = (serverToken) => {
4
- return new Promise(async (resolve, reject) => {
5
- try {
6
- const fetch = await import("node-fetch");
7
- const { config } = await import("../../erlc.js");
8
-
9
- const res = await fetch.default(`${BASEURL}/server/vehicles`, {
10
- headers: {
11
- Authorization: config?.globalToken,
12
- "Server-Key": serverToken,
13
- },
14
- });
15
- const data = await res.json().catch((err) => {
16
- return reject(err);
17
- });
18
-
19
- if (!res.ok) {
20
- return reject(data);
21
- }
22
-
23
- resolve(data);
24
- } catch (error) {
25
- reject(error);
26
- }
27
- });
28
- };
1
+ const { BASEURL } = require("../../constants.js");
2
+
3
+ /**
4
+ * Retrieves server vehicles information
5
+ * @param {string} serverToken - The server API key
6
+ * @returns {Promise<Array>} Promise that resolves to array of vehicles
7
+ */
8
+ module.exports = (serverToken) => {
9
+ return new Promise(async (resolve, reject) => {
10
+ // Input validation
11
+ if (!serverToken || typeof serverToken !== 'string') {
12
+ return reject(new Error('Server token is required and must be a string'));
13
+ }
14
+
15
+ try {
16
+ const fetch = await import("node-fetch");
17
+ const { config } = await import("../../erlc.js");
18
+
19
+ // Check if global token is configured
20
+ if (!config?.globalToken) {
21
+ return reject(new Error('Global token not configured. Please initialize the client first.'));
22
+ }
23
+
24
+ const res = await fetch.default(`${BASEURL}/server/vehicles`, {
25
+ headers: {
26
+ "Authorization": config.globalToken,
27
+ "Server-Key": serverToken,
28
+ },
29
+ timeout: 10000, // 10 second timeout
30
+ });
31
+
32
+ if (!res.ok) {
33
+ const errorData = await res.json().catch(() => ({ error: 'Unknown API error' }));
34
+ const error = new Error(`API Error: ${res.status} - ${errorData.error || res.statusText}`);
35
+ error.status = res.status;
36
+ error.data = errorData;
37
+ return reject(error);
38
+ }
39
+
40
+ const data = await res.json();
41
+ resolve(Array.isArray(data) ? data : []);
42
+
43
+ } catch (error) {
44
+ // Handle different types of errors
45
+ if (error.code === 'ENOTFOUND') {
46
+ reject(new Error('Network error: Unable to connect to ER:LC API'));
47
+ } else if (error.name === 'AbortError') {
48
+ reject(new Error('Request timeout: API took too long to respond'));
49
+ } else {
50
+ reject(error);
51
+ }
52
+ }
53
+ });
54
+ };
@@ -1,29 +1,70 @@
1
- const { BASEURL } = require("../../constants.js");
2
-
3
- module.exports = (serverToken, command) => {
4
- return new Promise(async (resolve, reject) => {
5
- try {
6
- const fetch = await import("node-fetch");
7
- const { config } = await import("../../erlc.js");
8
- const params = JSON.stringify({ command: `${command}` });
9
- const res = await fetch.default(`${BASEURL}/server/command`, {
10
- headers: {
11
- Authorization: config?.globalToken,
12
- "Server-Key": serverToken,
13
- "Content-Type": "application/json",
14
- },
15
- method: "POST",
16
- body: params,
17
- });
18
-
19
- if (!res.ok) {
20
- const errorData = await res.json();
21
- return reject(errorData);
22
- }
23
-
24
- resolve(true);
25
- } catch (error) {
26
- reject(error);
27
- }
28
- });
29
- };
1
+ const { BASEURL } = require("../../constants.js");
2
+
3
+ /**
4
+ * Executes a command on the server
5
+ * @param {string} serverToken - The server API key
6
+ * @param {string} command - The command to execute
7
+ * @returns {Promise<boolean>} Promise that resolves to true if command was executed successfully
8
+ */
9
+ module.exports = (serverToken, command) => {
10
+ return new Promise(async (resolve, reject) => {
11
+ // Input validation
12
+ if (!serverToken || typeof serverToken !== 'string') {
13
+ return reject(new Error('Server token is required and must be a string'));
14
+ }
15
+
16
+ if (!command || typeof command !== 'string') {
17
+ return reject(new Error('Command is required and must be a string'));
18
+ }
19
+
20
+ if (command.trim().length === 0) {
21
+ return reject(new Error('Command cannot be empty'));
22
+ }
23
+
24
+ try {
25
+ const fetch = await import("node-fetch");
26
+ const { config } = await import("../../erlc.js");
27
+
28
+ // Check if global token is configured
29
+ if (!config?.globalToken) {
30
+ return reject(new Error('Global token not configured. Please initialize the client first.'));
31
+ }
32
+
33
+ const requestBody = JSON.stringify({ command: command.trim() });
34
+
35
+ const res = await fetch.default(`${BASEURL}/server/command`, {
36
+ method: "POST",
37
+ headers: {
38
+ "Authorization": config.globalToken,
39
+ "Server-Key": serverToken,
40
+ "Content-Type": "application/json",
41
+ },
42
+ body: requestBody,
43
+ timeout: 15000, // 15 second timeout for commands
44
+ });
45
+
46
+ if (!res.ok) {
47
+ const errorData = await res.json().catch(() => ({ error: 'Unknown API error' }));
48
+ const error = new Error(`Command execution failed: ${res.status} - ${errorData.error || res.statusText}`);
49
+ error.status = res.status;
50
+ error.data = errorData;
51
+ return reject(error);
52
+ }
53
+
54
+ // Command executed successfully
55
+ resolve(true);
56
+
57
+ } catch (error) {
58
+ // Handle different types of errors
59
+ if (error.code === 'ENOTFOUND') {
60
+ reject(new Error('Network error: Unable to connect to ER:LC API'));
61
+ } else if (error.name === 'AbortError') {
62
+ reject(new Error('Request timeout: Command took too long to execute'));
63
+ } else if (error.message.includes('JSON')) {
64
+ reject(new Error('Invalid command format'));
65
+ } else {
66
+ reject(error);
67
+ }
68
+ }
69
+ });
70
+ };
@@ -1,5 +1,5 @@
1
- declare module 'NodeModule' {
2
- interface NodeModule {
3
- type?: string;
4
- }
1
+ declare module 'NodeModule' {
2
+ interface NodeModule {
3
+ type?: string;
4
+ }
5
5
  }
@@ -1,91 +1,98 @@
1
- declare module "erlc-api" {
2
- export interface ClientConfig {
3
- globalToken: string; // The ER:LC global API token
4
- }
5
-
6
- export const BASEURL = "https://api.policeroleplay.community/v1";
7
-
8
- type PlayerId = string;
9
- type PlayerName = string;
10
- type TextureName = string;
11
- type CarName = string;
12
-
13
- export type ErlcPlayer = `${PlayerName}:${PlayerId}`; // Playername:UserID
14
- export type ErlcPlayerPermission =
15
- | "Normal"
16
- | "Server Administrator"
17
- | "Server Owner"
18
- | "Server Moderator";
19
-
20
- export interface ServerStatus {
21
- Name: string; // The server name
22
- OwnerId: number; // The roblox id of the server owner
23
- CoOwnerIds: number[]; // The roblox ids of the server co owners
24
- CurrentPlayers: number; // The amount of people currently in-game
25
- MaxPlayers: number; // The amount of people who can join the server including server owner
26
- JoinKey: string; // The code used to join the private server
27
- AccVerifiedReq: "Disabled" | "Email" | "Phone/ID"; // The level of verification roblox accounts need to join the private server
28
- TeamBalance: boolean; // If team balance is enabled or not
29
- }
30
-
31
- export interface ServerPlayer {
32
- Player: ErlcPlayer;
33
- Permission: ErlcPlayerPermission;
34
- }
35
-
36
- export interface JoinLog {
37
- Join: boolean; // True is join and False is leave
38
- Timestamp: number; // Timestamp in seconds
39
- Player: ErlcPlayer;
40
- }
41
-
42
- export interface KillLog {
43
- Killed: ErlcPlayer;
44
- Timestamp: number; // Timestamp in seconds
45
- Killer: ErlcPlayer;
46
- }
47
-
48
- export interface CommandLog {
49
- Player: ErlcPlayer;
50
- Timestamp: number; // Timestamp in seconds
51
- Command: string;
52
- }
53
-
54
- export interface ModcallLog {
55
- Caller: ErlcPlayer;
56
- Moderator?: ErlcPlayer; // If call is unanswered property is undefined
57
- Timestamp: number; // Timestamp in seconds
58
- }
59
-
60
- export type ServerBan = Record<PlayerId, PlayerName>;
61
-
62
- export interface VehiclesLog {
63
- Texture: string | null;
64
- Name: string;
65
- Owner: ErlcPlayer;
66
- }
67
-
68
- export interface VSMCommandBody {
69
- command: string; // ":h Hey everyone!"
70
- }
71
-
72
-
73
- export function getBans(serverToken: string): Promise<ServerBan>;
74
- export function getCommandLogs(serverToken: string): Promise<CommandLog[]>;
75
- export function getJoinLogs(serverToken: string): Promise<JoinLog[]>;
76
- export function getKillLogs(serverToken: string): Promise<KillLog[]>;
77
- export function getModcallLogs(serverToken: string): Promise<ModcallLog[]>;
78
- export function getPlayers(serverToken: string): Promise<ServerPlayer[]>;
79
- export function getQueue(serverToken: string): Promise<number[]>;
80
- export function getServer(serverToken: string): Promise<ServerStatus>;
81
- export function getVehicles(serverToken: string): Promise<VehiclesLog[]>;
82
- export function runCommand(
83
- serverToken: string,
84
- command: string
85
- ): Promise<boolean>;
86
-
87
- export class Client {
88
- constructor(options: ClientConfig);
89
- config(): ClientConfig;
90
- }
91
- }
1
+ declare module "erlc-api" {
2
+ export interface ClientConfig {
3
+ globalToken: string; // The ER:LC global API token
4
+ }
5
+
6
+ export const BASEURL = "https://api.policeroleplay.community/v1";
7
+
8
+ type PlayerId = string;
9
+ type PlayerName = string;
10
+ type TextureName = string;
11
+ type CarName = string;
12
+
13
+ export type ErlcPlayer = `${PlayerName}:${PlayerId}`; // Playername:UserID
14
+ export type ErlcPlayerPermission =
15
+ | "Normal"
16
+ | "Server Administrator"
17
+ | "Server Owner"
18
+ | "Server Moderator";
19
+
20
+ export interface ServerStatus {
21
+ Name: string; // The server name
22
+ OwnerUsername: string; // The username of the server owner
23
+ CoOwnerUsernames: string[]; // The usernames of the server co owners
24
+ CurrentPlayers: number; // The amount of people currently in-game
25
+ MaxPlayers: number; // The amount of people who can join the server including server owner
26
+ JoinKey: string; // The code used to join the private server
27
+ AccVerifiedReq: "Disabled" | "Email" | "Phone/ID"; // The level of verification roblox accounts need to join the private server
28
+ TeamBalance: boolean; // If team balance is enabled or not
29
+ VanityURL: string; // The vanity URL to join the server
30
+ }
31
+
32
+ export interface ServerPlayer {
33
+ Player: ErlcPlayer;
34
+ Permission: ErlcPlayerPermission;
35
+ }
36
+
37
+ export interface JoinLog {
38
+ Join: boolean; // True is join and False is leave
39
+ Timestamp: number; // Timestamp in seconds
40
+ Player: ErlcPlayer;
41
+ }
42
+
43
+ export interface KillLog {
44
+ Killed: ErlcPlayer;
45
+ Timestamp: number; // Timestamp in seconds
46
+ Killer: ErlcPlayer;
47
+ }
48
+
49
+ export interface CommandLog {
50
+ Player: ErlcPlayer;
51
+ Timestamp: number; // Timestamp in seconds
52
+ Command: string;
53
+ }
54
+
55
+ export interface ModcallLog {
56
+ Caller: ErlcPlayer;
57
+ Moderator?: ErlcPlayer; // If call is unanswered property is undefined
58
+ Timestamp: number; // Timestamp in seconds
59
+ }
60
+
61
+ export type ServerBan = Record<PlayerId, PlayerName>;
62
+
63
+ export interface VehiclesLog {
64
+ Texture: string | null;
65
+ Name: string;
66
+ Owner: ErlcPlayer;
67
+ }
68
+
69
+ export interface ServerStaff {
70
+ CoOwners: number[];
71
+ Admins: Record<string, string>;
72
+ Mods: Record<string, string>;
73
+ }
74
+
75
+ export interface VSMCommandBody {
76
+ command: string; // ":h Hey everyone!"
77
+ }
78
+
79
+ export function getBans(serverToken: string): Promise<ServerBan>;
80
+ export function getCommandLogs(serverToken: string): Promise<CommandLog[]>;
81
+ export function getJoinLogs(serverToken: string): Promise<JoinLog[]>;
82
+ export function getKillLogs(serverToken: string): Promise<KillLog[]>;
83
+ export function getModcallLogs(serverToken: string): Promise<ModcallLog[]>;
84
+ export function getPlayers(serverToken: string): Promise<ServerPlayer[]>;
85
+ export function getQueue(serverToken: string): Promise<number[]>;
86
+ export function getServer(serverToken: string): Promise<ServerStatus>;
87
+ export function getStaff(serverToken: string): Promise<ServerStaff>;
88
+ export function getVehicles(serverToken: string): Promise<VehiclesLog[]>;
89
+ export function runCommand(
90
+ serverToken: string,
91
+ command: string
92
+ ): Promise<boolean>;
93
+
94
+ export class Client {
95
+ constructor(options: ClientConfig);
96
+ config(): ClientConfig;
97
+ }
98
+ }