1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-16 03:44:26 +00:00
arikawa/api/guild.go

292 lines
8.8 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
"github.com/diamondburned/arikawa/utils/httputil"
"github.com/diamondburned/arikawa/utils/json"
2020-01-04 04:19:24 +00:00
)
var 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 discord.Verification `json:"verification_level"`
Notification discord.Notification `json:"default_message_notifications"`
ExplicitFilter discord.ExplicitFilter `json:"explicit_content_filter"`
2020-01-04 04:19:24 +00:00
// [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
2020-04-19 21:53:53 +00:00
return g, c.RequestJSON(&g, "POST", Endpoint+"guilds", httputil.WithJSONBody(c, data))
2020-01-04 04:19:24 +00:00
}
2020-05-08 00:09:45 +00:00
func (c *Client) Guild(id discord.Snowflake) (*discord.Guild, error) {
2020-01-04 04:19:24 +00:00
var g *discord.Guild
2020-05-08 00:09:45 +00:00
return g, c.RequestJSON(&g, "GET", EndpointGuilds+id.String())
2020-01-04 04:19:24 +00:00
}
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.
2020-04-19 21:53:53 +00:00
func (c *Client) GuildsBefore(before discord.Snowflake, limit uint) ([]discord.Guild, error) {
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.
2020-04-19 21:53:53 +00:00
func (c *Client) GuildsAfter(after discord.Snowflake, limit uint) ([]discord.Guild, error) {
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.
2020-04-19 21:53:53 +00:00
func (c *Client) GuildsRange(before, after discord.Snowflake, limit uint) ([]discord.Guild, error) {
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-05-08 00:09:45 +00:00
func (c *Client) LeaveGuild(id discord.Snowflake) error {
return c.FastRequest("DELETE", EndpointMe+"/guilds/"+id.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 json.OptionString `json:"region,omitempty"`
2020-01-05 07:50:22 +00:00
2020-01-16 04:27:57 +00:00
// package d is just package discord
Verification *discord.Verification `json:"verification_level,omitempty"`
Notification *discord.Notification `json:"default_message_notifications,omitempty"`
ExplicitFilter *discord.ExplicitFilter `json:"explicit_content_filter,omitempty"`
2020-01-05 07:50:22 +00:00
AFKChannelID *discord.Snowflake `json:"afk_channel_id,string,omitempty"`
AFKTimeout *discord.Seconds `json:"afk_timeout,omitempty"`
2020-01-05 07:50:22 +00:00
OwnerID discord.Snowflake `json:"owner_id,omitempty"`
2020-01-05 07:50:22 +00:00
Icon *Image `json:"icon,omitempty"`
Splash *Image `json:"splash,omitempty"`
Banner *Image `json:"banner,omitempty"`
2020-01-05 07:50:22 +00:00
SystemChannelID *discord.Snowflake `json:"system_channel_id,omitempty"`
RulesChannelID *discord.Snowflake `json:"rules_channel_id,omitempty"`
PublicUpdatesChannelID *discord.Snowflake `json:"public_updates_channel_id,omitempty"`
PreferredLocale json.OptionString `json:"preferred_locale,omitempty"`
2020-01-05 07:50:22 +00:00
}
2020-05-08 00:09:45 +00:00
func (c *Client) ModifyGuild(id discord.Snowflake, data ModifyGuildData) (*discord.Guild, error) {
2020-01-16 04:27:57 +00:00
var g *discord.Guild
return g, c.RequestJSON(
&g, "PATCH",
2020-05-08 00:09:45 +00:00
EndpointGuilds+id.String(),
2020-01-05 07:50:22 +00:00
httputil.WithJSONBody(c, data),
)
}
2020-05-08 00:09:45 +00:00
func (c *Client) DeleteGuild(id discord.Snowflake) error {
return c.FastRequest("DELETE", EndpointGuilds+id.String())
2020-01-06 03:48:39 +00:00
}
// GuildVoiceRegions is the same as /voice, but returns VIP ones as well if
// available.
2020-04-19 21:53:53 +00:00
func (c *Client) VoiceRegionsGuild(guildID discord.Snowflake) ([]discord.VoiceRegion, error) {
2020-01-06 03:48:39 +00:00
var vrs []discord.VoiceRegion
2020-04-19 21:53:53 +00:00
return vrs, c.RequestJSON(&vrs, "GET", EndpointGuilds+guildID.String()+"/regions")
2020-01-06 03:48:39 +00:00
}
2020-05-08 00:09:45 +00:00
// AuditLogData contains query parameters used for AuditLog. All fields are
// optional.
type AuditLogData struct {
// Filter the log for actions made by a user
UserID discord.Snowflake `schema:"user_id,omitempty"`
2020-05-08 00:09:45 +00:00
// The type of audit log event
ActionType discord.AuditLogEvent `schema:"action_type,omitempty"`
2020-05-08 00:09:45 +00:00
// Filter the log before a certain entry ID
Before discord.Snowflake `schema:"before,omitempty"`
2020-05-08 00:09:45 +00:00
// How many entries are returned (default 50, minimum 1, maximum 100)
Limit uint `schema:"limit"`
2020-05-08 00:09:45 +00:00
}
// AuditLog returns an audit log object for the guild. Requires the
// VIEW_AUDIT_LOG permission.
func (c *Client) AuditLog(guildID discord.Snowflake, data AuditLogData) (*discord.AuditLog, error) {
switch {
case data.Limit == 0:
data.Limit = 50
case data.Limit > 100:
data.Limit = 100
}
var audit *discord.AuditLog
return audit, c.RequestJSON(
&audit, "GET",
EndpointGuilds+guildID.String()+"/audit-logs",
httputil.WithSchema(c, data),
2020-05-08 00:09:45 +00:00
)
}
2020-01-06 03:48:39 +00:00
// Integrations requires MANAGE_GUILD.
2020-04-19 21:53:53 +00:00
func (c *Client) Integrations(guildID discord.Snowflake) ([]discord.Integration, error) {
2020-01-06 03:48:39 +00:00
var ints []discord.Integration
2020-04-19 21:53:53 +00:00
return ints, c.RequestJSON(&ints, "GET", EndpointGuilds+guildID.String()+"/integrations")
2020-01-06 03:48:39 +00:00
}
// 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",
2020-04-19 21:53:53 +00:00
EndpointGuilds+guildID.String()+"/integrations/"+integrationID.String(),
2020-01-06 03:48:39 +00:00
httputil.WithSchema(c, param),
)
}
2020-04-19 21:53:53 +00:00
func (c *Client) SyncIntegration(guildID, integrationID discord.Snowflake) error {
2020-01-06 03:48:39 +00:00
return c.FastRequest("POST", EndpointGuilds+guildID.String()+
"/integrations/"+integrationID.String()+"/sync")
}
2020-04-19 21:53:53 +00:00
func (c *Client) GuildEmbed(guildID discord.Snowflake) (*discord.GuildEmbed, error) {
2020-01-06 03:48:39 +00:00
var ge *discord.GuildEmbed
2020-04-19 21:53:53 +00:00
return ge, c.RequestJSON(&ge, "GET", EndpointGuilds+guildID.String()+"/embed")
2020-01-06 03:48:39 +00:00
}
2020-04-19 21:53:53 +00:00
// ModifyGuildEmbed modifies the guild embed and updates the passed in
// GuildEmbed data.
//
// This method 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, data *discord.GuildEmbed) error {
return c.RequestJSON(&data, "PATCH", EndpointGuilds+guildID.String()+"/embed")
2020-01-06 03:48:39 +00:00
}
// GuildVanityURL returns *Invite, but only Code and Uses are filled. Requires
// MANAGE_GUILD.
2020-04-19 21:53:53 +00:00
func (c *Client) GuildVanityURL(guildID discord.Snowflake) (*discord.Invite, error) {
2020-01-06 03:48:39 +00:00
var inv *discord.Invite
2020-04-19 21:53:53 +00:00
return inv, c.RequestJSON(&inv, "GET", EndpointGuilds+guildID.String()+"/vanity-url")
2020-01-06 03:48:39 +00:00
}
type GuildImageType string
const (
GuildShield GuildImageType = "shield"
GuildBanner1 GuildImageType = "banner1"
GuildBanner2 GuildImageType = "banner2"
GuildBanner3 GuildImageType = "banner3"
GuildBanner4 GuildImageType = "banner4"
)
2020-04-19 21:53:53 +00:00
func (c *Client) GuildImageURL(guildID discord.Snowflake, img GuildImageType) string {
return EndpointGuilds + guildID.String() + "/widget.png?style=" + string(img)
2020-01-06 03:48:39 +00:00
}
2020-04-19 21:53:53 +00:00
func (c *Client) GuildImage(guildID discord.Snowflake, img GuildImageType) (io.ReadCloser, error) {
2020-01-06 03:48:39 +00:00
r, err := c.Request("GET", c.GuildImageURL(guildID, img))
if err != nil {
return nil, err
}
2020-04-19 21:53:53 +00:00
return r.GetBody(), nil
2020-01-06 03:48:39 +00:00
}