Skip to content

Commit c9a1bf6

Browse files
committed
Webhooks Fixes
1 parent 94a1dcf commit c9a1bf6

File tree

5 files changed

+356
-246
lines changed

5 files changed

+356
-246
lines changed

README.md

Lines changed: 139 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212

1313
A powerful and easy-to-use JavaScript/TypeScript package to interact with the [MCSS (MC Server Soft) API](https://www.mcserversoft.com/). This library provides a complete interface for managing Minecraft servers, users, backups, scheduled tasks, API keys, and webhooks.
1414

15+
## 🔗 Useful Links
16+
17+
- **Website**: [https://mcserversoft.com](https://mcserversoft.com)
18+
- **MCSS Documentation**: [MCSS Documentation](https://docs.mcserversoft.com/)
19+
- **MCSS API Documentation**: [API Documentation](https://docs.mcserversoft.com/apis/v2)
20+
- **Documentation**: [https://mcserversoft-community.github.io/mcss-api-js/](https://mcserversoft-community.github.io/mcss-api-js/)
21+
- **GitHub**: [https://github.com/mcserversoft-community/mcss-api-js](https://github.com/mcserversoft-community/mcss-api-js)
22+
1523
## 📦 Installation
1624

1725
You can install the package using your preferred package manager:
@@ -35,88 +43,118 @@ import { Client } from '@mcserversoft/mcss-api';
3543
const client = new Client("127.0.0.1", 8080, "YOUR_API_KEY", true);
3644
```
3745

38-
### 🎮 Server Management
46+
The `client` object is your main entry point and provides access to:
47+
48+
- **Modules**:
49+
- `client.servers`: Manage servers.
50+
- `client.users`: Manage users.
51+
- `client.webhooks`: Manage webhooks.
52+
- `client.apikeys`: Manage API keys.
53+
- **Direct Methods**:
54+
- `client.getStats()`: Get panel stats.
55+
- `client.getServers()`: Get all servers.
56+
- `client.getServerCount()`: Get server count.
57+
- `client.getSettings()`: Get MCSS settings.
58+
- `client.updateSettings()`: Update MCSS settings.
59+
60+
## ✨ Builder Classes
61+
62+
This library includes builder classes to make creating complex objects straightforward. These builders use a fluent interface, allowing you to chain methods together.
3963

40-
#### Get all servers
64+
- `Server`: For creating new server configurations.
65+
- `User`: For creating new users.
66+
- `Backup`: For creating new backup configurations.
67+
- `Task`: For creating new scheduled tasks.
68+
- `Key`: For creating new API keys.
69+
70+
### Example Usage of a Builder
4171

4272
```javascript
43-
async function getServers() {
44-
const servers = await client.getServers();
45-
if (Array.isArray(servers)) {
46-
servers.forEach(server => {
47-
console.log(`Server: ${server.name} (${server.status})`);
48-
console.log(`Players: ${server.players}/${server.maxPlayers}`);
49-
});
50-
}
51-
}
73+
import { Backup, Compression } from '@mcserversoft/mcss-api';
5274

53-
getServers();
75+
const newBackup = new Backup("My Important Backup", "local_destination")
76+
.setCompression(Compression.HIGH)
77+
.setSuspend(true)
78+
.setDeleteOldBackups(true);
79+
80+
// You can then use this `newBackup` object in the API methods.
81+
// For example:
82+
// await server.backups.create(newBackup);
5483
```
5584

56-
#### Get a specific server
85+
## 🎮 Server Management
86+
87+
When you fetch a server, you get a `ServerObject` which has its own methods and properties.
88+
89+
### The `ServerObject`
90+
91+
A `ServerObject` contains all the information about a server, as well as instance-specific modules for backups and scheduling.
92+
93+
- **Properties**: `id`, `name`, `status`, `players`, `maxPlayers`, etc.
94+
- **Modules**:
95+
- `server.backups`: Manage backups for this specific server.
96+
- `server.scheduler`: Manage scheduled tasks for this specific server.
97+
- **Methods**:
98+
- `server.getStats()`: Get server-specific stats.
99+
- `server.execute()`: Execute commands or actions.
100+
- `server.edit()`: Edit the server's properties.
101+
102+
### Get a specific server
57103

58104
```javascript
59105
async function getServer() {
60-
const response = await client.servers.get("YOUR_SERVER_ID");
61-
if (response.status === 200) {
62-
console.log(`Server data:`, response.data);
106+
const server = await client.servers.get("YOUR_SERVER_ID");
107+
if (server.status === 200) {
108+
console.log(`Server Name: ${server.name}`);
109+
console.log(`Status: ${server.status}`);
110+
111+
// You can access backups and scheduler directly
112+
const backups = await server.backups.get();
113+
console.log('Backups:', backups.data);
63114
}
64115
}
65116

66117
getServer();
67118
```
68119

69-
#### Control server operations
120+
### Control server operations
70121

71122
```javascript
72123
import { ServerAction } from '@mcserversoft/mcss-api';
73124

74125
async function manageServer() {
75-
// First get all servers to find the one we want
76-
const servers = await client.getServers();
77-
if (Array.isArray(servers) && servers.length > 0) {
78-
const server = servers[0]; // Use the first server as example
79-
126+
const server = await client.servers.get("YOUR_SERVER_ID");
127+
if (server.status === 200) {
80128
// Start the server
81129
const startResult = await server.execute(ServerAction.Start);
82130
console.log('Start result:', startResult.status);
83131

84132
// Send a command
85133
const commandResult = await server.execute("say Hello from the API!");
86134
console.log('Command result:', commandResult.status);
87-
88-
// Stop the server
89-
const stopResult = await server.execute(ServerAction.Stop);
90-
console.log('Stop result:', stopResult.status);
91135
}
92136
}
93137

94138
manageServer();
95139
```
96140

97-
### 👥 User Management
141+
## 👥 User Management
98142

99143
```javascript
144+
import { User } from '@mcserversoft/mcss-api';
145+
100146
async function manageUsers() {
101147
// Get all users
102148
const usersResponse = await client.users.get();
103149
if (usersResponse.status === 200) {
104150
console.log('Users:', usersResponse.data);
105151
}
106152

107-
// Get a specific user
108-
const userResponse = await client.users.getUser("USER_ID");
109-
if (userResponse.status === 200) {
110-
console.log('User:', userResponse.data);
111-
}
153+
// Create a new user using the User builder
154+
const newUser = new User("testuser", "password123", true, false, false)
155+
.setUsername("new-test-user")
156+
.setPassword("securePassword123");
112157

113-
// Create a new user
114-
const newUser = {
115-
username: "testuser",
116-
password: "password123",
117-
email: "test@example.com",
118-
role: "user"
119-
};
120158
const createdUser = await client.users.createUser(newUser);
121159
if (createdUser.status === 200) {
122160
console.log('User created:', createdUser.data);
@@ -126,27 +164,26 @@ async function manageUsers() {
126164
manageUsers();
127165
```
128166

129-
### 💾 Backup Management
167+
## 💾 Backup Management
130168

131169
```javascript
170+
import { Backup, Compression } from '@mcserversoft/mcss-api';
171+
132172
async function manageBackups() {
133-
// First get all servers to find the one we want
134-
const servers = await client.getServers();
135-
if (Array.isArray(servers) && servers.length > 0) {
136-
const serverId = servers[0].id; // Use the first server's ID
137-
173+
const server = await client.servers.get("YOUR_SERVER_ID");
174+
if (server.status === 200) {
138175
// Get all backups for a server
139-
const backupsResponse = await servers[0].backups.get();
176+
const backupsResponse = await server.backups.get();
140177
if (backupsResponse.status === 200) {
141178
console.log('Backups:', backupsResponse.data);
142179
}
143180

144-
// Create a new backup
145-
const newBackup = {
146-
name: "My Backup",
147-
destination: "local"
148-
};
149-
const createdBackup = await servers[0].backups.create(newBackup);
181+
// Create a new backup using the Backup builder
182+
const newBackup = new Backup("My New Backup", "local")
183+
.setCompression(Compression.HIGH)
184+
.setSuspend(true);
185+
186+
const createdBackup = await server.backups.create(newBackup);
150187
if (createdBackup.status === 200) {
151188
console.log('Backup created:', createdBackup.data);
152189
}
@@ -156,26 +193,26 @@ async function manageBackups() {
156193
manageBackups();
157194
```
158195

159-
### ⏰ Scheduled Tasks
196+
## ⏰ Scheduled Tasks
160197

161198
```javascript
199+
import { Task, ServerAction } from '@mcserversoft/mcss-api';
200+
162201
async function manageTasks() {
163-
// First get all servers to find the one we want
164-
const servers = await client.getServers();
165-
if (Array.isArray(servers) && servers.length > 0) {
202+
const server = await client.servers.get("YOUR_SERVER_ID");
203+
if (server.status === 200) {
166204
// Get all tasks for a server
167-
const tasksResponse = await servers[0].scheduler.getTasks();
205+
const tasksResponse = await server.scheduler.getTasks();
168206
if (tasksResponse.status === 200) {
169207
console.log('Tasks:', tasksResponse.data);
170208
}
171209

172-
// Create a new scheduled task
173-
const newTask = {
174-
name: "Daily Restart",
175-
action: "restart",
176-
schedule: "0 6 * * *" // Daily at 6 AM
177-
};
178-
const createdTask = await servers[0].scheduler.create(newTask);
210+
// Create a new scheduled task using the Task builder
211+
const newTask = new Task("Daily Restart", true, 0)
212+
.setTiming(true, 86400) // Repeat every 24 hours
213+
.addJob(ServerAction.Restart);
214+
215+
const createdTask = await server.scheduler.create(newTask);
179216
if (createdTask.status === 200) {
180217
console.log('Task created:', createdTask.data);
181218
}
@@ -185,7 +222,7 @@ async function manageTasks() {
185222
manageTasks();
186223
```
187224

188-
### 🔑 API Key Management
225+
## 🔑 API Key Management
189226

190227
```javascript
191228
import { Key, ServerPermissions } from '@mcserversoft/mcss-api';
@@ -197,16 +234,15 @@ async function manageApiKeys() {
197234
console.log('API Keys:', keysResponse.data);
198235
}
199236

200-
// Create a new API key
201-
const newKey = new Key(
202-
"My API Key",
203-
[
204-
{
205-
serverId: "YOUR_SERVER_ID",
206-
permissions: [ServerPermissions.Start, ServerPermissions.Stop]
207-
}
208-
]
209-
);
237+
// Create a new API key using the Key builder
238+
const newKey = new Key("My Test Key")
239+
.setIsAdmin(false)
240+
.setHasAccessToAllServers(false)
241+
.addCustomServerPermission("YOUR_SERVER_ID", {
242+
[ServerPermissions.VIEW_STATS]: true,
243+
[ServerPermissions.USE_CONSOLE]: true,
244+
});
245+
210246
const createdKey = await client.apikeys.create(newKey);
211247
if (createdKey.status === 200) {
212248
console.log('API Key created:', createdKey.data);
@@ -216,7 +252,7 @@ async function manageApiKeys() {
216252
manageApiKeys();
217253
```
218254

219-
### 🪝 Webhook Management
255+
## 🪝 Webhook Management
220256

221257
```javascript
222258
import { WebhookTrigger } from '@mcserversoft/mcss-api';
@@ -228,13 +264,13 @@ async function manageWebhooks() {
228264
console.log('Webhooks:', webhooksResponse.data);
229265
}
230266

231-
// Create a new webhook
232-
const createdWebhook = await client.webhooks.create(
233-
"My Webhook",
234-
"https://example.com/webhook",
235-
0,
236-
[WebhookTrigger.SERVER_STATUS_CHANGED]
237-
);
267+
// Create a new webhook using the Webhook builder
268+
const newWebhook = new Webhook("My Webhook", "https://example.com/webhook")
269+
.setMessageFormat(0) // 0 for JSON
270+
.setWebhookTriggers([WebhookTrigger.SERVER_STATUS_CHANGED])
271+
.addOptionalHeader("X-Custom-Header", "SomeValue");
272+
273+
const createdWebhook = await client.webhooks.create(newWebhook);
238274
if (createdWebhook.status === 200) {
239275
console.log('Webhook created:', createdWebhook.data);
240276
}
@@ -243,6 +279,24 @@ async function manageWebhooks() {
243279
manageWebhooks();
244280
```
245281

282+
## 📚 API Reference
283+
284+
### Enums
285+
286+
The library exports several enums to make working with specific values easier:
287+
288+
- `GB`: Memory allocation for servers (e.g., `GB.ONE`).
289+
- `Compression`: Backup compression levels (`HIGH`, `LOW`, `NONE`).
290+
- `KeepOnline`: Server keep-online strategies.
291+
- `ServerAction`: Actions like `Start`, `Stop`, `Restart`.
292+
- `ServerFilter`: Filters for listing servers.
293+
- `ServerCountFilter`: Filters for counting servers.
294+
- `ServerType`: Specific server types (e.g., `VANILLA`, `PAPER`).
295+
- `TaskFilter`: Filters for scheduled tasks.
296+
- `WebhookTrigger`: Events that trigger webhooks.
297+
- `WebhookMessageFormat`: Message formats for webhooks (e.g., `GENERAL`, `DISCORD`).
298+
- `ServerPermissions`: Permissions for API keys.
299+
246300
## 📚 API Documentation
247301

248302
For comprehensive API documentation with detailed examples, type definitions, and method signatures, visit our [TypeDoc documentation](https://mcserversoft-community.github.io/mcss-api-js/).
@@ -254,17 +308,6 @@ To generate the documentation locally, run:
254308
```bash
255309
npm run docs
256310
```
257-
258-
## 🤝 Contributing
259-
260-
We welcome contributions! Here's how you can help:
261-
262-
1. 🍴 **Fork** the repository
263-
2. 🌿 **Create** your feature branch (`git checkout -b feature/amazing-feature`)
264-
3.**Commit** your changes (`git commit -m 'Add some amazing feature'`)
265-
4. 📤 **Push** to the branch (`git push origin feature/amazing-feature`)
266-
5. 🔀 **Open** a Pull Request
267-
268311
### Development Setup
269312

270313
```bash
@@ -283,7 +326,7 @@ npm test
283326

284327
## 📋 Requirements
285328

286-
- Node.js 16.x or higher
329+
- Node.js 18.x or higher
287330
- MCSS (MC Server Soft) instance with API enabled
288331
- Valid API key with appropriate permissions
289332

@@ -304,4 +347,4 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
304347

305348
[Website](https://mcserversoft.com)[GitHub](https://github.com/mcserversoft-community)[Documentation](https://mcserversoft-community.github.io/mcss-api-js/)
306349

307-
</div>
350+
</div>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mcserversoft/mcss-api",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"description": "Simple package to interact with MCSS's API",
55
"license": "MIT",
66
"main": "dist/index.js",

0 commit comments

Comments
 (0)