Skip to content

Commit 7cd0dcc

Browse files
committed
🔨 refactor
1 parent a4a433d commit 7cd0dcc

File tree

8 files changed

+96
-179
lines changed

8 files changed

+96
-179
lines changed

src/app.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ SchemaManager.prototype.getSchema = function (callback): void {
6969
this.setSchema(schema, true);
7070
callback(null, this.schema);
7171
})
72-
.catch(err => {
73-
callback(err);
74-
});
72+
.catch(err => callback(err));
7573
};
7674
/*eslint-enable */
7775

src/classes/Autokeys/Autokeys.ts

Lines changed: 79 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ export default class Autokeys {
124124

125125
check(): void {
126126
log.debug(`checking autokeys (Enabled: ${String(this.isEnabled)})`);
127-
if (this.isEnabled === false) {
128-
return;
129-
}
127+
if (this.isEnabled === false) return;
130128

131129
const userPure = this.userPure;
132130

@@ -319,7 +317,6 @@ export default class Autokeys {
319317
if (setMaxKeys === currKeys) {
320318
setMaxKeys += 1;
321319
}
322-
323320
if (currKeys >= setMaxKeys) {
324321
setMaxKeys = currKeys + 1;
325322
}
@@ -354,152 +351,107 @@ export default class Autokeys {
354351
rKeysCanSell !== this.oldAmount.keysCanSell ||
355352
currKeys !== this.oldAmount.ofKeys);
356353

357-
if (this.isActive) {
358-
// if Autokeys already running
354+
const alertLowPure = () => {
355+
const msg = 'I am now low on both keys and refs.';
356+
if (opt.sendAlert.enable && opt.sendAlert.autokeys.lowPure) {
357+
if (opt.discordWebhook.sendAlert.enable && opt.discordWebhook.sendAlert.url.main !== '')
358+
sendAlert('lowPure', this.bot, msg);
359+
else this.bot.messageAdmins(msg, []);
360+
}
361+
};
362+
363+
const common1 = () => {
364+
this.setOverallStatus = [false, true, false, true, false, false];
365+
this.setOldAmount = [0, 0, rKeysCanBankMin, rKeysCanBankMax, currKeys];
366+
this.setActiveStatus = true;
367+
};
368+
369+
const common2 = () => {
370+
this.setOverallStatus = [true, false, false, false, true, false];
371+
this.setOldAmount = [0, rKeysCanBankMax, 0, 0, currKeys];
372+
this.setActiveStatus = true;
373+
};
374+
375+
const common3 = () => {
376+
this.setOverallStatus = [true, false, false, false, true, false];
377+
this.setOldAmount = [0, rKeysCanBuy, 0, 0, currKeys];
378+
this.setActiveStatus = true;
379+
};
380+
381+
const common4 = () => {
382+
this.setOverallStatus = [false, false, false, false, false, true];
383+
this.setOldAmount = [rKeysCanSell, 0, 0, 0, currKeys];
384+
this.setActiveStatus = true;
385+
};
386+
387+
const common5 = () => {
388+
this.setOverallStatus = [false, false, true, false, false, false];
389+
this.setActiveStatus = false;
390+
alertLowPure();
391+
};
392+
393+
const isNotInPricelist = this.bot.pricelist.getPrice({ priceKey: '5021;6', onlyEnabled: false }) === null;
394+
395+
if (this.isActive || (!this.isActive && !isNotInPricelist)) {
396+
// if Autokeys already running OR Not running and exist in pricelist
359397
if (isBanking) {
360398
// enable keys banking - if banking conditions to enable banking matched and banking is enabled
361-
this.setOverallStatus = [false, true, false, true, false, false];
362-
this.setOldAmount = [0, 0, rKeysCanBankMin, rKeysCanBankMax, currKeys];
363-
this.setActiveStatus = true;
399+
common1();
364400
this.updateToBank(setMinKeys, setMaxKeys, currKeyPrice);
365401
//
366402
} else if (isTooManyRefWhileBanking) {
367403
// enable keys banking - if refs > minRefs but Keys < minKeys, will buy keys.
368-
this.setOverallStatus = [true, false, false, false, true, false];
369-
this.setOldAmount = [0, rKeysCanBankMax, 0, 0, currKeys];
370-
this.setActiveStatus = true;
404+
common2();
371405
this.update(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
372406
//
373407
} else if (buyKeys) {
374408
// enable Autokeys - Buying - if buying keys conditions matched
375-
this.setOverallStatus = [true, false, false, false, true, false];
376-
this.setOldAmount = [0, rKeysCanBuy, 0, 0, currKeys];
377-
this.setActiveStatus = true;
409+
common3();
378410
this.update(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
379411
//
380412
} else if (sellKeys) {
381413
// enable Autokeys - Selling - if selling keys conditions matched
382-
this.setOverallStatus = [false, false, false, false, false, true];
383-
this.setOldAmount = [rKeysCanSell, 0, 0, 0, currKeys];
384-
this.setActiveStatus = true;
414+
common4();
385415
this.update(setMinKeys, setMaxKeys, currKeyPrice, 'sell');
386416
//
387-
} else if (isRemoveBankingKeys && isEnableKeyBanking) {
417+
} else if (
418+
this.isActive &&
419+
((isRemoveBankingKeys && isEnableKeyBanking) || (isRemoveAutoKeys && !isEnableKeyBanking))
420+
) {
388421
// disable keys banking - if to conditions to disable banking matched and banking is enabled
389422
this.setOverallStatus = [false, false, false, false, false, false];
390423
this.setActiveStatus = false;
391424
void this.disable(currKeyPrice);
392425
//
393-
} else if (isRemoveAutoKeys && !isEnableKeyBanking) {
394-
// disable Autokeys when conditions to disable Autokeys matched
395-
this.setOverallStatus = [false, false, false, false, false, false];
396-
this.setActiveStatus = false;
397-
void this.disable(currKeyPrice);
398-
//
399426
} else if (isAlertAdmins && !this.status.checkAlertOnLowPure) {
400427
// alert admins when low pure
401-
this.setOverallStatus = [false, false, true, false, false, false];
402-
this.setActiveStatus = false;
403-
404-
const msg = 'I am now low on both keys and refs.';
405-
if (opt.sendAlert.enable && opt.sendAlert.autokeys.lowPure) {
406-
if (opt.discordWebhook.sendAlert.enable && opt.discordWebhook.sendAlert.url.main !== '') {
407-
sendAlert('lowPure', this.bot, msg);
408-
} else {
409-
this.bot.messageAdmins(msg, []);
410-
}
411-
}
428+
common5();
412429
}
413-
} else {
414-
// if Autokeys is not running/disabled
415-
if (this.bot.pricelist.getPrice({ priceKey: '5021;6', onlyEnabled: false }) === null) {
416-
// if Mann Co. Supply Crate Key entry does not exist in the pricelist.json
417-
if (isBankingKeys && isEnableKeyBanking) {
418-
//create new Key entry and enable keys banking - if banking conditions to enable banking matched and banking is enabled
419-
this.setOverallStatus = [false, true, false, true, false, false];
420-
this.setOldAmount = [0, 0, rKeysCanBankMin, rKeysCanBankMax, currKeys];
421-
this.setActiveStatus = true;
422-
this.createToBank(setMinKeys, setMaxKeys, currKeyPrice);
423-
//
424-
} else if (isBankingBuyKeysWithEnoughRefs && isEnableKeyBanking) {
425-
// enable keys banking - if refs > minRefs but Keys < minKeys, will buy keys.
426-
this.setOverallStatus = [true, false, false, false, true, false];
427-
this.setOldAmount = [0, rKeysCanBankMax, 0, 0, currKeys];
428-
this.setActiveStatus = true;
429-
this.create(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
430-
//
431-
} else if (isBuyingKeys) {
432-
// create new Key entry and enable Autokeys - Buying - if buying keys conditions matched
433-
this.setOverallStatus = [true, false, false, false, true, false];
434-
this.setOldAmount = [0, rKeysCanBuy, 0, 0, currKeys];
435-
this.setActiveStatus = true;
436-
this.create(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
437-
//
438-
} else if (isSellingKeys) {
439-
// create new Key entry and enable Autokeys - Selling - if selling keys conditions matched
440-
this.setOverallStatus = [false, false, false, false, false, true];
441-
this.setOldAmount = [rKeysCanSell, 0, 0, 0, currKeys];
442-
this.setActiveStatus = true;
443-
this.create(setMinKeys, setMaxKeys, currKeyPrice, 'sell');
444-
//
445-
} else if (isAlertAdmins && !this.status.checkAlertOnLowPure) {
446-
// alert admins when low pure
447-
this.setOverallStatus = [false, false, true, false, false, false];
448-
this.setActiveStatus = false;
449-
450-
const msg = 'I am now low on both keys and refs.';
451-
if (opt.sendAlert.enable && opt.sendAlert.autokeys.lowPure) {
452-
if (opt.discordWebhook.sendAlert.enable && opt.discordWebhook.sendAlert.url.main !== '') {
453-
sendAlert('lowPure', this.bot, msg);
454-
} else {
455-
this.bot.messageAdmins(msg, []);
456-
}
457-
}
458-
}
459-
} else {
460-
// if Mann Co. Supply Crate Key entry already in the pricelist.json
461-
if (isBanking) {
462-
// enable keys banking - if banking conditions to enable banking matched and banking is enabled
463-
this.setOverallStatus = [false, true, false, true, false, false];
464-
this.setOldAmount = [0, 0, rKeysCanBankMin, rKeysCanBankMax, currKeys];
465-
this.setActiveStatus = true;
466-
this.updateToBank(setMinKeys, setMaxKeys, currKeyPrice);
467-
//
468-
} else if (isTooManyRefWhileBanking) {
469-
// enable keys banking - if refs > minRefs but Keys < minKeys, will buy keys.
470-
this.setOverallStatus = [true, false, false, false, true, false];
471-
this.setOldAmount = [0, rKeysCanBankMax, 0, 0, currKeys];
472-
this.setActiveStatus = true;
473-
this.update(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
474-
//
475-
} else if (buyKeys) {
476-
// enable Autokeys - Buying - if buying keys conditions matched
477-
this.setOverallStatus = [true, false, false, false, true, false];
478-
this.setOldAmount = [0, rKeysCanBuy, 0, 0, currKeys];
479-
this.setActiveStatus = true;
480-
this.update(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
481-
//
482-
} else if (sellKeys) {
483-
// enable Autokeys - Selling - if selling keys conditions matched
484-
this.setOverallStatus = [false, false, false, false, false, true];
485-
this.setOldAmount = [rKeysCanSell, 0, 0, 0, currKeys];
486-
this.setActiveStatus = true;
487-
this.update(setMinKeys, setMaxKeys, currKeyPrice, 'sell');
488-
//
489-
} else if (isAlertAdmins && !this.status.checkAlertOnLowPure) {
490-
// alert admins when low pure
491-
this.setOverallStatus = [false, false, true, false, false, false];
492-
this.setActiveStatus = false;
493-
494-
const msg = 'I am now low on both keys and refs.';
495-
if (opt.sendAlert.enable && opt.sendAlert.autokeys.lowPure) {
496-
if (opt.discordWebhook.sendAlert.enable && opt.discordWebhook.sendAlert.url.main !== '') {
497-
sendAlert('lowPure', this.bot, msg);
498-
} else {
499-
this.bot.messageAdmins(msg, []);
500-
}
501-
}
502-
}
430+
} else if (isNotInPricelist) {
431+
// if Autokeys is not running/disabled AND not exist in pricelist
432+
if (isBankingKeys && isEnableKeyBanking) {
433+
//create new Key entry and enable keys banking - if banking conditions to enable banking matched and banking is enabled
434+
common1();
435+
this.createToBank(setMinKeys, setMaxKeys, currKeyPrice);
436+
//
437+
} else if (isBankingBuyKeysWithEnoughRefs && isEnableKeyBanking) {
438+
// enable keys banking - if refs > minRefs but Keys < minKeys, will buy keys.
439+
common2();
440+
this.create(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
441+
//
442+
} else if (isBuyingKeys) {
443+
// create new Key entry and enable Autokeys - Buying - if buying keys conditions matched
444+
common3();
445+
this.create(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
446+
//
447+
} else if (isSellingKeys) {
448+
// create new Key entry and enable Autokeys - Selling - if selling keys conditions matched
449+
common4();
450+
this.create(setMinKeys, setMaxKeys, currKeyPrice, 'sell');
451+
//
452+
} else if (isAlertAdmins && !this.status.checkAlertOnLowPure) {
453+
// alert admins when low pure
454+
common5();
503455
}
504456
}
505457
log.debug(
@@ -689,11 +641,7 @@ export default class Autokeys {
689641
disable(keyPrices: KeyPrices): Promise<void> {
690642
return new Promise((resolve, reject) => {
691643
const match = this.bot.pricelist.getPrice({ priceKey: '5021;6', onlyEnabled: false });
692-
if (match === null) {
693-
return resolve();
694-
}
695-
696-
if (!match.enabled) {
644+
if (match === null || !match.enabled) {
697645
return resolve();
698646
}
699647

src/classes/Commands/Commands.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,9 +1505,7 @@ function getMptfDashboardItems(mptfApiKey: string, ignorePainted = false): Promi
15051505

15061506
return resolve(toReturn);
15071507
})
1508-
.catch(err => {
1509-
reject(err);
1510-
});
1508+
.catch(err => reject(err));
15111509
});
15121510
}
15131511

src/classes/Friends.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ export default class Friends {
6565
this.maxFriends = base + level * multiplier;
6666
resolve(this.maxFriends);
6767
})
68-
.catch(err => {
69-
return reject(err);
70-
});
68+
.catch(err => reject(err));
7169
});
7270
}
7371
}

src/classes/TF2Inventory.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ export default class TF2Inventory {
118118
this.items = body.result.items;
119119
return resolve();
120120
})
121-
.catch(err => {
122-
return reject(err);
123-
});
121+
.catch(err => reject(err));
124122
});
125123
}
126124

@@ -160,9 +158,7 @@ export default class TF2Inventory {
160158
history: []
161159
});
162160
})
163-
.catch(err => {
164-
return reject(err);
165-
});
161+
.catch(err => reject(err));
166162
});
167163
}
168164
}

src/lib/apiRequest.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,7 @@ export async function apiRequest<B>({
4545

4646
return new Promise((resolve, reject) => {
4747
axios(options)
48-
.then(response => {
49-
const body = response.data as B;
50-
resolve(body);
51-
})
52-
.catch((err: AxiosError) => {
53-
if (err) {
54-
reject(filterAxiosError(err));
55-
}
56-
});
48+
.then(response => resolve(response.data as B))
49+
.catch((err: AxiosError) => reject(filterAxiosError(err)));
5750
});
5851
}

src/lib/bans.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,8 @@ export default class Bans {
226226
return resolve({ isBanned: this._isBptfBanned, content: result.content });
227227
})
228228
.catch(err => {
229-
if (this.showLog) {
230-
log.warn('Failed to get data from backpack.tf');
231-
log.debug(err);
232-
}
229+
log.warn('Failed to get data from backpack.tf');
230+
log.debug(err);
233231
return resolve(undefined);
234232
});
235233
});
@@ -252,10 +250,8 @@ export default class Bans {
252250
return resolve({ isBanned: isSteamRepBanned, content: fullRepInfo });
253251
})
254252
.catch(err => {
255-
if (this.showLog) {
256-
log.warn('Failed to get data from SteamRep');
257-
log.debug(err);
258-
}
253+
log.warn('Failed to get data from SteamRep');
254+
log.debug(err);
259255
if (this._isBptfSteamRepBanned !== null) {
260256
return resolve({ isBanned: this._isBptfSteamRepBanned });
261257
}
@@ -308,7 +304,7 @@ export default class Bans {
308304
return resolve({ isBanned: false });
309305
})
310306
.catch(err => {
311-
if (this.showLog) log.warn('Failed to get data from Marketplace.tf', err);
307+
log.warn('Failed to get data from Marketplace.tf', err);
312308
return resolve(undefined);
313309
});
314310
});
@@ -338,12 +334,8 @@ export default class Bans {
338334
if (err instanceof AbortSignal && attempt !== 'retry') {
339335
return this.isListedUntrusted('retry');
340336
}
341-
if (err) {
342-
if (this.showLog) {
343-
log.warn('Failed to get data from Github');
344-
log.debug(err);
345-
}
346-
}
337+
log.warn('Failed to get data from Github');
338+
log.debug(err);
347339
resolve(undefined);
348340
});
349341
});

0 commit comments

Comments
 (0)