# 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 ](https://jumpathy.gitbook.io/betterchat-v3/server/getting-started)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)
```

![](https://3537671409-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FRUmEhzDEHjjqUvIxduhG%2Fuploads%2Fa3S7HlplkmSXJHm9rkTR%2Fimage.png?alt=media\&token=1a344e65-46b0-4087-9613-e81716a1278f)

\--> :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()](https://jumpathy.gitbook.io/betterchat-v3/speaker#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()
```
