server/src/events/chat/message.ts
2024-12-28 22:12:59 +01:00

46 lines
1.3 KiB
TypeScript

import { BaseEvent } from '#application/base/baseEvent'
import ZoneManager from '#managers/zoneManager'
import ZoneRepository from '#repositories/zoneRepository'
import ChatService from '#services/chatService'
type TypePayload = {
message: string
}
export default class ChatMessageEvent extends BaseEvent {
public listen(): void {
this.socket.on('chat:message', this.handleEvent.bind(this))
}
private async handleEvent(data: TypePayload, callback: (response: boolean) => void): Promise<void> {
try {
if (!data.message || ChatService.isCommand(data.message)) {
return callback(false)
}
const zoneCharacter = ZoneManager.getCharacterById(this.socket.characterId!)
if (!zoneCharacter) {
this.logger.error('chat:message error', 'Character not found')
return callback(false)
}
const character = zoneCharacter.character
const zone = await ZoneRepository.getById(character.zone?.id!)
if (!zone) {
this.logger.error('chat:message error', 'Zone not found')
return callback(false)
}
if (await ChatService.sendZoneMessage(this.io, this.socket, data.message, character.id, zone.id)) {
return callback(true)
}
callback(false)
} catch (error: any) {
this.logger.error('chat:message error', error.message)
callback(false)
}
}
}