48 lines
1.4 KiB
TypeScript

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:send_message', this.handleAlertCommand.bind(this))
}
private async handleAlertCommand(data: TypePayload, callback: (response: boolean) => void): Promise<void> {
try {
if (!isCommand(data.message, 'alert')) {
return
}
const args = getArgs('alert', data.message)
if (!args) {
callback(false)
return
}
const character = await CharacterRepository.getByUserAndId(this.socket.user?.id as number, this.socket.characterId as number)
if (!character) {
gameLogger.error('chat:alert_command error', 'Character not found')
callback(false)
return
}
this.io.emit('notification', { title: 'Message from GM', message: args.join(' ') })
callback(true)
} catch (error: any) {
gameLogger.error('chat:alert_command error', error.message)
callback(false)
}
}
}