From 13521eeeba143c94dc5521025949d64d4161eee8 Mon Sep 17 00:00:00 2001 From: Koki Takahashi Date: Sun, 23 Jun 2024 16:25:22 +0900 Subject: [PATCH] discord: Refactor usage of mutex variable to refer to class property instead of local variable in class methods --- discord/hayaoshi.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/discord/hayaoshi.ts b/discord/hayaoshi.ts index 95b49d09..d92dca96 100644 --- a/discord/hayaoshi.ts +++ b/discord/hayaoshi.ts @@ -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'; @@ -73,6 +72,8 @@ export default class Hayaoshi extends EventEmitter { joinVoiceChannelFn: () => VoiceConnection; + mutex = new Mutex(); + constructor( joinVoiceChannelFn: () => VoiceConnection, users: {discord: string, slack: string}[], @@ -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; } @@ -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); @@ -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);