浏览代码

Merge pull request #132 from nstafie/develop

BREAKING -- Add support for setting guild region and verification level
Bruce 9 年之前
父节点
当前提交
226df093f6
共有 2 个文件被更改,包括 61 次插入23 次删除
  1. 35 5
      restapi.go
  2. 26 18
      structs.go

+ 35 - 5
restapi.go

@@ -371,14 +371,44 @@ func (s *Session) GuildCreate(name string) (st *Guild, err error) {
 
 // GuildEdit edits a new Guild
 // guildID   : The ID of a Guild
-// name      : A name for the Guild (2-100 characters)
-func (s *Session) GuildEdit(guildID, name string) (st *Guild, err error) {
+// g 		 : A GuildParams struct with the values Name, Region and VerificationLevel defined.
+func (s *Session) GuildEdit(guildID string, g GuildParams) (st *Guild, err error) {
+
+	// Bounds checking for VerificationLevel, interval: [0, 3]
+	if g.VerificationLevel != nil {
+		val := *g.VerificationLevel
+		if val < 0 || val > 3 {
+			err = errors.New("VerificationLevel out of bounds, should be between 0 and 3")
+			return
+		}
+	}
+
+	//Bounds checking for regions
+	if g.Region != "" {
+		isValid := false
+		regions, _ := s.VoiceRegions()
+		for _, r := range regions {
+			if g.Region == r.ID {
+				isValid = true
+			}
+		}
+		if !isValid {
+			var valid []string
+			for _, r := range regions {
+				valid = append(valid, r.ID)
+			}
+			err = errors.New(fmt.Sprintf("Region not a valid region (%q)", valid))
+			return
+		}
+	}
 
 	data := struct {
-		Name string `json:"name"`
-	}{name}
+		Name              string `json:"name,omitempty"`
+		Region            string `json:"region,omitempty"`
+		VerificationLevel *int   `json:"verification_level,omitempty"`
+	}{g.Name, g.Region, g.VerificationLevel}
 
-	body, err := s.Request("POST", GUILD(guildID), data)
+	body, err := s.Request("PATCH", GUILD(guildID), data)
 	if err != nil {
 		return
 	}

+ 26 - 18
structs.go

@@ -148,24 +148,32 @@ type Emoji struct {
 // A Guild holds all data related to a specific Discord Guild.  Guilds are also
 // sometimes referred to as Servers in the Discord client.
 type Guild struct {
-	ID             string        `json:"id"`
-	Name           string        `json:"name"`
-	Icon           string        `json:"icon"`
-	Region         string        `json:"region"`
-	AfkChannelID   string        `json:"afk_channel_id"`
-	EmbedChannelID string        `json:"embed_channel_id"`
-	OwnerID        string        `json:"owner_id"`
-	JoinedAt       string        `json:"joined_at"` // make this a timestamp
-	Splash         string        `json:"splash"`
-	AfkTimeout     int           `json:"afk_timeout"`
-	EmbedEnabled   bool          `json:"embed_enabled"`
-	Large          bool          `json:"large"` // ??
-	Roles          []*Role       `json:"roles"`
-	Emojis         []*Emoji      `json:"emojis"`
-	Members        []*Member     `json:"members"`
-	Presences      []*Presence   `json:"presences"`
-	Channels       []*Channel    `json:"channels"`
-	VoiceStates    []*VoiceState `json:"voice_states"`
+	ID                string        `json:"id"`
+	Name              string        `json:"name"`
+	Icon              string        `json:"icon"`
+	Region            string        `json:"region"`
+	AfkChannelID      string        `json:"afk_channel_id"`
+	EmbedChannelID    string        `json:"embed_channel_id"`
+	OwnerID           string        `json:"owner_id"`
+	JoinedAt          string        `json:"joined_at"` // make this a timestamp
+	Splash            string        `json:"splash"`
+	AfkTimeout        int           `json:"afk_timeout"`
+	VerificationLevel int           `json:"verification_level"`
+	EmbedEnabled      bool          `json:"embed_enabled"`
+	Large             bool          `json:"large"` // ??
+	Roles             []*Role       `json:"roles"`
+	Emojis            []*Emoji      `json:"emojis"`
+	Members           []*Member     `json:"members"`
+	Presences         []*Presence   `json:"presences"`
+	Channels          []*Channel    `json:"channels"`
+	VoiceStates       []*VoiceState `json:"voice_states"`
+}
+
+// A GuildParams stores all the data needed to update discord guild settings
+type GuildParams struct {
+	Name              string `json:"name"`
+	Region            string `json:"region"`
+	VerificationLevel *int   `json:"verification_level"`
 }
 
 // A Role stores information about Discord guild member roles.