I am wondering what would be the best method for filtering messages sent through the realtime chat sockets.
We already have a client-side filter that compares messages to a .txt list of words, but want to know if server side you can create a .lua script to do the same process and replace words. Assuming we have to register a hook, I see a ChannelMessageUpdate hook but unsure if appropriate or how to then adapt.
@keiranlovett Good question, you’re on the right track. There are two message types you probably want to hook into: ChannelMessageSend (this is the main one) and ChannelMessageUpdate (only if your game clients use it).
In these hooks you should inspect the incoming message or send it to a profanity verification service, and either sanitise the text or reject the message.
I’m not sure which runtime you plan to use, but here’s an example of a Go runtime before hook that would reject bad messages.
// Inside your Go module's `InitModule` function.
if err := initializer.RegisterBeforeRt("ChannelMessageSend", func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, envelope *rtapi.Envelope) (*rtapi.Envelope, error) {
message := envelope.GetChannelMessageSend()
if strings.Contains(message.Content, "bad word") {
// Alternatively, to sanitise instead of reject:
// message.Content = strings.ReplaceAll(message.Content, "bad word", "****")
// Reject the message send:
return nil, runtime.NewError("profanity detected", 3)
}
return envelope, nil
}); err != nil {
return err
}
@keiranlovett You’re on the right track with the use of before hooks. That’s how most game teams implement a profanity filter. Some example code looks like:
// This code should be inside InitModule.
if err := initializer.RegisterBeforeRt("ChannelMessageSend", beforeRtChatMessageSend); err != nil {
return err
}
func beforeRtChatMessageSend(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, envelope *rtapi.Envelope) (envelope2 *rtapi.Envelope, e error) {
inputMessage := envelope.GetChannelMessageSend()
// Profanity check the chat message input.
inputMessage.Content = "some safe string"
return envelope, nil
}