| Conditions | 19 |
| Total Lines | 52 |
| Code Lines | 41 |
| 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 cmd.PlayerCommand 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 cmd |
||
| 10 | func PlayerCommand(ctx bot.Context) { |
||
| 11 | sess := ctx.Sessions.GetByGuild(ctx.Guild.ID) |
||
| 12 | if len(ctx.Args) == 0 { |
||
| 13 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_no_args")) |
||
| 14 | return |
||
| 15 | } |
||
| 16 | switch ctx.Args[0] { |
||
| 17 | case "play": |
||
| 18 | ctx.MetricsCommand("player", "play") |
||
| 19 | if sess == nil { |
||
| 20 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_not_in_voice")) |
||
| 21 | return |
||
| 22 | } |
||
| 23 | if len(ctx.Args) > 1 { |
||
| 24 | go sess.Player.Start(sess, ctx.Args[1], func(msg string) { |
||
| 25 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), msg) |
||
| 26 | }, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume) |
||
| 27 | |||
| 28 | } |
||
| 29 | case "list": |
||
| 30 | stations := ctx.DB.GetRadioStations() |
||
| 31 | if len(stations) > 0 { |
||
| 32 | var response string |
||
| 33 | if len(stations) > 20 { |
||
| 34 | for _, s := range stations[:20] { |
||
| 35 | response += fmt.Sprintf("[%v] - %v", s.Key, s.Name) |
||
| 36 | } |
||
| 37 | } else { |
||
| 38 | for _,s := range stations { |
||
| 39 | response += fmt.Sprintf("[%v] - %v", s.Key, s.Name) |
||
| 40 | } |
||
| 41 | } |
||
| 42 | ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("stations_list") + "\n" + response) |
||
| 43 | } |
||
| 44 | case "station": |
||
| 45 | if sess == nil { |
||
| 46 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_not_in_voice")) |
||
| 47 | return |
||
| 48 | } |
||
| 49 | if len(ctx.Args) > 1 { |
||
| 50 | station, err :=ctx.DB.GetRadioStationByKey(ctx.Args[1]) |
||
| 51 | if err != nil { |
||
| 52 | ctx.ReplyEmbed(ctx.Loc("player"), ctx.Loc("stations_not_found")) |
||
| 53 | return |
||
| 54 | } |
||
| 55 | go sess.Player.Start(sess, station.URL, func(msg string) { |
||
| 56 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), msg) |
||
| 57 | }, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume) |
||
| 58 | } |
||
| 59 | case "stop": |
||
| 60 | ctx.MetricsCommand("player", "stop") |
||
| 61 | sess.Stop() |
||
| 62 | } |
||
| 64 |