Browse Source

Guild Template (#1091)

* feat: guild templates

* task(endpoints): go fmt endpoints

* task(endpoints): go fmt

* docs(restapi): resolve suggestions from code review

Co-authored-by: Fedor Lapshin <fe.lap.prog@gmail.com>

* fix(restapi): add missing image field

Co-authored-by: Fedor Lapshin <fe.lap.prog@gmail.com>
Riley 3 years ago
parent
commit
8ac9c13dda
3 changed files with 144 additions and 0 deletions
  1. 3 0
      endpoints.go
  2. 105 0
      restapi.go
  3. 36 0
      structs.go

+ 3 - 0
endpoints.go

@@ -87,6 +87,9 @@ var (
 	EndpointGuildBanner        = func(gID, hash string) string { return EndpointCDNBanners + gID + "/" + hash + ".png" }
 	EndpointGuildStickers      = func(gID string) string { return EndpointGuilds + gID + "/stickers" }
 	EndpointGuildSticker       = func(gID, sID string) string { return EndpointGuilds + gID + "/stickers/" + sID }
+	EndpointGuildTemplate      = func(tID string) string { return EndpointGuilds + "/templates/" + tID }
+	EndpointGuildTemplates     = func(gID string) string { return EndpointGuilds + gID + "/templates" }
+	EndpointGuildTemplateSync  = func(gID, tID string) string { return EndpointGuilds + gID + "/templates/" + tID }
 
 	EndpointChannel                             = func(cID string) string { return EndpointChannels + cID }
 	EndpointChannelThreads                      = func(cID string) string { return EndpointChannel(cID) + "/threads" }

+ 105 - 0
restapi.go

@@ -1268,6 +1268,111 @@ func (s *Session) GuildEmojiDelete(guildID, emojiID string) (err error) {
 	return
 }
 
+// GuildTemplate returns a GuildTemplate for the given code
+// templateCode: The Code of a GuildTemplate
+func (s *Session) GuildTemplate(templateCode string) (st *GuildTemplate, err error) {
+
+	body, err := s.RequestWithBucketID("GET", EndpointGuildTemplate(templateCode), nil, EndpointGuildTemplate(templateCode))
+	if err != nil {
+		return
+	}
+
+	err = unmarshal(body, &st)
+	return
+}
+
+// GuildCreateWithTemplate creates a guild based on a GuildTemplate
+// templateCode: The Code of a GuildTemplate
+// name: The name of the guild (2-100) characters
+// icon: base64 encoded 128x128 image for the guild icon
+func (s *Session) GuildCreateWithTemplate(templateCode, name, icon string) (st *Guild, err error) {
+
+	data := struct {
+		Name string `json:"name"`
+		Icon string `json:"icon"`
+	}{name, icon}
+
+	body, err := s.RequestWithBucketID("POST", EndpointGuildTemplate(templateCode), data, EndpointGuildTemplate(templateCode))
+	if err != nil {
+		return
+	}
+
+	err = unmarshal(body, &st)
+	return
+}
+
+// GuildTemplates returns all of GuildTemplates
+// guildID: The ID of the guild
+func (s *Session) GuildTemplates(guildID string) (st []*GuildTemplate, err error) {
+
+	body, err := s.RequestWithBucketID("GET", EndpointGuildTemplates(guildID), nil, EndpointGuildTemplates(guildID))
+	if err != nil {
+		return
+	}
+
+	err = unmarshal(body, &st)
+	return
+}
+
+// GuildTemplateCreate creates a template for the guild
+// guildID: The ID of the guild
+// name: The name of the template (1-100 characters)
+// description: The description for the template (0-120 characters)
+func (s *Session) GuildTemplateCreate(guildID, name, description string) (st *GuildTemplate) {
+
+	data := struct {
+		Name        string `json:"name"`
+		Description string `json:"description"`
+	}{name, description}
+
+	body, err := s.RequestWithBucketID("POST", EndpointGuildTemplates(guildID), data, EndpointGuildTemplates(guildID))
+	if err != nil {
+		return
+	}
+
+	err = unmarshal(body, &st)
+	return
+}
+
+// GuildTemplateSync syncs the template to the guild's current state
+// guildID: The ID of the guild
+// templateCode: The code of the template
+func (s *Session) GuildTemplateSync(guildID, templateCode string) (err error) {
+
+	_, err = s.RequestWithBucketID("PUT", EndpointGuildTemplateSync(guildID, templateCode), nil, EndpointGuildTemplateSync(guildID, ""))
+	return
+}
+
+// GuildTemplateEdit modifies the template's metadata
+// guildID: The ID of the guild
+// templateCode: The code of the template
+// name: The name of the template (1-100 characters)
+// description: The description for the template (0-120 characters)
+func (s *Session) GuildTemplateEdit(guildID, templateCode, name, description string) (st *GuildTemplate, err error) {
+
+	data := struct {
+		Name        string `json:"name,omitempty"`
+		Description string `json:"description,omitempty"`
+	}{name, description}
+
+	body, err := s.RequestWithBucketID("PATCH", EndpointGuildTemplateSync(guildID, templateCode), data, EndpointGuildTemplateSync(guildID, ""))
+	if err != nil {
+		return
+	}
+
+	err = unmarshal(body, &st)
+	return
+}
+
+// GuildTemplateDelete deletes the template
+// guildID: The ID of the guild
+// templateCode: The code of the template
+func (s *Session) GuildTemplateDelete(guildID, templateCode string) (err error) {
+
+	_, err = s.RequestWithBucketID("DELETE", EndpointGuildTemplateSync(guildID, templateCode), nil, EndpointGuildTemplateSync(guildID, ""))
+	return
+}
+
 // ------------------------------------------------------------------------------------------------
 // Functions specific to Discord Channels
 // ------------------------------------------------------------------------------------------------

+ 36 - 0
structs.go

@@ -743,6 +743,42 @@ type GuildPreview struct {
 	Description string `json:"description"`
 }
 
+// A GuildTemplate represents
+type GuildTemplate struct {
+	// The unique code for the guild template
+	Code string `json:"code"`
+
+	// The name of the template
+	Name string `json:"name"`
+
+	// The description for the template
+	Description string `json:"description"`
+
+	// The number of times this template has been used
+	UsageCount string `json:"usage_count"`
+
+	// The ID of the user who created the template
+	CreatorID string `json:"creator_id"`
+
+	// The user who created the template
+	Creator *User `json:"creator"`
+
+	// The timestamp of when the template was created
+	CreatedAt time.Time `json:"created_at"`
+
+	// The timestamp of when the template was last synced
+	UpdatedAt time.Time `json:"updated_at"`
+
+	// The ID of the guild the template was based on
+	SourceGuildID string `json:"source_guild_id"`
+
+	// The guild 'snapshot' this template contains
+	SerializedSourceGuild *Guild `json:"serialized_source_guild"`
+
+	// Whether the template has unsynced changes
+	IsDirty bool `json:"is_dirty"`
+}
+
 // MessageNotifications is the notification level for a guild
 // https://discord.com/developers/docs/resources/guild#guild-object-default-message-notification-level
 type MessageNotifications int