Skip to content

Commit

Permalink
Add property to read immutable state from event. Add warning message …
Browse files Browse the repository at this point in the history
…if someone tries to change an immutable value (for administrative checks)
  • Loading branch information
Bas Corpelijn committed Feb 22, 2024
1 parent 05020eb commit fcacecb
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@ export class PreUserScoreChangedEventArguments extends PluginEventArguments {
public set changeInScore(newChangeInScore: number) {
if (isNaN(newChangeInScore)) {
console.error(`Failed to set new score change for source '${this.nameOfOriginPlugin}' for reason '${this.reason}': not a number`);
} else if (!this.immutable) {
} else if (this.isImmutable) {
console.warn(`Cannot change the value for an immutable event from source '${this.nameOfOriginPlugin}' with reason '${this.reason}'.`);
} else {
this.myChangeInScore = newChangeInScore;
}
}

/**
* Indicates if the event is immutable.
*/
public get immutable(): boolean {
return this.isImmutable;
}

private isImmutable = false;
private myChangeInScore = 0;

/**
Expand All @@ -43,10 +53,11 @@ export class PreUserScoreChangedEventArguments extends PluginEventArguments {
* not being caused by a plugin.
* @param immutable Disallows the score delta to be changed.
*/
constructor(chat: Chat, user: User, changeInScore: number, reason: string, nameOfOriginPlugin: string, private immutable: boolean) {
constructor(chat: Chat, user: User, changeInScore: number, reason: string, nameOfOriginPlugin: string, immutable: boolean) {
super(nameOfOriginPlugin, reason);
this.chat = chat;
this.user = user;
this.changeInScore = changeInScore;
this.myChangeInScore = changeInScore;
this.isImmutable = immutable;
}
}

0 comments on commit fcacecb

Please sign in to comment.