Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔎 execute pricecheck on add, update, and remove commands #500

Merged
merged 1 commit into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/classes/Commands/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default class Commands {
this.message = new c.MessageCommand(bot);
this.misc = new c.MiscCommands(bot);
this.opt = new c.OptionsCommand(bot);
this.pManager = new c.PricelistManager(bot);
this.pManager = new c.PricelistManager(bot, pricer);
this.request = new c.RequestCommands(bot, pricer);
this.review = new c.ReviewCommands(bot);
this.status = new c.StatusCommands(bot);
Expand Down
74 changes: 72 additions & 2 deletions src/classes/Commands/sub-classes/PricelistManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { removeLinkProtocol, testSKU, getItemFromParams, fixSKU } from '../funct
import Bot from '../../Bot';
import CommandParser from '../../CommandParser';
import { Entry, EntryData, PricelistChangedSource } from '../../Pricelist';
import Pricer, { RequestCheckResponse, RequestCheckFn } from '../../Pricer';
import validator from '../../../lib/validator';
import log from '../../../lib/logger';

Expand All @@ -25,12 +26,15 @@ export default class PricelistManagerCommands {

private executeTimeout: NodeJS.Timeout;

private requestCheck: RequestCheckFn;

stopAutoAddCommand(): void {
this.stopAutoAdd = true;
}

constructor(private readonly bot: Bot) {
constructor(private readonly bot: Bot, private priceSource: Pricer) {
this.bot = bot;
this.requestCheck = this.priceSource.requestCheck.bind(this.priceSource);
}

addCommand(steamID: SteamID, message: string): void {
Expand Down Expand Up @@ -165,6 +169,22 @@ export default class PricelistManagerCommands {
steamID,
`✅ Added "${entry.name}" (${entry.sku})` + this.generateAddedReply(isPremium, entry)
);

void this.requestCheck(params.sku, 'bptf').asCallback(
(err: ErrorRequest, body: RequestCheckResponse) => {
if (err) {
log.debug(`❌ Failed to request pricecheck for ${entry.sku}: ${JSON.stringify(err)}`);
}

if (!body) {
log.debug(
`❌ Error while requesting price check for ${entry.sku} (returned null/undefined).`
);
} else {
log.debug(`✅ Requested pricecheck for ${body.name} (${entry.sku}).`);
}
}
);
})
.catch(err => {
this.bot.sendMessage(steamID, `❌ Failed to add the item to the pricelist: ${(err as Error).message}`);
Expand Down Expand Up @@ -408,6 +428,22 @@ export default class PricelistManagerCommands {
total - added - skipped - failed
} remaining`
);

void this.requestCheck(params.sku, 'bptf').asCallback(
(err: ErrorRequest, body: RequestCheckResponse) => {
if (err) {
log.debug(`❌ Failed to request pricecheck for ${entry.sku}: ${JSON.stringify(err)}`);
}

if (!body) {
log.debug(
`❌ Error while requesting price check for ${entry.sku} (returned null/undefined).`
);
} else {
log.debug(`✅ Requested pricecheck for ${body.name} (${entry.sku}).`);
}
}
);
})
.catch(err => {
failed++;
Expand Down Expand Up @@ -898,6 +934,22 @@ export default class PricelistManagerCommands {
steamID,
`✅ Updated "${entry.name}" (${entry.sku})` + this.generateUpdateReply(isPremium, itemEntry, entry)
);

void this.requestCheck(params.sku, 'bptf').asCallback(
(err: ErrorRequest, body: RequestCheckResponse) => {
if (err) {
log.debug(`❌ Failed to request pricecheck for ${entry.sku}: ${JSON.stringify(err)}`);
}

if (!body) {
log.debug(
`❌ Error while requesting price check for ${entry.sku} (returned null/undefined).`
);
} else {
log.debug(`✅ Requested pricecheck for ${body.name} (${entry.sku}).`);
}
}
);
})
.catch((err: ErrorRequest) => {
this.bot.sendMessage(
Expand Down Expand Up @@ -1137,7 +1189,25 @@ export default class PricelistManagerCommands {

this.bot.pricelist
.removePrice(params.sku as string, true)
.then(entry => this.bot.sendMessage(steamID, `✅ Removed "${entry.name}".`))
.then(entry => {
this.bot.sendMessage(steamID, `✅ Removed "${entry.name}".`);

void this.requestCheck(params.sku, 'bptf').asCallback(
(err: ErrorRequest, body: RequestCheckResponse) => {
if (err) {
log.debug(`❌ Failed to request pricecheck for ${entry.sku}: ${JSON.stringify(err)}`);
}

if (!body) {
log.debug(
`❌ Error while requesting price check for ${entry.sku} (returned null/undefined).`
);
} else {
log.debug(`✅ Requested pricecheck for ${body.name} (${entry.sku}).`);
}
}
);
})
.catch(err =>
this.bot.sendMessage(steamID, `❌ Failed to remove pricelist entry: ${(err as Error).message}`)
);
Expand Down