View Source Nostrum.Struct.Channel (Nostrum v0.10.0)
Struct and helper functions for working with channels.
Channel Struct
The channel struct is used by Nostrum to represent a Discord Channel Object. More information can be found on the Discord API Channel Documentation.
The struct can have one of several forms depending on the type of channel. You can read more about the individual channel types below.
A typical channel would appear as:
%Nostrum.Struct.Channel{
guild_id: 766435015768539156,
id: 827333533688397865,
name: "announcements",
nsfw: false,
permission_overwrites: [],
position: 1,
type: 5,
}
The channel struct implements String.Chars
protocol through the mention/1
function. This example uses our channel from the previous code block.
channel |> to_string()
"<#766435015768539156>"
Channel Caching
Channels are cached within the guild object they are a member of. To fetch a cached channel you should use the Nostrum.Cache.ChannelGuildMapping
cache to map a cache to a guild ID.
Once you have a guild ID, you can use the Nostrum.Cache.GuildCache
to fetch the guild and all channels, then use the Nostrum.Struct.Guild.channels/0
field to find the channel.
Note on DM Channels
The
Nostrum.Cache.ChannelGuildMapping
by design only caches a mapping of channels to their respective guilds, and the stored channel object sits within theNostrum.Cache.GuildCache
.Due to how Direct Message channels work (no
CHANNEL_CREATE
events, not sent in any gateway events, etc.), we make no attempt to store direct message channels within any of the built-in Nostrum caching adapters.If you wish to implement this functionality yourself, you will have to fetch the channel details from the API by looking at the
channel_id
field of received message objects, you can then store these details in your own choice of caching system (ETS, Mnesia, etc.).
Example
> guild_id = Nostrum.Cache.ChannelGuildMapping.get(1229955694258684005)
1226944827137069107
> {:ok, guild} = Nostrum.Cache.GuildCache.get(guild_id)
{:ok,
%Nostrum.Struct.Guild{
id: 1226944827137069107,
name: "Craig Cat Zone",
...
}}
> guild.channels[channel_id].name
"cat-general"
Helper Functions
This module contains two functions for assisting with channel structs. mention/1
to convert the channel into a mention as a string, and link/1
to convert the channel into a hyperlink as a string. Further details and examples are detailed in the Functions section of this module.
Api Functions
The Nostrum Api contains numerous functions related to channels. Notable functions relating to channels are shown below.
Nostrum.Api.create_guild_channel/2
Nostrum.Api.get_channel/1
Nostrum.Api.modify_channel/3
Nostrum.Api.delete_channel/2
Nostrum.Api.add_pinned_channel_message/2
Nostrum.Api.create_channel_invite/3
Nostrum.Api.get_guild_channels/1
Nostrum.Api.modify_guild_channel_positions/2
Note: This is not an exhaustive list, for full details please see the
Nostrum.Api
module documentation.
Channel Types
Channels take the shape of various types depending on their use and not all fields are always used. The currently implemented channel types are detailed below. The type of channel is determined by the :type
field.
This diagram represents the regular channel types 0
, 2
, 5
and 13
.
The currently implemented channel types are:
Channel Type | ||
---|---|---|
0 | GUILD_TEXT | A text channel within a server |
1 | DM | A direct message between users |
2 | GUILD_VOICE | A voice channel within a server |
3 | GROUP_DM | A direct message between multiple users |
4 | GUILD_CATEGORY | A category that contains up to 50 channels |
5 | GUILD_NEWS | A channel that users can follow and crosspost |
6 | GUILD_STORE | A channel to sell games on Discord |
10 | GUILD_NEWS_THREAD | A temporary sub-channel within a news channel |
11 | GUILD_PUBLIC_THREAD | A temporary sub-channel within a text channel |
12 | GUILD_PRIVATE_THREAD | A temporary private sub-channel within a text channel |
13 | GUILD_STAGE_VOICE | A voice channel for hosting events with an audience |
15 | GUILD_FORUM | _A channel that can only contain threads |
You can use one of the Nostrum.Constants.ChannelType
methods.
More information about Discord Channel Types can be found on the Discord API Channel Type Documentation.
Summary
Types
The id of the application that created a group direct message or thread.
When the thread was archived.
The threads archived status.
Archive duration for the thread in minutes.
The bitate of the voice channel.
A partial channel object representing a channel mention.
Default duration for newly created threads in minutes.
The default layout for posts in a GUILD_FORUM
channel.
An object that specifies the emoji to use as the default way to react to a forum post.
The default sort order for posts in a GUILD_FORUM
channel.
The :rate_limit_per_user
which will be applied to threads created in the channel, in seconds.
Type 1 partial channel object representing a direct message.
User thread settings, currently only used for notifications.
A map representing a tag for use in forum channels.
Type 3 partial channel object representing a group direct message.
Type 4 partial channel object representing a channel category.
Guild channel types
Type 15 a guild forum channel.
The id of the guild the channel is located in.
Type 5 partial channel object representing a news channel.
Type 10 partial channel object representing a news thread.
Type 12 partial channel object representing a private thread.
Type 11 partial channel object representing a standard thread.
Type 13 channel object representing a stage channel.
Type 6 partial channel object representing a store channel.
Type 0 partial channel object representing a text channel within a guild.
Type 2 partial channel object representing an audio channel within a guild.
The hash of the channels icon.
The id of the channel object.
When the user joined the thread.
The id of the last message sent in the channel.
Timestamp for the last pinned message.
The threads locked status.
Present when the bot joins a thread.
Approximate count of members in a thread, capped at 50.
Approximate count of messages in a thread, capped at 50.
The name of the channel.
Included only in the THREAD_CREATE
event.
Whether the NSFW setting is enabled for this channel.
The id of the user of a group direct message or thread.
The id of the parent channel that this channel is located under.
A list of permission overwrites applied to the channel.
Computed permissions of the invoking user.
The position of the channel in the sidebar of the guild.
The users rate limit.
A list of users in a group DM.
Region id for the channel.
All valid channel types.
All valid text channels.
Thread-specific fields not needed by other channels.
The topic of the channel.
The type of channel.
User id of the threads creator.
The user limit of a voice channel.
The video quality mode of the channel.
A Nostrum.Struct.Channel
that represents a voice channel
Functions
Convert a channel into a mention.
Types
@type application_id() :: Nostrum.Snowflake.t() | nil
The id of the application that created a group direct message or thread.
This applies to bot created channels.
@type applied_tags() :: [Nostrum.Snowflake.t()] | nil
@type archive_timestamp() :: DateTime.t() | nil
When the thread was archived.
@type archived() :: boolean()
The threads archived status.
@type auto_archive_duration() :: integer()
Archive duration for the thread in minutes.
- 60, 1 hour
- 1440, 24 hours
- 4320, 3 days
- 10080, 7 days
@type bitrate() :: integer()
The bitate of the voice channel.
@type channel_mention() :: %Nostrum.Struct.Channel{ application_id: term(), applied_tags: term(), available_tags: term(), bitrate: term(), default_auto_archive_duration: term(), default_forum_layout: term(), default_reaction_emoji: term(), default_sort_order: term(), default_thread_rate_limit_per_user: term(), guild_id: guild_id(), icon: term(), id: id(), last_message_id: term(), last_pin_timestamp: term(), member: term(), member_count: term(), message_count: term(), name: name(), newly_created: term(), nsfw: term(), owner_id: term(), parent_id: term(), permission_overwrites: term(), permissions: term(), position: term(), rate_limit_per_user: term(), recipients: term(), rtc_region: term(), thread_metadata: term(), topic: term(), type: type(), user_limit: term(), video_quality_mode: term() }
A partial channel object representing a channel mention.
More information about the Discord Channel Mention Object can be found at the Discord API Channel Mention Object Documentation.
@type default_auto_archive_duration() :: integer()
Default duration for newly created threads in minutes.
- 60, 1 hour
- 1440, 24 hours
- 4320, 3 days
- 10080, 7 days
@type default_forum_layout() :: integer() | nil
The default layout for posts in a GUILD_FORUM
channel.
@type default_reaction_emoji() :: %{emoji_id: Nostrum.Snowflake.t() | nil, emoji_name: String.t() | nil} | nil
An object that specifies the emoji to use as the default way to react to a forum post.
:emoji_id
and :emoji_name
are mutually exclusive
@type default_sort_order() :: integer() | nil
The default sort order for posts in a GUILD_FORUM
channel.
@type default_thread_rate_limit_per_user() :: integer() | nil
The :rate_limit_per_user
which will be applied to threads created in the channel, in seconds.
@type dm_channel() :: %Nostrum.Struct.Channel{ application_id: term(), applied_tags: term(), available_tags: term(), bitrate: term(), default_auto_archive_duration: term(), default_forum_layout: term(), default_reaction_emoji: term(), default_sort_order: term(), default_thread_rate_limit_per_user: term(), guild_id: term(), icon: term(), id: id(), last_message_id: last_message_id(), last_pin_timestamp: last_pin_timestamp(), member: term(), member_count: term(), message_count: term(), name: term(), newly_created: term(), nsfw: term(), owner_id: term(), parent_id: term(), permission_overwrites: term(), permissions: term(), position: term(), rate_limit_per_user: term(), recipients: recipients(), rtc_region: term(), thread_metadata: term(), topic: term(), type: 1, user_limit: term(), video_quality_mode: term() }
Type 1 partial channel object representing a direct message.
@type flags() :: integer()
User thread settings, currently only used for notifications.
@type forum_tag() :: %{ id: Nostrum.Snowflake.t(), name: String.t(), moderated: boolean(), emoji_id: Nostrum.Snowflake.t() | nil, emoji_name: String.t() | nil }
A map representing a tag for use in forum channels.
:moderated
indicates whether the tag can only be added or removed by moderators.
:emoji_id
and :emoji_name
are mutually exclusive and indicate the emoji used to represent the tag.
@type group_dm_channel() :: %Nostrum.Struct.Channel{ application_id: application_id(), applied_tags: term(), available_tags: term(), bitrate: term(), default_auto_archive_duration: term(), default_forum_layout: term(), default_reaction_emoji: term(), default_sort_order: term(), default_thread_rate_limit_per_user: term(), guild_id: term(), icon: icon(), id: id(), last_message_id: last_message_id(), last_pin_timestamp: term(), member: term(), member_count: term(), message_count: term(), name: name(), newly_created: term(), nsfw: term(), owner_id: owner_id(), parent_id: term(), permission_overwrites: term(), permissions: term(), position: term(), rate_limit_per_user: term(), recipients: recipients(), rtc_region: term(), thread_metadata: term(), topic: term(), type: 3, user_limit: term(), video_quality_mode: term() }
Type 3 partial channel object representing a group direct message.
@type guild_category_channel() :: %Nostrum.Struct.Channel{ application_id: term(), applied_tags: term(), available_tags: term(), bitrate: term(), default_auto_archive_duration: term(), default_forum_layout: term(), default_reaction_emoji: term(), default_sort_order: term(), default_thread_rate_limit_per_user: term(), guild_id: guild_id(), icon: term(), id: id(), last_message_id: term(), last_pin_timestamp: term(), member: term(), member_count: term(), message_count: term(), name: name(), newly_created: term(), nsfw: nsfw(), owner_id: term(), parent_id: parent_id(), permission_overwrites: permission_overwrites(), permissions: term(), position: position(), rate_limit_per_user: term(), recipients: term(), rtc_region: term(), thread_metadata: term(), topic: term(), type: 4, user_limit: term(), video_quality_mode: term() }
Type 4 partial channel object representing a channel category.
Note: Other channels
parent_id
field refers to this type of object.
@type guild_channel() :: guild_text_channel() | guild_voice_channel() | guild_category_channel()
Guild channel types
@type guild_forum_channel() :: %Nostrum.Struct.Channel{ application_id: term(), applied_tags: term(), available_tags: [forum_tag()], bitrate: term(), default_auto_archive_duration: term(), default_forum_layout: default_forum_layout(), default_reaction_emoji: default_reaction_emoji(), default_sort_order: default_sort_order(), default_thread_rate_limit_per_user: default_thread_rate_limit_per_user(), guild_id: guild_id(), icon: term(), id: id(), last_message_id: last_message_id(), last_pin_timestamp: term(), member: term(), member_count: term(), message_count: term(), name: name(), newly_created: term(), nsfw: nsfw(), owner_id: term(), parent_id: parent_id(), permission_overwrites: permission_overwrites(), permissions: term(), position: position(), rate_limit_per_user: rate_limit_per_user(), recipients: term(), rtc_region: term(), thread_metadata: term(), topic: term(), type: type(), user_limit: term(), video_quality_mode: term() }
Type 15 a guild forum channel.
@type guild_id() :: Nostrum.Struct.Guild.id()
The id of the guild the channel is located in.
@type guild_news_channel() :: %Nostrum.Struct.Channel{ application_id: term(), applied_tags: term(), available_tags: term(), bitrate: term(), default_auto_archive_duration: default_auto_archive_duration(), default_forum_layout: term(), default_reaction_emoji: term(), default_sort_order: term(), default_thread_rate_limit_per_user: term(), guild_id: guild_id(), icon: term(), id: id(), last_message_id: last_message_id(), last_pin_timestamp: term(), member: term(), member_count: term(), message_count: term(), name: name(), newly_created: term(), nsfw: nsfw(), owner_id: term(), parent_id: parent_id(), permission_overwrites: permission_overwrites(), permissions: term(), position: position(), rate_limit_per_user: term(), recipients: term(), rtc_region: term(), thread_metadata: term(), topic: topic(), type: 5, user_limit: term(), video_quality_mode: term() }
Type 5 partial channel object representing a news channel.
@type guild_news_thread_channel() :: %Nostrum.Struct.Channel{ application_id: term(), applied_tags: term(), available_tags: term(), bitrate: term(), default_auto_archive_duration: term(), default_forum_layout: term(), default_reaction_emoji: term(), default_sort_order: term(), default_thread_rate_limit_per_user: term(), guild_id: guild_id(), icon: term(), id: id(), last_message_id: last_message_id(), last_pin_timestamp: term(), member: term(), member_count: member_count(), message_count: message_count(), name: name(), newly_created: newly_created(), nsfw: term(), owner_id: owner_id(), parent_id: parent_id(), permission_overwrites: term(), permissions: term(), position: term(), rate_limit_per_user: rate_limit_per_user(), recipients: term(), rtc_region: term(), thread_metadata: thread_metadata(), topic: term(), type: type(), user_limit: term(), video_quality_mode: term() }
Type 10 partial channel object representing a news thread.
@type guild_private_thread_channel() :: %Nostrum.Struct.Channel{ application_id: term(), applied_tags: term(), available_tags: term(), bitrate: term(), default_auto_archive_duration: term(), default_forum_layout: term(), default_reaction_emoji: term(), default_sort_order: term(), default_thread_rate_limit_per_user: term(), guild_id: guild_id(), icon: term(), id: id(), last_message_id: last_message_id(), last_pin_timestamp: term(), member: term(), member_count: member_count(), message_count: message_count(), name: name(), newly_created: newly_created(), nsfw: term(), owner_id: owner_id(), parent_id: parent_id(), permission_overwrites: term(), permissions: term(), position: term(), rate_limit_per_user: rate_limit_per_user(), recipients: term(), rtc_region: term(), thread_metadata: thread_metadata(), topic: term(), type: type(), user_limit: term(), video_quality_mode: term() }
Type 12 partial channel object representing a private thread.
@type guild_public_thread_channel() :: %Nostrum.Struct.Channel{ application_id: term(), applied_tags: applied_tags(), available_tags: term(), bitrate: term(), default_auto_archive_duration: term(), default_forum_layout: term(), default_reaction_emoji: term(), default_sort_order: term(), default_thread_rate_limit_per_user: term(), guild_id: guild_id(), icon: term(), id: id(), last_message_id: last_message_id(), last_pin_timestamp: term(), member: term(), member_count: member_count(), message_count: message_count(), name: name(), newly_created: newly_created(), nsfw: term(), owner_id: owner_id(), parent_id: parent_id(), permission_overwrites: term(), permissions: term(), position: term(), rate_limit_per_user: rate_limit_per_user(), recipients: term(), rtc_region: term(), thread_metadata: thread_metadata(), topic: term(), type: type(), user_limit: term(), video_quality_mode: term() }
Type 11 partial channel object representing a standard thread.
@type guild_stage_voice_channel() :: %Nostrum.Struct.Channel{ application_id: term(), applied_tags: term(), available_tags: term(), bitrate: term(), default_auto_archive_duration: term(), default_forum_layout: term(), default_reaction_emoji: term(), default_sort_order: term(), default_thread_rate_limit_per_user: term(), guild_id: guild_id(), icon: term(), id: id(), last_message_id: last_message_id(), last_pin_timestamp: term(), member: term(), member_count: member_count(), message_count: message_count(), name: name(), newly_created: term(), nsfw: term(), owner_id: owner_id(), parent_id: parent_id(), permission_overwrites: term(), permissions: term(), position: term(), rate_limit_per_user: rate_limit_per_user(), recipients: term(), rtc_region: term(), thread_metadata: thread_metadata(), topic: term(), type: type(), user_limit: term(), video_quality_mode: term() }
Type 13 channel object representing a stage channel.
@type guild_store_channel() :: %Nostrum.Struct.Channel{ application_id: term(), applied_tags: term(), available_tags: term(), bitrate: term(), default_auto_archive_duration: term(), default_forum_layout: term(), default_reaction_emoji: term(), default_sort_order: term(), default_thread_rate_limit_per_user: term(), guild_id: guild_id(), icon: term(), id: id(), last_message_id: term(), last_pin_timestamp: term(), member: term(), member_count: term(), message_count: term(), name: name(), newly_created: term(), nsfw: nsfw(), owner_id: term(), parent_id: parent_id(), permission_overwrites: permission_overwrites(), permissions: term(), position: position(), rate_limit_per_user: term(), recipients: term(), rtc_region: term(), thread_metadata: term(), topic: term(), type: type(), user_limit: term(), video_quality_mode: term() }
Type 6 partial channel object representing a store channel.
@type guild_text_channel() :: %Nostrum.Struct.Channel{ application_id: term(), applied_tags: term(), available_tags: term(), bitrate: term(), default_auto_archive_duration: default_auto_archive_duration(), default_forum_layout: term(), default_reaction_emoji: term(), default_sort_order: term(), default_thread_rate_limit_per_user: term(), guild_id: guild_id(), icon: term(), id: id(), last_message_id: last_message_id(), last_pin_timestamp: term(), member: term(), member_count: term(), message_count: term(), name: name(), newly_created: term(), nsfw: nsfw(), owner_id: term(), parent_id: parent_id(), permission_overwrites: permission_overwrites(), permissions: term(), position: position(), rate_limit_per_user: rate_limit_per_user(), recipients: term(), rtc_region: term(), thread_metadata: term(), topic: topic(), type: type(), user_limit: term(), video_quality_mode: term() }
Type 0 partial channel object representing a text channel within a guild.
@type guild_voice_channel() :: %Nostrum.Struct.Channel{ application_id: term(), applied_tags: term(), available_tags: term(), bitrate: bitrate(), default_auto_archive_duration: term(), default_forum_layout: term(), default_reaction_emoji: term(), default_sort_order: term(), default_thread_rate_limit_per_user: term(), guild_id: guild_id(), icon: term(), id: id(), last_message_id: term(), last_pin_timestamp: term(), member: term(), member_count: term(), message_count: term(), name: name(), newly_created: term(), nsfw: nsfw(), owner_id: term(), parent_id: parent_id(), permission_overwrites: permission_overwrites(), permissions: term(), position: position(), rate_limit_per_user: term(), recipients: term(), rtc_region: rtc_region(), thread_metadata: term(), topic: term(), type: 2, user_limit: user_limit(), video_quality_mode: term() }
Type 2 partial channel object representing an audio channel within a guild.
@type icon() :: String.t() | nil
The hash of the channels icon.
@type id() :: Nostrum.Snowflake.t()
The id of the channel object.
@type join_timestamp() :: DateTime.t()
When the user joined the thread.
@type last_message_id() :: Nostrum.Struct.Message.id() | nil
The id of the last message sent in the channel.
For GUILD_FORUM
channels, this is the last thread created in the channel.
@type last_pin_timestamp() :: DateTime.t() | nil
Timestamp for the last pinned message.
@type locked() :: boolean()
The threads locked status.
@type member() :: %{ id: id(), user_id: user_id(), join_timestamp: join_timestamp(), flags: flags() }
Present when the bot joins a thread.
Note: This is omitted on threads that the bot can immediately access on :GUILD_CREATE
events received.
@type member_count() :: integer() | nil
Approximate count of members in a thread, capped at 50.
@type message_count() :: integer() | nil
Approximate count of messages in a thread, capped at 50.
@type name() :: String.t()
The name of the channel.
@type newly_created() :: boolean() | nil
Included only in the THREAD_CREATE
event.
@type nsfw() :: boolean()
Whether the NSFW setting is enabled for this channel.
@type owner_id() :: Nostrum.Struct.User.id()
The id of the user of a group direct message or thread.
This applies to user created channels.
@type parent_id() :: id() | nil
The id of the parent channel that this channel is located under.
For threads, that is the channel that contains the thread. For regular channels, it is the category that the channel is located under.
@type permission_overwrites() :: [Nostrum.Struct.Overwrite.t()]
A list of permission overwrites applied to the channel.
@type permissions() :: String.t()
Computed permissions of the invoking user.
Permissions for the invoking user in the channel, including overwrites, only included when part of the resolved data received on a slash command interaction
@type position() :: integer()
The position of the channel in the sidebar of the guild.
@type rate_limit_per_user() :: integer() | nil
The users rate limit.
Amount of seconds a user has to wait before sending another message (0-21600); bots, as well as users with the permission manage_messages or manage_channel, are unaffected
@type recipients() :: [Nostrum.Struct.User.t()]
A list of users in a group DM.
@type rtc_region() :: String.t() | nil
Region id for the channel.
More information about region ids can be found on the Discord API Voice Region Object Documentation.
@type t() :: guild_text_channel() | dm_channel() | guild_voice_channel() | group_dm_channel() | guild_category_channel() | guild_news_channel() | guild_store_channel() | guild_news_thread_channel() | guild_public_thread_channel() | guild_private_thread_channel() | guild_stage_voice_channel() | guild_forum_channel()
All valid channel types.
@type text_channel() :: guild_text_channel() | dm_channel() | group_dm_channel()
All valid text channels.
@type thread_metadata() :: %{ archived: archived(), auto_archive_duration: auto_archive_duration(), archive_timestamp: archive_timestamp(), locked: boolean(), invitable: boolean() | nil, create_timestamp: DateTime.t() | nil }
Thread-specific fields not needed by other channels.
@type topic() :: String.t()
The topic of the channel.
@type type() :: integer()
The type of channel.
More information about Discord Channel Types can be found under the types
on the Discord API Channel Type Documentation.
@type user_id() :: Nostrum.Snowflake.t() | nil
User id of the threads creator.
@type user_limit() :: integer()
The user limit of a voice channel.
@type video_quality_mode() :: integer() | nil
The video quality mode of the channel.
More information about video quality modes can be found on the Discord API Video Quality Mode Documentation.
@type voice_channel() :: guild_voice_channel()
A Nostrum.Struct.Channel
that represents a voice channel
Functions
Convert a channel into a mention.
Handles the conversion of a Nostrum.Struct.Channel
into the required format to mention the channel within a message. Mentioning the channel will provide a clickable link to take the user to the channel.
Parameters
- channel:
Nostrum.Struct.Channel.t/0
Examples
channel
|> Nostrum.Struct.Channel.mention()
"<#381889573426429952>"