1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-17 20:32:48 +00:00
arikawa/api/channel.go

184 lines
5.8 KiB
Go

package api
import (
"github.com/diamondburned/arikawa/discord"
"github.com/diamondburned/arikawa/utils/httputil"
"github.com/diamondburned/arikawa/utils/json/option"
)
var EndpointChannels = Endpoint + "channels/"
func (c *Client) Channels(
guildID discord.Snowflake) ([]discord.Channel, error) {
var chs []discord.Channel
return chs, c.RequestJSON(&chs, "GET",
EndpointGuilds+guildID.String()+"/channels")
}
type CreateChannelData struct {
Name string `json:"name"` // 2-100
Topic string `json:"topic,omitempty"`
Type discord.ChannelType `json:"type,omitempty"`
VoiceBitrate uint `json:"bitrate,omitempty"`
VoiceUserLimit uint `json:"user_limit,omitempty"`
UserRateLimit discord.Seconds `json:"rate_limit_per_user,omitempty"`
NSFW bool `json:"nsfw,omitempty"`
Position option.Int `json:"position,omitempty"`
Permissions []discord.Overwrite `json:"permission_overwrites,omitempty"`
CategoryID discord.Snowflake `json:"parent_id,string,omitempty"`
}
func (c *Client) CreateChannel(
guildID discord.Snowflake,
data CreateChannelData) (*discord.Channel, error) {
var ch *discord.Channel
return ch, c.RequestJSON(
&ch, "POST",
EndpointGuilds+guildID.String()+"/channels",
httputil.WithJSONBody(data),
)
}
type MoveChannelData struct {
ID discord.Snowflake `json:"id"`
Position option.Int `json:"position"`
}
// MoveChannel modifies the position of channels in the guild. Requires
// MANAGE_CHANNELS.
func (c *Client) MoveChannel(guildID discord.Snowflake, datum []MoveChannelData) error {
return c.FastRequest(
"PATCH",
EndpointGuilds+guildID.String()+"/channels",
httputil.WithJSONBody(datum),
)
}
func (c *Client) Channel(channelID discord.Snowflake) (*discord.Channel, error) {
var channel *discord.Channel
return channel, c.RequestJSON(&channel, "GET", EndpointChannels+channelID.String())
}
type ModifyChannelData struct {
// All types
Name string `json:"name,omitempty"`
// Type allows conversions between text and news channels.
Type *discord.ChannelType `json:"type,omitempty"`
Position option.Int `json:"position,omitempty"`
Permissions *[]discord.Overwrite `json:"permission_overwrites,omitempty"`
// Text only
Topic option.String `json:"topic,omitempty"`
NSFW option.Bool `json:"nsfw,omitempty"`
// 0-21600 seconds, refer to (discord.Channel).UserRateLimit.
UserRateLimit option.Int `json:"rate_limit_per_user,omitempty"`
// Voice only
// 8000 - 96000 (or 128000 for Nitro)
VoiceBitrate option.Uint `json:"bitrate,omitempty"`
// 0 no limit, 1-99
VoiceUserLimit option.Uint `json:"user_limit,omitempty"`
// Text OR Voice
CategoryID discord.Snowflake `json:"parent_id,string,omitempty"`
}
func (c *Client) ModifyChannel(channelID discord.Snowflake, data ModifyChannelData) error {
return c.FastRequest(
"PATCH",
EndpointChannels+channelID.String(),
httputil.WithJSONBody(data),
)
}
func (c *Client) DeleteChannel(channelID discord.Snowflake) error {
return c.FastRequest("DELETE", EndpointChannels+channelID.String())
}
func (c *Client) EditChannelPermission(
channelID discord.Snowflake, overwrite discord.Overwrite) error {
url := EndpointChannels + channelID.String() + "/permissions/" + overwrite.ID.String()
overwrite.ID = 0
return c.FastRequest("PUT", url, httputil.WithJSONBody(overwrite))
}
func (c *Client) DeleteChannelPermission(channelID, overwriteID discord.Snowflake) error {
return c.FastRequest("DELETE",
EndpointChannels+channelID.String()+"/permissions/"+overwriteID.String())
}
// Typing posts a typing indicator to the channel. Undocumented, but the client
// usually clears the typing indicator after 8-10 seconds (or after a message).
func (c *Client) Typing(channelID discord.Snowflake) error {
return c.FastRequest("POST", EndpointChannels+channelID.String()+"/typing")
}
func (c *Client) PinnedMessages(channelID discord.Snowflake) ([]discord.Message, error) {
var pinned []discord.Message
return pinned, c.RequestJSON(&pinned, "GET", EndpointChannels+channelID.String()+"/pins")
}
// PinMessage pins a message, which requires MANAGE_MESSAGES/
func (c *Client) PinMessage(channelID, messageID discord.Snowflake) error {
return c.FastRequest("PUT", EndpointChannels+channelID.String()+"/pins/"+messageID.String())
}
// UnpinMessage also requires MANAGE_MESSAGES.
func (c *Client) UnpinMessage(channelID, messageID discord.Snowflake) error {
return c.FastRequest("DELETE", EndpointChannels+channelID.String()+"/pins/"+messageID.String())
}
// AddRecipient adds a user to a group direct message. As accessToken is needed,
// clearly this endpoint should only be used for OAuth. AccessToken can be
// obtained with the "gdm.join" scope.
func (c *Client) AddRecipient(
channelID, userID discord.Snowflake, accessToken, nickname string) error {
var params struct {
AccessToken string `json:"access_token"`
Nickname string `json:"nickname"`
}
params.AccessToken = accessToken
params.Nickname = nickname
return c.FastRequest(
"PUT",
EndpointChannels+channelID.String()+"/recipients/"+userID.String(),
httputil.WithJSONBody(params),
)
}
// RemoveRecipient removes a user from a group direct message.
func (c *Client) RemoveRecipient(channelID, userID discord.Snowflake) error {
return c.FastRequest("DELETE",
EndpointChannels+channelID.String()+"/recipients/"+userID.String())
}
// Ack is the read state of a channel. This is undocumented.
type Ack struct {
Token string `json:"token"`
}
// Ack marks the read state of a channel. This is undocumented. The method will
// write to the ack variable passed in. If this method is called asynchronously,
// then ack should be mutex guarded.
func (c *Client) Ack(channelID, messageID discord.Snowflake, ack *Ack) error {
return c.RequestJSON(
ack, "POST",
EndpointChannels+channelID.String()+
"/messages/"+messageID.String()+"/ack",
httputil.WithJSONBody(ack),
)
}