forked from noxious/server
29 lines
926 B
TypeScript
29 lines
926 B
TypeScript
import { Server } from 'socket.io'
|
|
import { TSocket } from '../../../utilities/types'
|
|
import { getArgs, isCommand } from '../../../utilities/chat'
|
|
import CharacterRepository from '../../../repositories/characterRepository'
|
|
|
|
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 (!isCommand(data.message, 'alert')) return
|
|
|
|
const args = getArgs('alert', data.message)
|
|
|
|
if (!args) return
|
|
|
|
const character = await CharacterRepository.getByUserAndId(socket.user?.id as number, socket.characterId as number)
|
|
if (!character) return
|
|
|
|
io.emit('notification', { title: 'Message from GM', message: args.join(' ') })
|
|
callback(true)
|
|
} catch (error: any) {
|
|
console.log(`---Error sending message: ${error.message}`)
|
|
}
|
|
})
|
|
}
|