> For the complete documentation index, see [llms.txt](https://tgiann.gitbook.io/tgiann/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tgiann.gitbook.io/tgiann/scripts/tgiann-event/exports.md). Agents can also search and read this documentation through its [MCP server](https://tgiann.gitbook.io/tgiann/~gitbook/mcp).

# Exports

The server exports are the public API of tgiann-event. Other resources (for example the tgiann-phone Calendar app) use them to list, create, approve and delete events with the same validation, cooldown, permissions and Discord messages as the event menu.

## Client

### Open

Opens the event menu for the local player, the same as the command or keybind.

```lua
exports["tgiann-event"]:Open()
```

***

### Close

Closes the event menu if it is open.

```lua
exports["tgiann-event"]:Close()
```

***

### CalcClosestEventUnixTime

Asks the event UI to find the player's next upcoming event and saves it on the local player's state bags: `tgiannClosestEventTime` (start time in seconds, `false` when there is none) and `tgiannClosestEventImg` (image link, `false` when there is none).

```lua
exports["tgiann-event"]:CalcClosestEventUnixTime()
local nextEventTime = LocalPlayer.state.tgiannClosestEventTime
```

***

### createEvent

Sends a create request for the local player to the server. It goes through the same checks as the server `CreateEvent` export (validation, cooldown, admin approval) and the player gets a success or error notification.

```lua
exports["tgiann-event"]:createEvent({
    eventName = "Friday night car meet",
    imgUrl = "https://example.com/image.jpg",
    date = GetCloudTimeAsInt() * 1000 + 86400000,
    selectedEventType = "carmeet",
    description = "Bring your cleanest ride, we start at the pier.",
    coords = GetEntityCoords(PlayerPedId()),
})
```

#### Parameters

* **eventData**: `table` same fields as `data` in the server `CreateEvent` export

## Server

### GetEvents

Returns events sorted by start time, oldest first. Without a filter only approved events are returned. Events waiting for approval are for admins only: the event menu and live updates never send them to other players, not even to the player who created them. Use `includePending` only for lists you show to admins.

```lua
local events = exports["tgiann-event"]:GetEvents()
local upcoming = exports["tgiann-event"]:GetEvents({ from = os.time() })
```

#### Parameters

* **filter?**: `table`
  * **from?**: `number` only events starting at or after this time (seconds)
  * **to?**: `number` only events starting at or before this time (seconds)
  * **includePending?**: `boolean` include every event that is waiting for approval, only for lists shown to admins
  * **citizenId?**: `string` ignored, kept so older code keeps working. Events waiting for approval are only returned with `includePending`

#### Returns

* `EventRecord[]` list of events, see [EventRecord](#eventrecord)

***

### GetEvent

Returns one event by id, approved or not.

```lua
local event = exports["tgiann-event"]:GetEvent(12)
```

#### Parameters

* **id**: `number` event id

#### Returns

* `EventRecord|nil` the event, `nil` when it doesn't exist

***

### GetEventTypes

Returns the event types from `config.eventTypes`, keyed by id. `order` is the position in the config list.

```lua
local types = exports["tgiann-event"]:GetEventTypes()
print(types.party.icon, types.party.order)
```

#### Returns

* `table<string, { icon: string, label: string|table<string, string>, order: number }>` event types by id

***

### GetEventTypeList

Returns the event types as a list, in the same order as `config.eventTypes`. The first entry is the default type, and events with a type that was removed from the config are shown as that type too. `label` is either a plain text or one text per language, like `{ en = "Party", tr = "Parti" }`.

```lua
local typeList = exports["tgiann-event"]:GetEventTypeList()
local defaultType = typeList[1].id
```

#### Returns

* `{ id: string, icon: string, label: string|table<string, string> }[]` ordered event types

***

### NeedsApproval

Returns whether new events wait for admin approval (`config.needAdminApproval`).

```lua
local needsApproval = exports["tgiann-event"]:NeedsApproval()
```

#### Returns

* `boolean` `true` when new events need an admin to approve them

***

### IsAdmin

Returns whether a player can approve and delete any event. It uses `sEditable.playerIsAdmin` from `server/editable.lua`.

```lua
local isAdmin = exports["tgiann-event"]:IsAdmin(source)
```

#### Parameters

* **source**: `number` player server id

#### Returns

* `boolean` whether the player is an event admin

***

### CreateEvent

Creates an event in the name of a player. The data is checked on the server (see [Error keys](#error-keys)) and each character has to wait 30 seconds between two events, exports included. The event is approved right away when `isApproved` is `true` or `config.needAdminApproval` is `false`, otherwise it waits for an admin and only admins can see it until it's approved. Approved events are posted to the Discord webhook and `tgiann-event:server:eventChanged` fires with `created`. The player doesn't get a notification from this export, use the returned error key to show your own message.

Older code that ignores the return values keeps working.

```lua
local id, err = exports["tgiann-event"]:CreateEvent(source, {
    eventName = "Friday night car meet",
    imgUrl = "https://example.com/image.jpg",
    date = (os.time() + 86400) * 1000,
    selectedEventType = "carmeet",
    description = "Bring your cleanest ride, we start at the pier.",
    coords = vector3(-1850.0, -1230.0, 13.0),
})
if not id then
    print("Event was not created: " .. err)
end
```

#### Parameters

* **source**: `number` server id of the organizer
* **data**: `table`
  * **eventName**: `string` 3 to 100 characters
  * **imgUrl**: `string` http or https link, up to 500 characters, must not match `config.imageUrlBlacklist`
  * **date**: `number` start time in **milliseconds**, in the future and at most one year ahead
  * **selectedEventType**: `string` an `id` from `config.eventTypes`
  * **description**: `string` markdown, 10 to 4000 characters
  * **coords**: `vector3|table` event location, `{ x = 0.0, y = 0.0, z = 0.0 }` also works
* **isApproved?**: `boolean` skip admin approval

#### Returns

* `number|nil` id of the new event, `nil` when it failed
* `string|nil` [error key](#error-keys) when it failed

***

### DeleteEvent

Deletes an event. Admins can delete any event, other players only their own. The event disappears for everyone right away, stops counting in the statistics, and `tgiann-event:server:eventChanged` fires with `deleted`.

```lua
local deleted, err = exports["tgiann-event"]:DeleteEvent(source, 12)
```

#### Parameters

* **source**: `number` server id of the player doing the delete
* **id**: `number` event id

#### Returns

* `boolean` whether the event was deleted
* `string|nil` error key: `EVENT_NOT_FOUND` or `EVENT_NO_PERMISSION`

***

### ApproveEvent

Approves an event that is waiting for approval. Admins only. The event shows up for everyone, gets posted to the Discord webhook, and `tgiann-event:server:eventChanged` fires with `approved`. If the creator is online, they get the `EVENT_APPROVED_OWNER_NOTIF` notification.

```lua
local approved, err = exports["tgiann-event"]:ApproveEvent(source, 12)
```

#### Parameters

* **source**: `number` server id of the admin
* **id**: `number` event id

#### Returns

* `boolean` whether the event was approved
* `string|nil` error key: `EVENT_NO_PERMISSION`, `EVENT_NOT_FOUND` or `EVENT_ALREADY_APPROVED`

***

### GetStats

Returns the numbers the Statistics screen shows. Only approved events that still exist are counted, by the day they were created, using the server's time zone. Events waiting for approval are only counted in `pending`. The `period` argument is not used right now, the full payload is always returned.

```lua
local stats = exports["tgiann-event"]:GetStats()
print(stats.totals.thisMonth, stats.pending, stats.upcoming)
```

#### Parameters

* **period?**: `string` not used right now, the payload always contains every period

#### Returns

* `table` statistics payload:
  * **totals**: `table<periodKey, number>` approved events created in each period
  * **daily**: `{ t: number, count: number }[]` last 30 days, oldest first, `t` is the server-local midnight in seconds
  * **monthDaily**: `{ t: number, count: number }[]` every day of the current month up to today
  * **monthly**: `{ t: number, count: number }[]` one entry per month from the first event's month to now, `t` is the month start
  * **periods**: `table<periodKey, table>` per period details:
    * **total**: `number` approved events created in the period
    * **organizers**: `number` unique organizers in the period
    * **topPlayers**: `{ citizenId: string, name: string, job: string|nil, count: number, img: string|nil }[]` top 10 organizers
    * **topJobs**: `{ job: string, label: string, count: number }[]` top 10 jobs
  * **pending?**: `number` events waiting for approval. This export always includes it, the Statistics screen only gets it for admins
  * **upcoming**: `number` approved events that haven't started yet
  * **utcOffset**: `number` server time zone offset in seconds
  * **generatedAt**: `number` server time in seconds

`periodKey` is one of `thisWeek` (starts on Monday), `thisMonth`, `last7`, `last15`, `last30`, `allTime`.

## Server events

### tgiann-event:server:eventChanged

Fires on the server after an event is created, approved or deleted, from the menu or through the exports. It is a local server event, listen to it with `AddEventHandler` (clients can't trigger it). For `deleted`, `record` is the event as it was right before it was removed.

```lua
AddEventHandler("tgiann-event:server:eventChanged", function(action, record)
    if action == "created" and not record.approved then
        print(("New event waiting for approval: %s"):format(record.title))
    end
end)
```

#### Parameters

* **action**: `string` `"created"`, `"approved"` or `"deleted"`
* **record**: `EventRecord` the event, see [EventRecord](#eventrecord)

## Reference

### EventRecord

Event table returned by `GetEvents`, `GetEvent` and passed to `tgiann-event:server:eventChanged`.

* **id**: `number` event id
* **title**: `string` event name
* **description**: `string` markdown description
* **img**: `string` image link
* **unixTime**: `number` start time in seconds
* **coords**: `{ x: number, y: number, z: number }|nil` event location
* **creatorCitizenId**: `string` organizer's citizen id
* **creatorName**: `string` organizer's character name
* **creatorImg**: `string|nil` organizer's photo (from `sEditable.getPlayerImg`)
* **creatorJobLabel**: `string|nil` organizer's job label when the event was created
* **type**: `string` event type id from `config.eventTypes`. Events saved with a type that is no longer in the config come back as the first type in the list
* **approved**: `boolean` whether the event is approved

### Error keys

`CreateEvent`, `DeleteEvent` and `ApproveEvent` return one of these keys when they fail. Each key has a matching text in the language file.

| Key                         | When                                                                    |
| --------------------------- | ----------------------------------------------------------------------- |
| `EVENT_INVALID_DATA`        | `data` is not a table                                                   |
| `EVENT_INVALID_TITLE`       | `eventName` is not 3 to 100 characters                                  |
| `EVENT_INVALID_DESCRIPTION` | `description` is not 10 to 4000 characters                              |
| `EVENT_INVALID_IMAGE`       | `imgUrl` is not an http or https link, or is longer than 500 characters |
| `EVENT_IMAGE_BLOCKED`       | `imgUrl` matches an entry in `config.imageUrlBlacklist`                 |
| `EVENT_INVALID_DATE`        | `date` is in the past or more than one year ahead                       |
| `EVENT_INVALID_TYPE`        | `selectedEventType` is not an id from `config.eventTypes`               |
| `EVENT_INVALID_COORDS`      | `coords` is missing or invalid                                          |
| `EVENT_COOLDOWN`            | the character created an event less than 30 seconds ago                 |
| `EVENT_PLAYER_NOT_FOUND`    | the player's character data couldn't be loaded                          |
| `EVENT_NOT_FOUND`           | no event with that id                                                   |
| `EVENT_NO_PERMISSION`       | the player is not an admin (or not the creator, for deletes)            |
| `EVENT_ALREADY_APPROVED`    | the event is already approved                                           |
| `EVENT_CREATE_FAILED`       | the database insert failed                                              |

## Integrations

### tgiann-phone Calendar

When tgiann-event is started, the tgiann-phone Calendar app shows public events next to personal ones and lets players publish events from the phone. It goes through the exports above, so event types, approval, validation, cooldown and admin rights all come from tgiann-event, and changes reach open phones live through `tgiann-event:server:eventChanged`. Turn it off in tgiann-phone's `configs/configApps.lua`:

```lua
config.calendar.useTgiannEvent = true                        -- Show tgiann-event events in the calendar and let players publish them (only while tgiann-event is running)
```
