Conditions | 16 |
Total Lines | 85 |
Code Lines | 69 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like bot.*Twitch.Update often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package bot |
||
129 | func (t *Twitch) Update() { |
||
130 | var gameResult TwitchGameResult |
||
131 | var streamResult TwitchStreamResult |
||
132 | var streams = make(map[string]*TwitchStreamData) |
||
133 | var games = make(map[string]*TwitchGameData) |
||
134 | var oauthToken = t.OAuthToken() |
||
135 | timeout := time.Duration(time.Duration(1) * time.Second) |
||
136 | client := &http.Client{ |
||
137 | Timeout: time.Duration(timeout), |
||
138 | } |
||
139 | streamQuery := url.Values{} |
||
140 | gameQuery := url.Values{} |
||
141 | for _, g := range t.Guilds { |
||
142 | for _, s := range g.Streams { |
||
143 | streamQuery.Add("user_login", s.Login) |
||
144 | } |
||
145 | } |
||
146 | // Streams |
||
147 | tsreq, _ := http.NewRequest("GET", fmt.Sprintf("https://api.twitch.tv/helix/streams?%v", streamQuery.Encode()), nil) |
||
148 | tsreq.Header.Add("Client-ID", t.Conf.Twitch.ClientID) |
||
149 | tsreq.Header.Add("Authorization", "Bearer "+oauthToken) |
||
150 | tsresp, tserr := client.Do(tsreq) |
||
151 | if tserr == nil { |
||
152 | jerr := json.NewDecoder(tsresp.Body).Decode(&streamResult) |
||
153 | if jerr != nil { |
||
154 | t.DB.Log("Twitch", "", "Parsing Twitch API stream error") |
||
155 | } |
||
156 | } else { |
||
157 | t.DB.Log("Twitch", "", fmt.Sprintf("Getting Twitch API stream error: %v", tserr.Error())) |
||
158 | return |
||
159 | } |
||
160 | for i, s := range streamResult.Data { |
||
161 | gameQuery.Add("id", s.GameID) |
||
162 | streams[s.UserID] = &streamResult.Data[i] |
||
163 | } |
||
164 | |||
165 | // Games |
||
166 | tgreq, _ := http.NewRequest("GET", fmt.Sprintf("https://api.twitch.tv/helix/games?%v", gameQuery.Encode()), nil) |
||
167 | tgreq.Header.Add("Client-ID", t.Conf.Twitch.ClientID) |
||
168 | tgreq.Header.Add("Authorization", "Bearer "+oauthToken) |
||
169 | tgresp, tgerr := client.Do(tgreq) |
||
170 | if tgerr == nil { |
||
171 | jerr := json.NewDecoder(tgresp.Body).Decode(&gameResult) |
||
172 | if jerr != nil { |
||
173 | t.DB.Log("Twitch", "", "Parsing Twitch API game error") |
||
174 | return |
||
175 | } |
||
176 | } else { |
||
177 | t.DB.Log("Twitch", "", fmt.Sprintf("Getting Twitch API game error: %v", tgerr.Error())) |
||
178 | return |
||
179 | } |
||
180 | for i, g := range gameResult.Data { |
||
181 | games[g.ID] = &gameResult.Data[i] |
||
182 | } |
||
183 | |||
184 | for _, g := range t.Guilds { |
||
185 | for _, s := range g.Streams { |
||
186 | if stream, ok := streams[s.UserID]; ok { |
||
187 | if !s.IsOnline { |
||
188 | gameName := "Unknown" |
||
189 | if _, ok := games[stream.GameID]; ok { |
||
190 | gameName = games[stream.GameID].Name |
||
191 | } |
||
192 | t.Guilds[s.Guild].Streams[s.Login].IsOnline = true |
||
193 | t.DB.UpdateStream(s) |
||
194 | imgURL := strings.Replace(stream.ThumbnailURL, "{width}", "320", -1) |
||
195 | imgURL = strings.Replace(imgURL, "{height}", "180", -1) |
||
196 | emb := NewEmbed(stream.Title). |
||
197 | URL(fmt.Sprintf("http://www.twitch.tv/%v", s.Login)). |
||
198 | Author(s.Name, "", s.ProfileImageURL). |
||
199 | Field("Viewers", fmt.Sprintf("%v", stream.Viewers), true). |
||
200 | Field("Game", gameName, true). |
||
201 | AttachImgURL(imgURL). |
||
202 | Color(t.Conf.General.EmbedColor) |
||
203 | if s.CustomMessage != "" { |
||
204 | emb.Content = s.CustomMessage |
||
205 | } else { |
||
206 | emb.Content = fmt.Sprintf(t.Conf.GetLocaleLang("twitch_online", stream.Language), s.Name, s.Login) |
||
207 | } |
||
208 | _, _ = t.Discord.ChannelMessageSendComplex(s.Channel, emb.MessageSend) |
||
209 | } |
||
210 | } else { |
||
211 | if s.IsOnline == true { |
||
212 | t.Guilds[s.Guild].Streams[s.Login].IsOnline = false |
||
213 | t.DB.UpdateStream(s) |
||
214 | } |
||
293 |