Skip to content

Commit 5e1823d

Browse files
committed
More player lock improvements
Changed the lock method to return void instead of boolean, simplified unlock handling, and added a delayed unlock after successful action. Also improved error handling and logging for failed actions.
1 parent f5789d3 commit 5e1823d

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/Core/GTATunesPlayer.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -728,13 +728,13 @@ export class Player extends EventEmitter<PlayerEventMap> {
728728
async lock(
729729
action: () => PossiblyAsync<void>,
730730
interaction?: Interaction
731-
): Promise<boolean> {
731+
): Promise<void> {
732732
let isLocked = this.isLocked;
733733
const lockedAt = this.lockedAt;
734734
this.locked = true;
735-
this.lockedAt = unix();
735+
this.lockedAt ??= unix();
736736

737-
if (lockedAt !== null && isLocked && (unix() - (lockedAt ?? 0) > 20)) {
737+
if (lockedAt !== null && isLocked && (unix() - lockedAt > 20)) {
738738
gtaTunesLog('PLAYER', `Player lock in guild ${p.magenta(this.guild.name)} (${p.magenta(this.guild.id)}) has expired, forcing unlock.`);
739739
isLocked = false;
740740
this.lockedAt = null;
@@ -748,7 +748,7 @@ export class Player extends EventEmitter<PlayerEventMap> {
748748

749749
if (interaction) {
750750
await LockedPlayerError.handleInteraction(this, interaction);
751-
return false;
751+
return;
752752
}
753753

754754
throw new LockedPlayerError(this);
@@ -759,26 +759,23 @@ export class Player extends EventEmitter<PlayerEventMap> {
759759
`Locking player in ${p.magenta(this.guild.name)} (${p.magenta(this.guild.id)}).`
760760
);
761761

762-
try {
763-
await action();
764-
} catch (e) {
762+
const unlock = () => {
765763
gtaTunesLog(
766764
'PLAYER',
767-
`Unlocking player in ${p.magenta(this.guild.name)} (${p.magenta(this.guild.id)}) after failing action.`
765+
`Unlocking player in ${p.magenta(this.guild.name)} (${p.magenta(this.guild.id)}).`
768766
);
769767
this.locked = false;
770768
this.lockedAt = null;
771-
throw e;
772769
}
773770

774-
gtaTunesLog(
775-
'PLAYER',
776-
`Unlocking player in ${p.magenta(this.guild.name)} (${p.magenta(this.guild.id)}).`
777-
);
778-
this.locked = false;
779-
this.lockedAt = null;
780-
781-
return true;
771+
try {
772+
await action();
773+
setTimeout(() => unlock(), 1500);
774+
} catch (e) {
775+
gtaTunesLog('FAIL', 'Locked player action failed', e);
776+
unlock();
777+
throw e;
778+
}
782779
}
783780
}
784781

0 commit comments

Comments
 (0)