github.com/diamondburned/arikawa@v1.3.14/discord/channel.go (about)

     1  package discord
     2  
     3  import "github.com/diamondburned/arikawa/utils/json"
     4  
     5  // https://discord.com/developers/docs/resources/channel#channel-object
     6  type Channel struct {
     7  	// ID is the id of this channel.
     8  	ID ChannelID `json:"id,string"`
     9  	// Type is the type of channel.
    10  	Type ChannelType `json:"type"`
    11  	// GuildID is the id of the guild.
    12  	GuildID GuildID `json:"guild_id,string,omitempty"`
    13  
    14  	// Position is the sorting position of the channel.
    15  	Position int `json:"position,omitempty"`
    16  	// Permissions are the explicit permission overrides for members and roles.
    17  	Permissions []Overwrite `json:"permission_overwrites,omitempty"`
    18  
    19  	// Name is the name of the channel (2-100 characters).
    20  	Name string `json:"name,omitempty"`
    21  	// Topic is the channel topic (0-1024 characters).
    22  	Topic string `json:"topic,omitempty"`
    23  	// NSFW specifies whether the channel is nsfw.
    24  	NSFW bool `json:"nsfw"`
    25  
    26  	// LastMessageID is the id of the last message sent in this channel (may
    27  	// not point to an existing or valid message).
    28  	LastMessageID MessageID `json:"last_message_id,string,omitempty"`
    29  
    30  	// VoiceBitrate is the bitrate (in bits) of the voice channel.
    31  	VoiceBitrate uint `json:"bitrate,omitempty"`
    32  	// VoiceUserLimit is the user limit of the voice channel.
    33  	VoiceUserLimit uint `json:"user_limit,omitempty"`
    34  
    35  	// UserRateLimit is the amount of seconds a user has to wait before sending
    36  	// another message (0-21600). Bots, as well as users with the permission
    37  	// manage_messages or manage_channel, are unaffected.
    38  	UserRateLimit Seconds `json:"rate_limit_per_user,omitempty"`
    39  
    40  	// DMRecipients are the recipients of the DM.
    41  	DMRecipients []User `json:"recipients,omitempty"`
    42  	// Icon is the icon hash.
    43  	Icon Hash `json:"icon,omitempty"`
    44  	// DMOwnerID is the id of the DM creator.
    45  	DMOwnerID UserID `json:"owner_id,string,omitempty"`
    46  
    47  	// AppID is the application id of the group DM creator if it is
    48  	// bot-created.
    49  	AppID AppID `json:"application_id,string,omitempty"`
    50  
    51  	// CategoryID is the id of the parent category for a channel (each parent
    52  	// category can contain up to 50 channels).
    53  	CategoryID ChannelID `json:"parent_id,string,omitempty"`
    54  	// LastPinTime is when the last pinned message was pinned.
    55  	LastPinTime Timestamp `json:"last_pin_timestamp,omitempty"`
    56  }
    57  
    58  // Mention returns a mention of the channel.
    59  func (ch Channel) Mention() string {
    60  	return ch.ID.Mention()
    61  }
    62  
    63  // IconURL returns the URL to the channel icon in the PNG format.
    64  // An empty string is returned if there's no icon.
    65  func (ch Channel) IconURL() string {
    66  	return ch.IconURLWithType(PNGImage)
    67  }
    68  
    69  // IconURLWithType returns the URL to the channel icon using the passed
    70  // ImageType. An empty string is returned if there's no icon.
    71  //
    72  // Supported ImageTypes: PNG, JPEG, WebP
    73  func (ch Channel) IconURLWithType(t ImageType) string {
    74  	if ch.Icon == "" {
    75  		return ""
    76  	}
    77  
    78  	return "https://cdn.discordapp.com/channel-icons/" +
    79  		ch.ID.String() + "/" + t.format(ch.Icon)
    80  }
    81  
    82  type ChannelType uint8
    83  
    84  // https://discord.com/developers/docs/resources/channel#channel-object-channel-types
    85  var (
    86  	// GuildText is a text channel within a server.
    87  	GuildText ChannelType = 0
    88  	// DirectMessage is a direct message between users.
    89  	DirectMessage ChannelType = 1
    90  	// GuildVoice is a voice channel within a server.
    91  	GuildVoice ChannelType = 2
    92  	// GroupDM is a direct message between multiple users.
    93  	GroupDM ChannelType = 3
    94  	// GuildCategory is an organizational category that contains up to 50
    95  	// channels.
    96  	GuildCategory ChannelType = 4
    97  	// GuildNews is a channel that users can follow and crosspost into their
    98  	// own server.
    99  	GuildNews ChannelType = 5
   100  	// GuildStore is a channel in which game developers can sell their game on
   101  	// Discord.
   102  	GuildStore ChannelType = 6
   103  )
   104  
   105  // https://discord.com/developers/docs/resources/channel#overwrite-object
   106  type Overwrite struct {
   107  	// ID is the role or user id.
   108  	ID Snowflake `json:"id"`
   109  	// Type is either "role" or "member".
   110  	Type OverwriteType `json:"type"`
   111  	// Allow is a permission bit set for granted permissions.
   112  	Allow Permissions `json:"allow,string"`
   113  	// Deny is a permission bit set for denied permissions.
   114  	Deny Permissions `json:"deny,string"`
   115  }
   116  
   117  // UnmarshalJSON unmarshals the passed json data into the Overwrite.
   118  // This is necessary because Discord has different names for fields when
   119  // sending than receiving.
   120  func (o *Overwrite) UnmarshalJSON(data []byte) (err error) {
   121  	var recv struct {
   122  		ID    Snowflake     `json:"id"`
   123  		Type  OverwriteType `json:"type"`
   124  		Allow Permissions   `json:"allow_new,string"`
   125  		Deny  Permissions   `json:"deny_new,string"`
   126  	}
   127  
   128  	err = json.Unmarshal(data, &recv)
   129  	if err != nil {
   130  		return
   131  	}
   132  
   133  	o.ID = recv.ID
   134  	o.Type = recv.Type
   135  	o.Allow = recv.Allow
   136  	o.Deny = recv.Deny
   137  
   138  	return
   139  }
   140  
   141  type OverwriteType string
   142  
   143  const (
   144  	// OverwriteRole is an overwrite for a role.
   145  	OverwriteRole OverwriteType = "role"
   146  	// OverwriteMember is an overwrite for a member.
   147  	OverwriteMember OverwriteType = "member"
   148  )