import { Server } from 'socket.io' import { TSocket } from '../../../utilities/types' import { getArgs, isCommand } from '../../../utilities/chat' import CharacterRepository from '../../../repositories/characterRepository' import { gameLogger } from '../../../utilities/logger' type TypePayload = { message: string } export default class AlertCommandEvent { constructor( private readonly io: Server, private readonly socket: TSocket ) {} public listen(): void { this.socket.on('chat:message', this.handleAlertCommand.bind(this)) } private async handleAlertCommand(data: TypePayload, callback: (response: boolean) => void): Promise { try { if (!isCommand(data.message, 'alert')) { return } // Check if character exists const character = await CharacterRepository.getByUserAndId(this.socket.userId!, this.socket.characterId!) if (!character) { gameLogger.error('chat:alert_command error', 'Character not found') return callback(false) } // Check if the user is the GM if (character.role !== 'gm') { gameLogger.info(`User ${character.id} tried to set time but is not a game master.`) return callback(false) } const args = getArgs('alert', data.message) if (!args) { return callback(false) } this.io.emit('notification', { title: 'Message from GM', message: args.join(' ') }) return callback(true) } catch (error: any) { gameLogger.error('chat:alert_command error', error.message) callback(false) } } }