Skip to content

Commit

Permalink
🔨 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
idinium96 committed May 19, 2024
1 parent a4a433d commit 7cd0dcc
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 179 deletions.
4 changes: 1 addition & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ SchemaManager.prototype.getSchema = function (callback): void {
this.setSchema(schema, true);
callback(null, this.schema);
})
.catch(err => {
callback(err);
});
.catch(err => callback(err));
};
/*eslint-enable */

Expand Down
210 changes: 79 additions & 131 deletions src/classes/Autokeys/Autokeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ export default class Autokeys {

check(): void {
log.debug(`checking autokeys (Enabled: ${String(this.isEnabled)})`);
if (this.isEnabled === false) {
return;
}
if (this.isEnabled === false) return;

const userPure = this.userPure;

Expand Down Expand Up @@ -319,7 +317,6 @@ export default class Autokeys {
if (setMaxKeys === currKeys) {
setMaxKeys += 1;
}

if (currKeys >= setMaxKeys) {
setMaxKeys = currKeys + 1;
}
Expand Down Expand Up @@ -354,152 +351,107 @@ export default class Autokeys {
rKeysCanSell !== this.oldAmount.keysCanSell ||
currKeys !== this.oldAmount.ofKeys);

if (this.isActive) {
// if Autokeys already running
const alertLowPure = () => {
const msg = 'I am now low on both keys and refs.';
if (opt.sendAlert.enable && opt.sendAlert.autokeys.lowPure) {
if (opt.discordWebhook.sendAlert.enable && opt.discordWebhook.sendAlert.url.main !== '')
sendAlert('lowPure', this.bot, msg);
else this.bot.messageAdmins(msg, []);
}
};

const common1 = () => {
this.setOverallStatus = [false, true, false, true, false, false];
this.setOldAmount = [0, 0, rKeysCanBankMin, rKeysCanBankMax, currKeys];
this.setActiveStatus = true;
};

const common2 = () => {
this.setOverallStatus = [true, false, false, false, true, false];
this.setOldAmount = [0, rKeysCanBankMax, 0, 0, currKeys];
this.setActiveStatus = true;
};

const common3 = () => {
this.setOverallStatus = [true, false, false, false, true, false];
this.setOldAmount = [0, rKeysCanBuy, 0, 0, currKeys];
this.setActiveStatus = true;
};

const common4 = () => {
this.setOverallStatus = [false, false, false, false, false, true];
this.setOldAmount = [rKeysCanSell, 0, 0, 0, currKeys];
this.setActiveStatus = true;
};

const common5 = () => {
this.setOverallStatus = [false, false, true, false, false, false];
this.setActiveStatus = false;
alertLowPure();
};

const isNotInPricelist = this.bot.pricelist.getPrice({ priceKey: '5021;6', onlyEnabled: false }) === null;

if (this.isActive || (!this.isActive && !isNotInPricelist)) {
// if Autokeys already running OR Not running and exist in pricelist
if (isBanking) {
// enable keys banking - if banking conditions to enable banking matched and banking is enabled
this.setOverallStatus = [false, true, false, true, false, false];
this.setOldAmount = [0, 0, rKeysCanBankMin, rKeysCanBankMax, currKeys];
this.setActiveStatus = true;
common1();
this.updateToBank(setMinKeys, setMaxKeys, currKeyPrice);
//
} else if (isTooManyRefWhileBanking) {
// enable keys banking - if refs > minRefs but Keys < minKeys, will buy keys.
this.setOverallStatus = [true, false, false, false, true, false];
this.setOldAmount = [0, rKeysCanBankMax, 0, 0, currKeys];
this.setActiveStatus = true;
common2();
this.update(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
//
} else if (buyKeys) {
// enable Autokeys - Buying - if buying keys conditions matched
this.setOverallStatus = [true, false, false, false, true, false];
this.setOldAmount = [0, rKeysCanBuy, 0, 0, currKeys];
this.setActiveStatus = true;
common3();
this.update(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
//
} else if (sellKeys) {
// enable Autokeys - Selling - if selling keys conditions matched
this.setOverallStatus = [false, false, false, false, false, true];
this.setOldAmount = [rKeysCanSell, 0, 0, 0, currKeys];
this.setActiveStatus = true;
common4();
this.update(setMinKeys, setMaxKeys, currKeyPrice, 'sell');
//
} else if (isRemoveBankingKeys && isEnableKeyBanking) {
} else if (
this.isActive &&
((isRemoveBankingKeys && isEnableKeyBanking) || (isRemoveAutoKeys && !isEnableKeyBanking))
) {
// disable keys banking - if to conditions to disable banking matched and banking is enabled
this.setOverallStatus = [false, false, false, false, false, false];
this.setActiveStatus = false;
void this.disable(currKeyPrice);
//
} else if (isRemoveAutoKeys && !isEnableKeyBanking) {
// disable Autokeys when conditions to disable Autokeys matched
this.setOverallStatus = [false, false, false, false, false, false];
this.setActiveStatus = false;
void this.disable(currKeyPrice);
//
} else if (isAlertAdmins && !this.status.checkAlertOnLowPure) {
// alert admins when low pure
this.setOverallStatus = [false, false, true, false, false, false];
this.setActiveStatus = false;

const msg = 'I am now low on both keys and refs.';
if (opt.sendAlert.enable && opt.sendAlert.autokeys.lowPure) {
if (opt.discordWebhook.sendAlert.enable && opt.discordWebhook.sendAlert.url.main !== '') {
sendAlert('lowPure', this.bot, msg);
} else {
this.bot.messageAdmins(msg, []);
}
}
common5();
}
} else {
// if Autokeys is not running/disabled
if (this.bot.pricelist.getPrice({ priceKey: '5021;6', onlyEnabled: false }) === null) {
// if Mann Co. Supply Crate Key entry does not exist in the pricelist.json
if (isBankingKeys && isEnableKeyBanking) {
//create new Key entry and enable keys banking - if banking conditions to enable banking matched and banking is enabled
this.setOverallStatus = [false, true, false, true, false, false];
this.setOldAmount = [0, 0, rKeysCanBankMin, rKeysCanBankMax, currKeys];
this.setActiveStatus = true;
this.createToBank(setMinKeys, setMaxKeys, currKeyPrice);
//
} else if (isBankingBuyKeysWithEnoughRefs && isEnableKeyBanking) {
// enable keys banking - if refs > minRefs but Keys < minKeys, will buy keys.
this.setOverallStatus = [true, false, false, false, true, false];
this.setOldAmount = [0, rKeysCanBankMax, 0, 0, currKeys];
this.setActiveStatus = true;
this.create(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
//
} else if (isBuyingKeys) {
// create new Key entry and enable Autokeys - Buying - if buying keys conditions matched
this.setOverallStatus = [true, false, false, false, true, false];
this.setOldAmount = [0, rKeysCanBuy, 0, 0, currKeys];
this.setActiveStatus = true;
this.create(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
//
} else if (isSellingKeys) {
// create new Key entry and enable Autokeys - Selling - if selling keys conditions matched
this.setOverallStatus = [false, false, false, false, false, true];
this.setOldAmount = [rKeysCanSell, 0, 0, 0, currKeys];
this.setActiveStatus = true;
this.create(setMinKeys, setMaxKeys, currKeyPrice, 'sell');
//
} else if (isAlertAdmins && !this.status.checkAlertOnLowPure) {
// alert admins when low pure
this.setOverallStatus = [false, false, true, false, false, false];
this.setActiveStatus = false;

const msg = 'I am now low on both keys and refs.';
if (opt.sendAlert.enable && opt.sendAlert.autokeys.lowPure) {
if (opt.discordWebhook.sendAlert.enable && opt.discordWebhook.sendAlert.url.main !== '') {
sendAlert('lowPure', this.bot, msg);
} else {
this.bot.messageAdmins(msg, []);
}
}
}
} else {
// if Mann Co. Supply Crate Key entry already in the pricelist.json
if (isBanking) {
// enable keys banking - if banking conditions to enable banking matched and banking is enabled
this.setOverallStatus = [false, true, false, true, false, false];
this.setOldAmount = [0, 0, rKeysCanBankMin, rKeysCanBankMax, currKeys];
this.setActiveStatus = true;
this.updateToBank(setMinKeys, setMaxKeys, currKeyPrice);
//
} else if (isTooManyRefWhileBanking) {
// enable keys banking - if refs > minRefs but Keys < minKeys, will buy keys.
this.setOverallStatus = [true, false, false, false, true, false];
this.setOldAmount = [0, rKeysCanBankMax, 0, 0, currKeys];
this.setActiveStatus = true;
this.update(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
//
} else if (buyKeys) {
// enable Autokeys - Buying - if buying keys conditions matched
this.setOverallStatus = [true, false, false, false, true, false];
this.setOldAmount = [0, rKeysCanBuy, 0, 0, currKeys];
this.setActiveStatus = true;
this.update(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
//
} else if (sellKeys) {
// enable Autokeys - Selling - if selling keys conditions matched
this.setOverallStatus = [false, false, false, false, false, true];
this.setOldAmount = [rKeysCanSell, 0, 0, 0, currKeys];
this.setActiveStatus = true;
this.update(setMinKeys, setMaxKeys, currKeyPrice, 'sell');
//
} else if (isAlertAdmins && !this.status.checkAlertOnLowPure) {
// alert admins when low pure
this.setOverallStatus = [false, false, true, false, false, false];
this.setActiveStatus = false;

const msg = 'I am now low on both keys and refs.';
if (opt.sendAlert.enable && opt.sendAlert.autokeys.lowPure) {
if (opt.discordWebhook.sendAlert.enable && opt.discordWebhook.sendAlert.url.main !== '') {
sendAlert('lowPure', this.bot, msg);
} else {
this.bot.messageAdmins(msg, []);
}
}
}
} else if (isNotInPricelist) {
// if Autokeys is not running/disabled AND not exist in pricelist
if (isBankingKeys && isEnableKeyBanking) {
//create new Key entry and enable keys banking - if banking conditions to enable banking matched and banking is enabled
common1();
this.createToBank(setMinKeys, setMaxKeys, currKeyPrice);
//
} else if (isBankingBuyKeysWithEnoughRefs && isEnableKeyBanking) {
// enable keys banking - if refs > minRefs but Keys < minKeys, will buy keys.
common2();
this.create(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
//
} else if (isBuyingKeys) {
// create new Key entry and enable Autokeys - Buying - if buying keys conditions matched
common3();
this.create(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
//
} else if (isSellingKeys) {
// create new Key entry and enable Autokeys - Selling - if selling keys conditions matched
common4();
this.create(setMinKeys, setMaxKeys, currKeyPrice, 'sell');
//
} else if (isAlertAdmins && !this.status.checkAlertOnLowPure) {
// alert admins when low pure
common5();
}
}
log.debug(
Expand Down Expand Up @@ -689,11 +641,7 @@ export default class Autokeys {
disable(keyPrices: KeyPrices): Promise<void> {
return new Promise((resolve, reject) => {
const match = this.bot.pricelist.getPrice({ priceKey: '5021;6', onlyEnabled: false });
if (match === null) {
return resolve();
}

if (!match.enabled) {
if (match === null || !match.enabled) {
return resolve();
}

Expand Down
4 changes: 1 addition & 3 deletions src/classes/Commands/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1505,9 +1505,7 @@ function getMptfDashboardItems(mptfApiKey: string, ignorePainted = false): Promi

return resolve(toReturn);
})
.catch(err => {
reject(err);
});
.catch(err => reject(err));
});
}

Expand Down
4 changes: 1 addition & 3 deletions src/classes/Friends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ export default class Friends {
this.maxFriends = base + level * multiplier;
resolve(this.maxFriends);
})
.catch(err => {
return reject(err);
});
.catch(err => reject(err));
});
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/classes/TF2Inventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ export default class TF2Inventory {
this.items = body.result.items;
return resolve();
})
.catch(err => {
return reject(err);
});
.catch(err => reject(err));
});
}

Expand Down Expand Up @@ -160,9 +158,7 @@ export default class TF2Inventory {
history: []
});
})
.catch(err => {
return reject(err);
});
.catch(err => reject(err));
});
}
}
Expand Down
11 changes: 2 additions & 9 deletions src/lib/apiRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,7 @@ export async function apiRequest<B>({

return new Promise((resolve, reject) => {
axios(options)
.then(response => {
const body = response.data as B;
resolve(body);
})
.catch((err: AxiosError) => {
if (err) {
reject(filterAxiosError(err));
}
});
.then(response => resolve(response.data as B))
.catch((err: AxiosError) => reject(filterAxiosError(err)));
});
}
22 changes: 7 additions & 15 deletions src/lib/bans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,8 @@ export default class Bans {
return resolve({ isBanned: this._isBptfBanned, content: result.content });
})
.catch(err => {
if (this.showLog) {
log.warn('Failed to get data from backpack.tf');
log.debug(err);
}
log.warn('Failed to get data from backpack.tf');
log.debug(err);
return resolve(undefined);
});
});
Expand All @@ -252,10 +250,8 @@ export default class Bans {
return resolve({ isBanned: isSteamRepBanned, content: fullRepInfo });
})
.catch(err => {
if (this.showLog) {
log.warn('Failed to get data from SteamRep');
log.debug(err);
}
log.warn('Failed to get data from SteamRep');
log.debug(err);
if (this._isBptfSteamRepBanned !== null) {
return resolve({ isBanned: this._isBptfSteamRepBanned });
}
Expand Down Expand Up @@ -308,7 +304,7 @@ export default class Bans {
return resolve({ isBanned: false });
})
.catch(err => {
if (this.showLog) log.warn('Failed to get data from Marketplace.tf', err);
log.warn('Failed to get data from Marketplace.tf', err);
return resolve(undefined);
});
});
Expand Down Expand Up @@ -338,12 +334,8 @@ export default class Bans {
if (err instanceof AbortSignal && attempt !== 'retry') {
return this.isListedUntrusted('retry');
}
if (err) {
if (this.showLog) {
log.warn('Failed to get data from Github');
log.debug(err);
}
}
log.warn('Failed to get data from Github');
log.debug(err);
resolve(undefined);
});
});
Expand Down
Loading

0 comments on commit 7cd0dcc

Please sign in to comment.