Combine Precondition and object available on the Command

Not sure if I've put that the right way, but I'm sure that the example will make some sense. I have this precondition that works.
export class OnlyAlive extends Precondition {
public override async chatInputRun(interaction: Command.ChatInputCommandInteraction, command: Command) {
const request = new DrpgCommandRequest(interaction, command);
return this.checkAlive(request);
}

public override async messageRun(message: Message, command: Command) {
const request = new DrpgCommandRequest(message, command);
return this.checkAlive(request);
}

private async checkAlive(request: DrpgCommandRequest): Promise<PreconditionResult> {
const character = await CharacterService.getCharacter(request.author);
if (character) {
//Ideally - right here - I would set some property that I can access inside the command
return this.ok();
} else {
const e = Swrpg.warnCharacterNotFound(request.author, request);
return this.error({ message: e.data.description, identifier: e.data.title });
}
}
}
export class OnlyAlive extends Precondition {
public override async chatInputRun(interaction: Command.ChatInputCommandInteraction, command: Command) {
const request = new DrpgCommandRequest(interaction, command);
return this.checkAlive(request);
}

public override async messageRun(message: Message, command: Command) {
const request = new DrpgCommandRequest(message, command);
return this.checkAlive(request);
}

private async checkAlive(request: DrpgCommandRequest): Promise<PreconditionResult> {
const character = await CharacterService.getCharacter(request.author);
if (character) {
//Ideally - right here - I would set some property that I can access inside the command
return this.ok();
} else {
const e = Swrpg.warnCharacterNotFound(request.author, request);
return this.error({ message: e.data.description, identifier: e.data.title });
}
}
}
I've used it to replace a bunch of code that I had in several commands that was similar to
const character = await CharacterService.getCharacter(request.author);
if(!character) return Logger.Error("No Character");

//Do something with the character object
const character = await CharacterService.getCharacter(request.author);
if(!character) return Logger.Error("No Character");

//Do something with the character object
The key to my question is on that last line above.... do something with the character object. To do this when I need to, it means that I need to use CharacterService.getCharacter a second time - which if possible, I'd like to avoid. Almost all of my commands have a context of the character. The reason I have it as a precondition is so that I can selectively apply it to the commands, as not all of them need the character. But enough of them do, that if I can abstract that character up into the command itself, it'll be useful.
Can I do something at the precondition level, to assign something, so that in the command itself, I will have access to the character?
Otherwise, is there some better way to do this, that is not a precondition?
2 Replies
Favna
Favna11mo ago
Can I do something at the precondition level, to assign something, so that in the command itself, I will have access to the character?
There isnt. Honestly with how many times it's been requested however maybe we should consider adding a preconditionData: T or something. Thoughts @vladdy ?
Otherwise, is there some better way to do this, that is not a precondition?
if (someCheckFunction(...)) {
// do something
} else {
// do something else
}
if (someCheckFunction(...)) {
// do something
} else {
// do something else
}
Preconditions are just glorified if checks after all. @vladdy ThisPoint
vladdy
vladdy11mo ago
maybe, idk rn havent had time to think about it