| Conditions | 20 |
| Total Lines | 74 |
| Code Lines | 60 |
| 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 |
||
| 101 | func (t *Twitch) Update() { |
||
| 102 | var gameResult TwitchGameResult |
||
| 103 | var streamResult TwitchStreamResult |
||
| 104 | var streams = make(map[string]*TwitchStreamData) |
||
| 105 | var games = make(map[string]*TwitchGameData) |
||
| 106 | timeout := time.Duration(time.Duration(1) * time.Second) |
||
| 107 | client := &http.Client{ |
||
| 108 | Timeout: time.Duration(timeout), |
||
| 109 | } |
||
| 110 | streamQuery := url.Values{} |
||
| 111 | gameQuery := url.Values{} |
||
| 112 | for _,g := range t.Guilds { |
||
| 113 | for _,s := range g.Streams { |
||
| 114 | streamQuery.Add("user_login", s.Login) |
||
| 115 | } |
||
| 116 | } |
||
| 117 | // Streams |
||
| 118 | tsreq, _ := http.NewRequest("GET", fmt.Sprintf("https://api.twitch.tv/helix/streams?%v", streamQuery.Encode()), nil) |
||
| 119 | tsreq.Header.Add("Client-ID", t.Conf.Twitch.ClientID) |
||
| 120 | tsresp, tserr := client.Do(tsreq) |
||
| 121 | if tserr == nil { |
||
| 122 | jerr := json.NewDecoder(tsresp.Body).Decode(&streamResult) |
||
| 123 | if jerr != nil { |
||
| 124 | t.DB.Log("Twitch", "", "Parsing Twitch API stream error") |
||
| 125 | } |
||
| 126 | } else { |
||
| 127 | t.DB.Log("Twitch", "", fmt.Sprintf("Getting Twitch API stream error: %v", tserr.Error())) |
||
| 128 | return |
||
| 129 | } |
||
| 130 | for i, s := range streamResult.Data { |
||
| 131 | gameQuery.Add("id", s.GameID) |
||
| 132 | streams[s.UserID] = &streamResult.Data[i] |
||
| 133 | } |
||
| 134 | |||
| 135 | // Games |
||
| 136 | tgreq, _ := http.NewRequest("GET", fmt.Sprintf("https://api.twitch.tv/helix/games?%v", gameQuery.Encode()), nil) |
||
| 137 | tgreq.Header.Add("Client-ID", t.Conf.Twitch.ClientID) |
||
| 138 | tgresp, tgerr := client.Do(tgreq) |
||
| 139 | if tgerr == nil { |
||
| 140 | jerr := json.NewDecoder(tgresp.Body).Decode(&gameResult) |
||
| 141 | if jerr != nil { |
||
| 142 | t.DB.Log("Twitch", "", "Parsing Twitch API game error") |
||
| 143 | return |
||
| 144 | } |
||
| 145 | } else { |
||
| 146 | t.DB.Log("Twitch", "", fmt.Sprintf("Getting Twitch API game error: %v", tgerr.Error())) |
||
| 147 | return |
||
| 148 | } |
||
| 149 | for i, g := range gameResult.Data { |
||
| 150 | games[g.ID] = &gameResult.Data[i] |
||
| 151 | } |
||
| 152 | |||
| 153 | for _,g := range t.Guilds { |
||
| 154 | for _,s := range g.Streams { |
||
| 155 | if stream,ok := streams[s.Login]; ok { |
||
| 156 | if !s.IsOnline { |
||
| 157 | t.Guilds[s.Guild].Streams[s.Login].IsOnline = true |
||
| 158 | t.DB.UpdateStream(s) |
||
| 159 | imgUrl := strings.Replace(stream.ThumbnailURL, "{width}", "320", -1) |
||
|
|
|||
| 160 | imgUrl = strings.Replace(imgUrl, "{height}", "180", -1) |
||
| 161 | emb := NewEmbed(stream.Title). |
||
| 162 | URL(fmt.Sprintf("http://www.twitch.tv/%v", s.Login)). |
||
| 163 | Author(stream.UserName, "", ""). |
||
| 164 | Field("Viewers", fmt.Sprintf("%v", stream.Viewers), true). |
||
| 165 | Field("Game", games[stream.GameID].Name, true). |
||
| 166 | AttachImgURL(imgUrl). |
||
| 167 | Color(t.Conf.General.EmbedColor) |
||
| 168 | _, _ = t.Discord.ChannelMessageSend(s.Channel, fmt.Sprintf(t.Conf.GetLocaleLang("twitch_online", stream.Language), stream.UserName, s.Login)) |
||
| 169 | _, _ = t.Discord.ChannelMessageSendEmbed(s.Channel, emb.GetEmbed()) |
||
| 170 | } |
||
| 171 | } else { |
||
| 172 | if s.IsOnline == true { |
||
| 173 | t.Guilds[s.Guild].Streams[s.Login].IsOnline = false |
||
| 174 | t.DB.UpdateStream(s) |
||
| 175 | } |
||
| 300 |