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