Skip to content

Commit

Permalink
discord: Refactor usage of mutex variable to refer to class property …
Browse files Browse the repository at this point in the history
…instead of local variable in class methods
  • Loading branch information
hakatashi committed Jun 23, 2024
1 parent 2d8ef87 commit 13521ee
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions discord/hayaoshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {extractValidAnswers, judgeAnswer, formatQuizToSsml, fetchIntroQuizData,
import {getSpeech, Voice} from './speeches';

const log = logger.child({bot: 'discord'});
const mutex = new Mutex();

type QuizMode = 'quiz' | 'intro-quiz';

Expand Down Expand Up @@ -73,6 +72,8 @@ export default class Hayaoshi extends EventEmitter {

joinVoiceChannelFn: () => VoiceConnection;

mutex = new Mutex();

constructor(
joinVoiceChannelFn: () => VoiceConnection,
users: {discord: string, slack: string}[],
Expand Down Expand Up @@ -389,7 +390,7 @@ export default class Hayaoshi extends EventEmitter {
this.state.timeupTimeoutId = setTimeout(resolve, 5000);
});
log.info('[hayaoshi] onFinishReadingQuestion - timeout');
mutex.runExclusive(async () => {
this.mutex.runExclusive(async () => {
if (this.state.phase !== 'gaming') {
return;
}
Expand Down Expand Up @@ -454,7 +455,7 @@ export default class Hayaoshi extends EventEmitter {

setAnswerTimeout() {
return setTimeout(() => {
mutex.runExclusive(async () => {
this.mutex.runExclusive(async () => {
await this.playSound('timeup');
this.state.penaltyUsers.add(this.state.pusher);
this.incrementPenalty(this.state.pusher);
Expand Down Expand Up @@ -634,7 +635,7 @@ export default class Hayaoshi extends EventEmitter {
}

log.info(`[hayaoshi] onMessage: ${message.content}`);
mutex.runExclusive(async () => {
this.mutex.runExclusive(async () => {
if (this.state.phase === 'answering' && this.state.pusher === message.member.user.id && message.content !== 'p') {
log.info(`[hayaoshi] onMessage - answering: ${message.content}`);
clearTimeout(this.state.answerTimeoutId);
Expand Down

0 comments on commit 13521ee

Please sign in to comment.