forked from noxious/server
47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
import { BaseEvent } from '#application/base/baseEvent'
|
|
import CharacterRepository from '#repositories/characterRepository'
|
|
import ChatService from '#services/chatService'
|
|
|
|
type TypePayload = {
|
|
message: string
|
|
}
|
|
|
|
export default class AlertCommandEvent 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 (!ChatService.isCommand(data.message, 'alert')) {
|
|
return
|
|
}
|
|
|
|
// Check if character exists
|
|
const character = await CharacterRepository.getByUserAndId(this.socket.userId!, this.socket.characterId!)
|
|
if (!character) {
|
|
this.logger.error('chat:alert_command error', 'Character not found')
|
|
return callback(false)
|
|
}
|
|
|
|
// Check if the user is the GM
|
|
if (character.role !== 'gm') {
|
|
this.logger.info(`User ${character.id} tried to set time but is not a game master.`)
|
|
return callback(false)
|
|
}
|
|
|
|
const args = ChatService.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) {
|
|
this.logger.error('chat:alert_command error', error.message)
|
|
callback(false)
|
|
}
|
|
}
|
|
}
|