github.com/diamondburned/arikawa@v1.3.14/api/channel.go (about) 1 package api 2 3 import ( 4 "github.com/diamondburned/arikawa/discord" 5 "github.com/diamondburned/arikawa/utils/httputil" 6 "github.com/diamondburned/arikawa/utils/json/option" 7 ) 8 9 var EndpointChannels = Endpoint + "channels/" 10 11 // Channels returns a list of guild channel objects. 12 func (c *Client) Channels(guildID discord.GuildID) ([]discord.Channel, error) { 13 var chs []discord.Channel 14 return chs, c.RequestJSON(&chs, "GET", EndpointGuilds+guildID.String()+"/channels") 15 } 16 17 // https://discord.com/developers/docs/resources/guild#create-guild-channel-json-params 18 type CreateChannelData struct { 19 // Name is the channel name (2-100 characters). 20 // 21 // Channel Type: All 22 Name string `json:"name"` 23 // Type is the type of channel. 24 // 25 // Channel Type: All 26 Type discord.ChannelType `json:"type,omitempty"` 27 // Topic is the channel topic (0-1024 characters). 28 // 29 // Channel Types: Text, News 30 Topic string `json:"topic,omitempty"` 31 // VoiceBitrate is the bitrate (in bits) of the voice channel. 32 // 8000 to 96000 (128000 for VIP servers) 33 // 34 // Channel Types: Voice 35 VoiceBitrate uint `json:"bitrate,omitempty"` 36 // VoiceUserLimit is the user limit of the voice channel. 37 // 0 refers to no limit, 1 to 99 refers to a user limit. 38 // 39 // Channel Types: Voice 40 VoiceUserLimit uint `json:"user_limit,omitempty"` 41 // UserRateLimit is the amount of seconds a user has to wait before sending 42 // another message (0-21600). 43 // Bots, as well as users with the permission manage_messages or 44 // manage_channel, are unaffected. 45 // 46 // Channel Types: Text 47 UserRateLimit discord.Seconds `json:"rate_limit_per_user,omitempty"` 48 // Position is the sorting position of the channel. 49 // 50 // Channel Types: All 51 Position option.Int `json:"position,omitempty"` 52 // Permissions are the channel's permission overwrites. 53 // 54 // Channel Types: All 55 Permissions []discord.Overwrite `json:"permission_overwrites,omitempty"` 56 // CategoryID is the id of the parent category for a channel. 57 // 58 // Channel Types: Text, News, Store, Voice 59 CategoryID discord.ChannelID `json:"parent_id,string,omitempty"` 60 // NSFW specifies whether the channel is nsfw. 61 // 62 // Channel Types: Text, News, Store. 63 NSFW bool `json:"nsfw,omitempty"` 64 } 65 66 // CreateChannel creates a new channel object for the guild. 67 // 68 // Requires the MANAGE_CHANNELS permission. 69 // Fires a Channel Create Gateway event. 70 func (c *Client) CreateChannel( 71 guildID discord.GuildID, data CreateChannelData) (*discord.Channel, error) { 72 var ch *discord.Channel 73 return ch, c.RequestJSON( 74 &ch, "POST", 75 EndpointGuilds+guildID.String()+"/channels", 76 httputil.WithJSONBody(data), 77 ) 78 } 79 80 type MoveChannelData struct { 81 // ID is the channel id. 82 ID discord.ChannelID `json:"id"` 83 // Position is the sorting position of the channel 84 Position option.Int `json:"position"` 85 } 86 87 // MoveChannel modifies the position of channels in the guild. 88 // 89 // Requires MANAGE_CHANNELS. 90 func (c *Client) MoveChannel(guildID discord.GuildID, data []MoveChannelData) error { 91 return c.FastRequest( 92 "PATCH", 93 EndpointGuilds+guildID.String()+"/channels", httputil.WithJSONBody(data), 94 ) 95 } 96 97 // Channel gets a channel by ID. Returns a channel object. 98 func (c *Client) Channel(channelID discord.ChannelID) (*discord.Channel, error) { 99 var channel *discord.Channel 100 return channel, c.RequestJSON(&channel, "GET", EndpointChannels+channelID.String()) 101 } 102 103 // https://discord.com/developers/docs/resources/channel#modify-channel-json-params 104 type ModifyChannelData struct { 105 // Name is the 2-100 character channel name. 106 // 107 // Channel Types: All 108 Name string `json:"name,omitempty"` 109 // Type is the type of the channel. 110 // Only conversion between text and news is supported and only in guilds 111 // with the "NEWS" feature 112 // 113 // Channel Types: Text, News 114 Type *discord.ChannelType `json:"type,omitempty"` 115 // Postion is the position of the channel in the left-hand listing 116 // 117 // Channel Types: All 118 Position option.NullableInt `json:"position,omitempty"` 119 // Topic is the 0-1024 character channel topic. 120 // 121 // Channel Types: Text, News 122 Topic option.NullableString `json:"topic,omitempty"` 123 // NSFW specifies whether the channel is nsfw. 124 // 125 // Channel Types: Text, News, Store. 126 NSFW option.NullableBool `json:"nsfw,omitempty"` 127 // UserRateLimit is the amount of seconds a user has to wait before sending 128 // another message (0-21600). 129 // Bots, as well as users with the permission manage_messages or 130 // manage_channel, are unaffected. 131 // 132 // Channel Types: Text 133 UserRateLimit option.NullableUint `json:"rate_limit_per_user,omitempty"` 134 // VoiceBitrate is the bitrate (in bits) of the voice channel. 135 // 8000 to 96000 (128000 for VIP servers) 136 // 137 // Channel Types: Voice 138 VoiceBitrate option.NullableUint `json:"bitrate,omitempty"` 139 // VoiceUserLimit is the user limit of the voice channel. 140 // 0 refers to no limit, 1 to 99 refers to a user limit. 141 // 142 // Channel Types: Voice 143 VoiceUserLimit option.NullableUint `json:"user_limit,omitempty"` 144 // Permissions are the channel or category-specific permissions. 145 // 146 // Channel Types: All 147 Permissions *[]discord.Overwrite `json:"permission_overwrites,omitempty"` 148 // CategoryID is the id of the new parent category for a channel. 149 // Channel Types: Text, News, Store, Voice 150 CategoryID discord.ChannelID `json:"parent_id,string,omitempty"` 151 } 152 153 // ModifyChannel updates a channel's settings. 154 // 155 // Requires the MANAGE_CHANNELS permission for the guild. 156 func (c *Client) ModifyChannel(channelID discord.ChannelID, data ModifyChannelData) error { 157 return c.FastRequest("PATCH", EndpointChannels+channelID.String(), httputil.WithJSONBody(data)) 158 } 159 160 // DeleteChannel deletes a channel, or closes a private message. Requires the 161 // MANAGE_CHANNELS permission for the guild. Deleting a category does not 162 // delete its child channels: they will have their parent_id removed and a 163 // Channel Update Gateway event will fire for each of them. 164 // 165 // Fires a Channel Delete Gateway event. 166 func (c *Client) DeleteChannel(channelID discord.ChannelID) error { 167 return c.FastRequest("DELETE", EndpointChannels+channelID.String()) 168 } 169 170 // https://discord.com/developers/docs/resources/channel#edit-channel-permissions-json-params 171 type EditChannelPermissionData struct { 172 // Type is either "role" or "member". 173 Type discord.OverwriteType `json:"type"` 174 // Allow is a permission bit set for granted permissions. 175 Allow discord.Permissions `json:"allow,string"` 176 // Deny is a permission bit set for denied permissions. 177 Deny discord.Permissions `json:"deny,string"` 178 } 179 180 // EditChannelPermission edits the channel's permission overwrites for a user 181 // or role in a channel. Only usable for guild channels. 182 // 183 // Requires the MANAGE_ROLES permission. 184 func (c *Client) EditChannelPermission( 185 channelID discord.ChannelID, overwriteID discord.Snowflake, data EditChannelPermissionData) error { 186 187 return c.FastRequest( 188 "PUT", EndpointChannels+channelID.String()+"/permissions/"+overwriteID.String(), 189 httputil.WithJSONBody(data), 190 ) 191 } 192 193 // DeleteChannelPermission deletes a channel permission overwrite for a user or 194 // role in a channel. Only usable for guild channels. 195 // 196 // Requires the MANAGE_ROLES permission. 197 func (c *Client) DeleteChannelPermission(channelID discord.ChannelID, overwriteID discord.Snowflake) error { 198 return c.FastRequest( 199 "DELETE", 200 EndpointChannels+channelID.String()+"/permissions/"+overwriteID.String(), 201 ) 202 } 203 204 // Typing posts a typing indicator to the channel. Undocumented, but the client 205 // usually clears the typing indicator after 8-10 seconds (or after a message). 206 func (c *Client) Typing(channelID discord.ChannelID) error { 207 return c.FastRequest("POST", EndpointChannels+channelID.String()+"/typing") 208 } 209 210 // PinnedMessages returns all pinned messages in the channel as an array of 211 // message objects. 212 func (c *Client) PinnedMessages(channelID discord.ChannelID) ([]discord.Message, error) { 213 var pinned []discord.Message 214 return pinned, c.RequestJSON(&pinned, "GET", EndpointChannels+channelID.String()+"/pins") 215 } 216 217 // PinMessage pins a message in a channel. 218 // 219 // Requires the MANAGE_MESSAGES permission. 220 func (c *Client) PinMessage(channelID discord.ChannelID, messageID discord.MessageID) error { 221 return c.FastRequest("PUT", EndpointChannels+channelID.String()+"/pins/"+messageID.String()) 222 } 223 224 // UnpinMessage deletes a pinned message in a channel. 225 // 226 // Requires the MANAGE_MESSAGES permission. 227 func (c *Client) UnpinMessage(channelID discord.ChannelID, messageID discord.MessageID) error { 228 return c.FastRequest("DELETE", EndpointChannels+channelID.String()+"/pins/"+messageID.String()) 229 } 230 231 // AddRecipient adds a user to a group direct message. As accessToken is needed, 232 // clearly this endpoint should only be used for OAuth. AccessToken can be 233 // obtained with the "gdm.join" scope. 234 func (c *Client) AddRecipient(channelID discord.ChannelID, userID discord.UserID, accessToken, nickname string) error { 235 236 var params struct { 237 AccessToken string `json:"access_token"` 238 Nickname string `json:"nickname"` 239 } 240 241 params.AccessToken = accessToken 242 params.Nickname = nickname 243 244 return c.FastRequest( 245 "PUT", 246 EndpointChannels+channelID.String()+"/recipients/"+userID.String(), 247 httputil.WithJSONBody(params), 248 ) 249 } 250 251 // RemoveRecipient removes a user from a group direct message. 252 func (c *Client) RemoveRecipient(channelID discord.ChannelID, userID discord.UserID) error { 253 return c.FastRequest( 254 "DELETE", 255 EndpointChannels+channelID.String()+"/recipients/"+userID.String(), 256 ) 257 } 258 259 // Ack is the read state of a channel. This is undocumented. 260 type Ack struct { 261 Token string `json:"token"` 262 } 263 264 // Ack marks the read state of a channel. This is undocumented. The method will 265 // write to the ack variable passed in. If this method is called asynchronously, 266 // then ack should be mutex guarded. 267 func (c *Client) Ack(channelID discord.ChannelID, messageID discord.MessageID, ack *Ack) error { 268 return c.RequestJSON( 269 ack, "POST", 270 EndpointChannels+channelID.String()+"/messages/"+messageID.String()+"/ack", 271 httputil.WithJSONBody(ack), 272 ) 273 }