forked from noxious/server
34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
import { Server } from 'socket.io'
|
|
import { TSocket } from '../../utilities/types'
|
|
import CharacterRepository from '../../repositories/characterRepository'
|
|
import ZoneRepository from '../../repositories/zoneRepository'
|
|
import { isCommand } from '../../utilities/chat'
|
|
|
|
type TypePayload = {
|
|
message: string
|
|
}
|
|
|
|
export default function (socket: TSocket, io: Server) {
|
|
socket.on('chat:send_message', async (data: TypePayload, callback: (response: boolean) => void) => {
|
|
try {
|
|
if (!data.message) return
|
|
if (isCommand(data.message)) return
|
|
|
|
const character = await CharacterRepository.getByUserAndId(socket.user?.id as number, socket.character?.id as number)
|
|
if (!character) return
|
|
|
|
const zone = await ZoneRepository.getById(character.zoneId)
|
|
if (!zone) return
|
|
|
|
callback(true)
|
|
|
|
io.to(zone.id.toString()).emit('chat:message', {
|
|
character: character,
|
|
message: data.message
|
|
})
|
|
} catch (error: any) {
|
|
console.log(`---Error sending message: ${error.message}`)
|
|
}
|
|
})
|
|
}
|