-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: created cancel command (#1475)
- Loading branch information
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import SteamID from 'steamid'; | ||
import CommandHandler, { ICommand } from '../../CommandHandler'; | ||
import log from '../../../../lib/logger'; | ||
import Bot from '../../../Bot'; | ||
import IPricer from '../../../IPricer'; | ||
|
||
export default class CancelCommand implements ICommand { | ||
name = 'cancel'; | ||
|
||
description = 'Cancel the trade offer ❌'; | ||
|
||
allowInvalidType = false; | ||
|
||
constructor( | ||
public readonly bot: Bot, | ||
public readonly pricer: IPricer, | ||
public readonly commandHandler: CommandHandler | ||
) { | ||
this.bot = bot; | ||
this.pricer = pricer; | ||
this.commandHandler = commandHandler; | ||
} | ||
|
||
execute = (steamID: SteamID, message: string) => { | ||
// Maybe have the cancel command only cancel the offer in the queue, and have a command for canceling the offer? | ||
const positionInQueue = this.commandHandler.cartQueue.getPosition(steamID); | ||
|
||
// If a user is in the queue, then they can't have an active offer | ||
|
||
const custom = this.bot.options.commands.cancel.customReply; | ||
if (positionInQueue === 0) { | ||
// The user is in the queue and the offer is already being processed | ||
const cart = this.commandHandler.cartQueue.getCart(steamID); | ||
|
||
if (cart.isMade) { | ||
return this.bot.sendMessage( | ||
steamID, | ||
custom.isBeingSent | ||
? custom.isBeingSent | ||
: '⚠️ Your offer is already being sent! Please try again when the offer is active.' | ||
); | ||
} else if (cart.isCanceled) { | ||
return this.bot.sendMessage( | ||
steamID, | ||
custom.isCancelling | ||
? custom.isCancelling | ||
: '⚠️ Your offer is already being canceled. Please wait a few seconds for it to be canceled.' | ||
); | ||
} | ||
|
||
cart.setCanceled = 'BY_USER'; | ||
} else if (positionInQueue !== -1) { | ||
// The user is in the queue | ||
this.commandHandler.cartQueue.dequeue(steamID); | ||
this.bot.sendMessage( | ||
steamID, | ||
custom.isRemovedFromQueue ? custom.isRemovedFromQueue : '✅ You have been removed from the queue.' | ||
); | ||
|
||
clearTimeout(this.commandHandler.adminInventoryReset); | ||
delete this.commandHandler.adminInventory[steamID.getSteamID64()]; | ||
} else { | ||
// User is not in the queue, check if they have an active offer | ||
|
||
const activeOffer = this.bot.trades.getActiveOffer(steamID); | ||
|
||
if (activeOffer === null) { | ||
return this.bot.sendMessage( | ||
steamID, | ||
custom.noActiveOffer ? custom.noActiveOffer : "❌ You don't have an active offer." | ||
); | ||
} | ||
|
||
void this.bot.trades.getOffer(activeOffer).asCallback((err, offer) => { | ||
if (err || !offer) { | ||
const errStringify = JSON.stringify(err); | ||
const errMessage = errStringify === '' ? (err as Error)?.message : errStringify; | ||
return this.bot.sendMessage( | ||
steamID, | ||
`❌ Ohh nooooes! Something went wrong while trying to get the offer: ${errMessage}` + | ||
(!offer ? ` (or the offer might already be canceled)` : '') | ||
); | ||
} | ||
|
||
offer.data('canceledByUser', true); | ||
|
||
offer.cancel(err => { | ||
// Only react to error, if the offer is canceled then the user | ||
// will get an alert from the onTradeOfferChanged handler | ||
|
||
if (err) { | ||
log.warn('Error while trying to cancel an offer: ', err); | ||
return this.bot.sendMessage( | ||
steamID, | ||
`❌ Ohh nooooes! Something went wrong while trying to cancel the offer: ${err.message}` | ||
); | ||
} | ||
|
||
return this.bot.sendMessage( | ||
steamID, | ||
`✅ Offer sent (${offer.id}) has been successfully cancelled.` | ||
); | ||
}); | ||
}); | ||
} | ||
}; | ||
} |