AI Core

ai-chat for developers

The chat API ai-chat answers (FiveM's chat resource), its exports, events and message shape.

ai-chat answers FiveM's chat resource API, so you write chat code the standard FiveM way. Exports answer under both exports['ai-chat'] and exports.chat.

ChatMessage

The message shape every export and event takes.

Prop

Type

Server exports

addMessage

Sends a chat line to one player, several players or everyone.

Side: Server · Waits: no

Signature
addMessage(target: number | number[], message: ChatMessage): void
Lua
exports.chat:addMessage(source, { args = { 'Bank', 'Your salary arrived.' }, color = { 61, 247, 158 } })

Parameters

Prop

Type

Returns

Nothing. A message without a valid args list is dropped.

Example

import { callExport } from "@aicore-fivem/lib/server";

function announce(text: string): void {
  callExport("chat", "addMessage", -1, { args: ["City", text], color: [61, 247, 158], multiline: true });
}
announce("The bank closes in 10 minutes.");

registerMessageHook

Runs your function for every message a player sends, before it is delivered. The hook can change the message, change who gets it, or cancel it. Hooks are removed when your resource stops.

Side: Server · Waits: no

Signature
registerMessageHook(hook: (source: number, message: ChatMessage, ref: HookRef) => void): void

HookRef

Prop

Type

Example

import { callExport } from "@aicore-fivem/lib/server";

callExport("chat", "registerMessageHook", (source: number, message: { args: string[] }, ref: { cancel(): void }) => {
  const text = message.args[message.args.length - 1] ?? "";
  if (/discord\.gg\//i.test(text)) ref.cancel();
});

registerMode

Adds a channel from a script (FiveM's chat modes). Its callback decides who gets each message, like a message hook. The mode is removed when your resource stops.

Side: Server · Waits: no

Signature
registerMode(data: { name: string; displayName?: string; color?: string; cb?: (source: number, message: ChatMessage, ref: HookRef) => void }): void

Prop

Type

Client exports

ExportWhat it does
addMessage(message: ChatMessage | string): voidShows a line on this player's chat only.
addSuggestion(name: string, help: string, params?: { name: string, help: string }[]): voidAdds a command suggestion for this player.

Events

chatMessage (server)

Fires on the server for every message a player sends, before delivery. Cancel it (CancelEvent()) to drop the message.

Side: Server (local event)

ArgumentTypeWhat it is
sourcenumberThe sender's server id.
authorstringThe author shown (the character name).
textstringThe message text.
on("chatMessage", (source: number, author: string, text: string) => {
  console.log(`[chat] ${author} (${source}): ${text}`);
});

Client events ai-chat listens to

Send them from the server (TriggerClientEvent) or from a client script (TriggerEvent).

EventArgumentsWhat it does
chat:addMessagemessage: ChatMessage | stringShows a line.
chatMessageauthor: string, color: [r, g, b], text: stringShows a line (old style).
chat:addSuggestionname: string, help: string, params?: { name, help }[]Adds one suggestion.
chat:addSuggestionssuggestions: { name, help, params? }[]Adds several suggestions.
chat:removeSuggestionname: stringRemoves a suggestion.
chat:addMode / chat:removeModemode / nameAdds or removes a script mode on this client.
chat:clearnoneEmpties this player's chat.

chat:addTemplate is ignored: templates (HTML) are never drawn. Messages typed by players are not sent through _chat:messageEntered; listen to chatMessage on the server.

On this page