forked from Dom-in/ASF-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregcmd.js
213 lines (194 loc) · 6.01 KB
/
regcmd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const { REST, Routes } = require('discord.js');
const config = require('./configs/config.json');
const fetch = require('node-fetch');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
async function fetchBots() {
try {
await waitForASFOnline();
const response = await fetch(`${config.security.SSL_STAT}://${config.security.IP}/Api/Bot/ASF`, {
method: "get",
headers: {
"Content-Type": "application/json",
Authentication: config.security.IPC_PASSWORD,
},
});
const body = await response.json();
if (body.Success) {
return Object.keys(body.Result);
}
throw new Error("Failed to fetch bot names");
} catch (error) {
console.error("Fetch error:", error);
return [];
}
}
async function waitForASFOnline() {
return new Promise(async (resolve, reject) => {
const checkInterval = setInterval(async () => {
try {
const response = await fetch(`${config.security.SSL_STAT}://${config.security.IP}/HealthCheck`, { method: "get" });
if (response.status === 200) {
clearInterval(checkInterval);
resolve();
} else if (response.status === 502) {
console.log(`No running Service`);
} else {
console.log(`Unexpected status: ${response.status}`);
}
} catch (error) {
if (error.code === "ETIMEDOUT" || error.code === "ECONNREFUSED") {
console.log(`Server is offline or Service is booting`);
} else {
console.error("Fetch error:", error);
clearInterval(checkInterval);
reject(error);
}
}
}, 10000);
});
}
const staticCommands = [
{
"name": "botversion",
"description": "Discord Bot-version"
},
{
"name": "exit",
"description": "Ends ASF Process"
},
{
"name": "restart",
"description": "Restart ASF"
},
{
"name": "update",
"description": "Update ASF"
}
];
async function registerCommands() {
const botNames = await fetchBots();
const basic = ["resume", "start", "stop"].map((command) => ({
"name": command,
"description": `${command.charAt(0).toUpperCase() + command.slice(1)} ASF bot`,
"options": [
{
"name": "botname",
"description": `The bot to ${command}`,
"type": 3,
"required": false,
"choices": botNames.map(bot => ({
name: bot,
value: bot
}))
}
]
}));
const status = {
"name": "status",
"description": `Get status for ASF or bot`,
"options": [
{
"name": "botname",
"description": `The bot to check`,
"type": 3,
"required": false,
"choices": botNames.map(bot => ({
name: bot,
value: bot
}))
}
]
}
const pause = {
"name": "pause",
"description": "Pause bots farming",
"options": [
{
"name": "botname",
"description": "The bot to pause",
"type": 3,
"required": false,
"choices": botNames.map(bot => ({
name: bot,
value: bot
}))
},
{
"name": "time",
"description": "In seconds",
"type": 4,
"required": false
}
]
}
const addLicense = {
"name": "addlicense",
"description": "Add licences to you account",
"options": [
{
"name": "license_ids",
"description": "The ids to add",
"type": 3,
"required": true
},
{
"name": "botname",
"description": "The bot to addlicense",
"type": 3,
"required": false,
"choices": botNames.map(bot => ({
name: bot,
value: bot
}))
}
]
};
const redeemPoints = {
"name": "redeempoints",
"description": "Redeem items from the pointshop",
"options": [
{
"name": "item_ids",
"description": "The ids to redeem",
"type": 3,
"required": true
},
{
"name": "botname",
"description": "The bot to redeem",
"type": 3,
"required": false,
"choices": botNames.map(bot => ({
name: bot,
value: bot
}))
}
]
};
const commands = [...staticCommands, ...basic, pause, addLicense, redeemPoints, status];
const rest = new REST({ version: '10' }).setToken(config.bot.token);
try {
console.log('Started refreshing application (/) commands. (This might take a bit)');
await rest.put(
Routes.applicationGuildCommands(
config.bot.ID,
config.security.SERVER_ID
),
{ body: [] }
);
console.log('Successfully deleted all commands.');
await rest.put(
Routes.applicationGuildCommands(
config.bot.ID,
config.security.SERVER_ID
),
{ body: commands }
);
console.log('Successfully reloaded application (/) commands.');
process.exit(0);
} catch (error) {
console.error(`Error during command registration: ${error}`);
process.exit(1);
}
}
registerCommands();