> For the complete documentation index, see [llms.txt](https://jumpathy.gitbook.io/betterchat-v3/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jumpathy.gitbook.io/betterchat-v3/server/channel.md).

# Channel

A channel is a location where speakers can exchange messages, this page covers how to create them and their methods.

{% hint style="info" %}
The default chat channel name is 'Main'.
{% endhint %}

[The server API ](/betterchat-v3/server/getting-started.md)allows you to access channels and manage them from there.

```lua
local channelModule = api.channel --> {...}
```

### Fetching Channels

**--> :getByName(\<string:name>)**

This method fetches a channel by it's name, it will return nil if there's no channel found.

```lua
channel:getByName("Main") --> main chat
```

### Creating a channel

**--> .new(\<string:name>,\<boolean:autojoin>**

This method creates a channel object with the specified name, and if it's marked as 'autojoin', speakers will automatically be assigned to it.

```lua
channel.new("Test",true) --> Channel named 'Test' that everyone is now assigned to.
```

### Channel events, properties & methods

#### Properties

\--> .name \<string>

The '.name' property represents the channel's name.

```lua
channel.name --> <string>
```

\--> .messageCount \<integer>

The amount of messages in the channel thus far.

```lua
if(channel.messageCount > 100) then
    -- do something cool
end
```

\--> .speakers \<table>

The speakers associated with the channel in a table.

```lua
for _,speaker in pairs(channel.speakers) do
    print(speaker.name)
end
```

#### Events

\--> .chatted \<table:message>

This event is fired with a raw message object when a message is sent in the channel.

```lua
channel.events.chatted:Connect(function(object)
    print(object) --> {...} <message>
end)
```

\--> .speakerAdded \<table:speaker>

This event is fired when a speaker is added to the channel.

```lua
channel.events.speakerAdded:Connect(function(speaker)
    print(speaker.name,"is now in da club")
end)
```

\--> .speakerRemoved \<string:speakerName>

This event is fired when a speaker is removed from the channel.

```lua
channel.events.speakerRemoved:Connect(function(speaker)
    print("We didn't need",speaker.name,"anyways..")
end)
```

#### Methods

\--> :registerMessageProcess(\<string:name>,\<function:callback>)

The function passed will be called with a message object twice (before filtering, and after filtering). You can make changes to the message before it's filtered to add data to the message or remove data.

```lua
-- This code works as-is and turns all messages sent in the 'Main' channel into a red 'quack'.

local channel = api.channel:getByName("Main")
channel:registerMessageProcess("Quackify",function(obj,filtered)
    if not filtered then
        obj.message = "quack"
        obj.data.chatColor = Color3.fromRGB(255,0,0)
    end
end)
```

![](/files/AFZoy55XBx7HPCrRA5B3)

\--> :unregisterMessageProcess(\<string:name>)

Was the quackify function too overpowered? We have the power to undo that now!

```lua
channel:unregisterMessageProcess("Quackify") --> quackify is no more!
```

\--> :canSpeakerTalk(\<table:speaker>) -> \<boolean>

This method returns a boolean determining whether or not a speaker is in the specified channel.

```lua
if not channel:canSpeakerTalk(speaker) then
    print("rigged")
end
```

\--> :sendMessage(\<table:speaker>,\<string:text>,(?\<table:replyTo>)) --> \<table:message>

This method is used internally from [speaker:say()](/betterchat-v3/server/speaker.md#methods-1), and can be used as such:

```lua
local speaker = ...
local channel = ...

local msg = channel:sendMessage(speaker,"hi")
channel:sendMessage(speaker,"hello..",msg) --> Reply with "hello.."!
```

\-->:editMessage(\<integer:id>,\<string:text>)

This method can be used to edit an existing message by ID.

```lua
local msg = channel:sendMessage(speaker,"hey")
local id = msg.data.id --> id specifically

channel:editMessage(id,"goodbye") --> (hey -> goodbye)
```

\--> :getMessageById(\<integer:id>)

This method can be used to fetch a message by it's ID.

```lua
local byId = channel:getMessageById(12) --> message with ID '12' if it exists in that channel
```

\--> :Destroy(\<void>)

This method destroys the channel and disconnects everything.

```lua
channel:Destroy()
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jumpathy.gitbook.io/betterchat-v3/server/channel.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
