forked from noxious/server
Commands
This commit is contained in:
33
src/events/chat/gm/AlertCommand.ts
Normal file
33
src/events/chat/gm/AlertCommand.ts
Normal file
@ -0,0 +1,33 @@
|
||||
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.character?.id as number)
|
||||
if (!character) return
|
||||
|
||||
// When 1 arg is provided, only send message. 2 includes a title
|
||||
if (args.length === 1) {
|
||||
return io.emit('notification', { message: args[0] })
|
||||
}
|
||||
|
||||
io.emit('notification', { title: args[0], message: args.slice(1).join(' ') })
|
||||
callback(true)
|
||||
} catch (error: any) {
|
||||
console.log(`---Error sending message: ${error.message}`)
|
||||
}
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user