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