1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-18 12:52:45 +00:00
arikawa/api/guild.go

286 lines
7.6 KiB
Go
Raw Normal View History

2020-01-02 05:39:52 +00:00
package api
2020-01-04 04:19:24 +00:00
import (
2020-01-06 03:48:39 +00:00
"io"
2020-01-04 04:19:24 +00:00
"github.com/diamondburned/arikawa/discord" // for clarity
d "github.com/diamondburned/arikawa/discord"
"github.com/diamondburned/arikawa/utils/httputil"
2020-01-04 04:19:24 +00:00
)
2020-01-02 05:39:52 +00:00
const EndpointGuilds = Endpoint + "guilds/"
2020-01-04 04:19:24 +00:00
// https://discordapp.com/developers/docs/resources/guild#create-guild-json-params
type CreateGuildData struct {
2020-01-06 03:48:39 +00:00
Name string `json:"name"`
Icon Image `json:"image,omitempty"`
2020-01-04 04:19:24 +00:00
// package dc is just package discord
Verification d.Verification `json:"verification_level"`
Notification d.Notification `json:"default_message_notifications"`
ExplicitFilter d.ExplicitFilter `json:"explicit_content_filter"`
// [0] (First entry) is ALWAYS @everyone.
2020-01-06 03:48:39 +00:00
Roles []discord.Role `json:"roles,omitempty"`
// Voice only
VoiceRegion string `json:"region,omitempty"`
2020-01-04 04:19:24 +00:00
// Partial, id field is ignored. Usually only Name and Type are changed.
2020-01-06 03:48:39 +00:00
Channels []discord.Channel `json:"channels,omitempty"`
2020-01-04 04:19:24 +00:00
}
func (c *Client) CreateGuild(data CreateGuildData) (*discord.Guild, error) {
var g *discord.Guild
return g, c.RequestJSON(&g, "POST", Endpoint+"guilds",
httputil.WithJSONBody(c, data))
}
func (c *Client) Guild(guildID discord.Snowflake) (*discord.Guild, error) {
var g *discord.Guild
return g, c.RequestJSON(&g, "GET", EndpointGuilds+guildID.String())
}
2020-01-16 04:27:57 +00:00
// Guilds returns all guilds, automatically paginating. Be careful, as this
// method may abuse the API by requesting thousands or millions of guilds. For
// lower-level access, usee GuildsRange. Guilds returned have some fields
// filled only (ID, Name, Icon, Owner, Permissions).
func (c *Client) Guilds(max uint) ([]discord.Guild, error) {
var guilds []discord.Guild
2020-01-06 05:22:26 +00:00
var after discord.Snowflake = 0
2020-01-16 04:27:57 +00:00
const hardLimit int = 100
for fetch := uint(hardLimit); max > 0; fetch = uint(hardLimit) {
2020-01-18 19:34:08 +00:00
if max > 0 {
if fetch > max {
fetch = max
}
max -= fetch
}
2020-01-16 04:27:57 +00:00
g, err := c.GuildsAfter(after, fetch)
2020-01-06 05:22:26 +00:00
if err != nil {
2020-01-16 04:27:57 +00:00
return guilds, err
2020-01-06 05:22:26 +00:00
}
2020-01-16 04:27:57 +00:00
guilds = append(guilds, g...)
2020-01-06 05:22:26 +00:00
2020-01-16 04:27:57 +00:00
if len(g) < hardLimit {
2020-01-06 05:22:26 +00:00
break
}
2020-01-16 04:27:57 +00:00
after = g[hardLimit-1].ID
2020-01-06 05:22:26 +00:00
}
2020-01-16 04:27:57 +00:00
return guilds, nil
2020-01-06 05:22:26 +00:00
}
2020-01-16 04:27:57 +00:00
// GuildsBefore fetches guilds. Check GuildsRange.
func (c *Client) GuildsBefore(
before discord.Snowflake, limit uint) ([]discord.Guild, error) {
2020-01-06 03:48:39 +00:00
2020-01-16 04:27:57 +00:00
return c.GuildsRange(before, 0, limit)
2020-01-05 07:50:22 +00:00
}
2020-01-16 04:27:57 +00:00
// GuildsAfter fetches guilds. Check GuildsRange.
func (c *Client) GuildsAfter(
after discord.Snowflake, limit uint) ([]discord.Guild, error) {
2020-01-05 07:50:22 +00:00
2020-01-16 04:27:57 +00:00
return c.GuildsRange(0, after, limit)
2020-01-05 07:50:22 +00:00
}
2020-01-16 04:27:57 +00:00
// GuildsRange fetches guilds. The limit is 1-100.
func (c *Client) GuildsRange(
before, after discord.Snowflake, limit uint) ([]discord.Guild, error) {
2020-01-05 07:50:22 +00:00
2020-01-16 04:27:57 +00:00
if limit == 0 {
limit = 100
2020-01-05 07:50:22 +00:00
}
2020-01-16 04:27:57 +00:00
if limit > 100 {
limit = 100
2020-01-05 07:50:22 +00:00
}
var param struct {
2020-01-16 04:27:57 +00:00
Before discord.Snowflake `schema:"before"`
After discord.Snowflake `schema:"after"`
Limit uint `schema:"limit"`
2020-01-05 07:50:22 +00:00
}
2020-01-16 04:27:57 +00:00
param.Before = before
param.After = after
param.Limit = limit
2020-01-05 07:50:22 +00:00
2020-01-16 04:27:57 +00:00
var gs []discord.Guild
return gs, c.RequestJSON(
&gs, "GET",
EndpointMe+"/guilds",
2020-01-06 03:48:39 +00:00
httputil.WithSchema(c, param),
2020-01-05 07:50:22 +00:00
)
}
2020-01-16 04:27:57 +00:00
func (c *Client) LeaveGuild(guildID discord.Snowflake) error {
return c.FastRequest("DELETE", EndpointMe+"/guilds/"+guildID.String())
2020-01-05 07:50:22 +00:00
}
2020-01-16 04:27:57 +00:00
// https://discordapp.com/developers/docs/resources/guild#modify-guild-json-params
type ModifyGuildData struct {
Name string `json:"name,omitempty"`
Region string `json:"region,omitempty"`
Icon Image `json:"image,omitempty"`
2020-01-05 07:50:22 +00:00
2020-01-16 04:27:57 +00:00
// package d is just package discord
Verification *d.Verification `json:"verification_level,omitempty"`
Notification *d.Notification `json:"default_message_notifications,omitempty"`
ExplicitFilter *d.ExplicitFilter `json:"explicit_content_filter,omitempty"`
2020-01-05 07:50:22 +00:00
2020-01-16 04:27:57 +00:00
AFKChannelID *d.Snowflake `json:"afk_channel_id,string,omitempty"`
AFKTimeout *d.Seconds `json:"afk_timeout,omitempty"`
2020-01-05 07:50:22 +00:00
2020-01-16 04:27:57 +00:00
OwnerID d.Snowflake `json:"owner_id,string,omitempty"`
2020-01-05 07:50:22 +00:00
2020-01-16 04:27:57 +00:00
Splash Image `json:"splash,omitempty"`
Banner Image `json:"banner,omitempty"`
2020-01-05 07:50:22 +00:00
2020-01-16 04:27:57 +00:00
SystemChannelID d.Snowflake `json:"system_channel_id,string,omitempty"`
2020-01-05 07:50:22 +00:00
}
2020-01-16 04:27:57 +00:00
func (c *Client) ModifyGuild(
guildID discord.Snowflake, data ModifyGuildData) (*discord.Guild, error) {
2020-01-05 07:50:22 +00:00
2020-01-16 04:27:57 +00:00
var g *discord.Guild
return g, c.RequestJSON(
&g, "PATCH",
EndpointGuilds+guildID.String(),
2020-01-05 07:50:22 +00:00
httputil.WithJSONBody(c, data),
)
}
2020-01-16 04:27:57 +00:00
func (c *Client) DeleteGuild(guildID discord.Snowflake) error {
return c.FastRequest("DELETE", EndpointGuilds+guildID.String())
2020-01-06 03:48:39 +00:00
}
// GuildVoiceRegions is the same as /voice, but returns VIP ones as well if
// available.
func (c *Client) VoiceRegionsGuild(
guildID discord.Snowflake) ([]discord.VoiceRegion, error) {
var vrs []discord.VoiceRegion
return vrs, c.RequestJSON(&vrs, "GET",
EndpointGuilds+guildID.String()+"/regions")
}
// Integrations requires MANAGE_GUILD.
func (c *Client) Integrations(
guildID discord.Snowflake) ([]discord.Integration, error) {
var ints []discord.Integration
return ints, c.RequestJSON(&ints, "GET",
EndpointGuilds+guildID.String()+"/integrations")
}
// AttachIntegration requires MANAGE_GUILD.
func (c *Client) AttachIntegration(
guildID, integrationID discord.Snowflake,
2020-01-15 07:34:18 +00:00
integrationType discord.Service) error {
2020-01-06 03:48:39 +00:00
var param struct {
2020-01-15 07:34:18 +00:00
Type discord.Service `json:"type"`
ID discord.Snowflake `json:"id"`
2020-01-06 03:48:39 +00:00
}
return c.FastRequest(
"POST",
EndpointGuilds+guildID.String()+"/integrations",
httputil.WithJSONBody(c, param),
)
}
// ModifyIntegration requires MANAGE_GUILD.
func (c *Client) ModifyIntegration(
guildID, integrationID discord.Snowflake,
2020-01-06 03:48:39 +00:00
expireBehavior, expireGracePeriod int, emoticons bool) error {
var param struct {
ExpireBehavior int `json:"expire_behavior"`
ExpireGracePeriod int `json:"expire_grace_period"`
EnableEmoticons bool `json:"enable_emoticons"`
}
param.ExpireBehavior = expireBehavior
param.ExpireGracePeriod = expireGracePeriod
param.EnableEmoticons = emoticons
2020-01-16 04:27:57 +00:00
return c.FastRequest(
"PATCH",
EndpointGuilds+guildID.String()+
"/integrations/"+integrationID.String(),
2020-01-06 03:48:39 +00:00
httputil.WithSchema(c, param),
)
}
func (c *Client) SyncIntegration(
guildID, integrationID discord.Snowflake) error {
return c.FastRequest("POST", EndpointGuilds+guildID.String()+
"/integrations/"+integrationID.String()+"/sync")
}
func (c *Client) GuildEmbed(
guildID discord.Snowflake) (*discord.GuildEmbed, error) {
var ge *discord.GuildEmbed
return ge, c.RequestJSON(&ge, "GET",
EndpointGuilds+guildID.String()+"/embed")
}
// ModifyGuildEmbed should be used with care: if you still want the embed
// enabled, you need to set the Enabled boolean, even if it's already enabled.
// If you don't, JSON will default it to false.
func (c *Client) ModifyGuildEmbed(
guildID discord.Snowflake,
2020-01-06 03:48:39 +00:00
data discord.GuildEmbed) (*discord.GuildEmbed, error) {
return &data, c.RequestJSON(&data, "PATCH",
EndpointGuilds+guildID.String()+"/embed")
}
// GuildVanityURL returns *Invite, but only Code and Uses are filled. Requires
// MANAGE_GUILD.
func (c *Client) GuildVanityURL(
guildID discord.Snowflake) (*discord.Invite, error) {
var inv *discord.Invite
return inv, c.RequestJSON(&inv, "GET",
EndpointGuilds+guildID.String()+"/vanity-url")
}
type GuildImageType string
const (
GuildShield GuildImageType = "shield"
GuildBanner1 GuildImageType = "banner1"
GuildBanner2 GuildImageType = "banner2"
GuildBanner3 GuildImageType = "banner3"
GuildBanner4 GuildImageType = "banner4"
)
func (c *Client) GuildImageURL(
guildID discord.Snowflake, img GuildImageType) string {
return EndpointGuilds + guildID.String() +
"/widget.png?style=" + string(img)
}
func (c *Client) GuildImage(
guildID discord.Snowflake, img GuildImageType) (io.ReadCloser, error) {
r, err := c.Request("GET", c.GuildImageURL(guildID, img))
if err != nil {
return nil, err
}
return r.Body, nil
}