# Events

> Run code on its own when something happens in your server, like a member joining or a button click.

A command runs when someone types it. An **event** runs on its own when something happens in your server. A member joins, a reaction is added, a button is clicked.

## Make an event

1. Open **Create → Event**.
2. Give it a **Name**. Only you see this.
3. Pick a **Type**. This is what makes it run.
4. Write the code in the **Code** box.
5. Press **Save**.

An event has one code box. It has no Response box and no post-hook. To say something, the code sends its own message with `send_message`.

> **How many events can be on:** Free servers can have **2** events turned on at once. Starter has 5. Basic and Advanced have no limit.

## Event types

| Type | Runs when | Extra field |
|---|---|---|
| Member Join | A member joins the server | |
| Member Leave | A member leaves or is kicked | |
| Member Update | A member's nickname, roles or timeout changes | |
| Button Click / Select Menu Change | Someone clicks a button or picks from a menu | Custom ids |
| Modal Submit | Someone sends a form | Custom ids |
| Autocomplete | Someone types in a command option with suggestions | |
| Thread (Create/Delete) | A thread is made, changed or deleted | |
| Reaction (Add/Remove) | Someone adds or removes a reaction | Reactions |
| MESSAGE (Pro Only) | Someone sends a message. Premium servers only | |
| Clock | A timer you set with `schedule_clock` or a job goes off | Custom ids |
| Voice Join | A member joins a voice channel | |
| Voice Leave | A member leaves a voice channel | |
| Voice Change | A member moves to another voice channel | |
| Vote (top.gg) | Someone votes for the server on top.gg | |

**Custom ids** says which buttons, forms or timers this event listens to. Separate them with commas. For Clock, `remind_*` matches every id that starts with `remind_`.

**Reactions** says which emojis this event listens to. Use the emoji, a custom emoji name, or its ID.

Each type gets different variables, like `user` or `channel`. See [Event variables](https://ccbot.app/docs/reference/event-variables) for the full list.

## Example: auto role on join

Type: **Member Join**

```python
add_roles(user, "Member")
send_message("welcome", f"Welcome {user.mention}! You are member #{server.member_count}.")
```

`user` is the member who joined. `"Member"` and `"welcome"` are a role name and a channel name. IDs and mentions work too.

> **Tip:** For a simple welcome message, you do not need code. Use the **Welcome & Leave** page. See [Welcome message](https://ccbot.app/docs/guides/welcome-message).

## Example: keyword auto reply

Type: **MESSAGE (Pro Only)**

```python
if user is None or user.is_bot:
    return

text = message.content.lower()

if "how do i join" in text:
    send_message(channel, "Read the steps in #how-to-join.", reply=message_id)
```

> **Always skip bots first:** A message event also runs for the bot's own messages. Without the first two lines, the bot can reply to its own reply, again and again.

## Example: log reactions

Type: **Reaction (Add/Remove)**. Reactions: `⭐`

```python
if action == "add":
    send_message("star-log", f"{user.mention} starred a message in {channel.mention}")
```

`action` is `"add"` or `"remove"`.

## Curly brackets do not work here

Events have no reply box, so `{user.mention}` style variables do nothing. Build text with an f-string: `f"Hi {user.mention}"`. The `f` before the quote matters.

## Commands-only functions

These only work in commands, not in events: `set_content`, `set_embed`, `set_response_message`, `set_dm_response`, `set_components`, `set_view`, `add_attachment`, `end`.

Use `send_message` instead.
