| Conditions | 14 |
| Total Lines | 36 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Complex classes like cmd.TwitchCommand 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 |
||
| 9 | func TwitchCommand(ctx bot.Context) { |
||
| 10 | ctx.MetricsCommand("twitch") |
||
| 11 | if ctx.GetRoles().ExistsName("bot.admin") || ctx.IsAdmin() { |
||
| 12 | if len(ctx.Args) == 0 { |
||
| 13 | return |
||
| 14 | } |
||
| 15 | switch ctx.Args[0] { |
||
| 16 | case "add": |
||
| 17 | if len(ctx.Args) > 1 { |
||
| 18 | username, err := ctx.Twitch.AddStreamer(ctx.Guild.ID, ctx.Message.ChannelID, ctx.Args[1]) |
||
| 19 | if err != nil { |
||
| 20 | ctx.ReplyEmbed("Twitch", ctx.Loc("twitch_add_error")) |
||
| 21 | } else { |
||
| 22 | ctx.ReplyEmbed("Twitch", fmt.Sprintf(ctx.Loc("twitch_added"), username)) |
||
|
|
|||
| 23 | } |
||
| 24 | } |
||
| 25 | case "remove": |
||
| 26 | if len(ctx.Args) > 1 { |
||
| 27 | err := ctx.Twitch.RemoveStreamer(ctx.Args[1], ctx.Guild.ID) |
||
| 28 | if err != nil { |
||
| 29 | ctx.ReplyEmbed("Twitch", ctx.Loc("twitch_remove_error")) |
||
| 30 | } else { |
||
| 31 | ctx.ReplyEmbed("Twitch", ctx.Loc("twitch_removed")) |
||
| 32 | } |
||
| 33 | } |
||
| 34 | case "count": |
||
| 35 | if ctx.IsAdmin() { |
||
| 36 | count := 0 |
||
| 37 | for _,g := range ctx.Twitch.Guilds { |
||
| 38 | count += len(g.Streams) |
||
| 39 | } |
||
| 40 | ctx.ReplyEmbed("Twitch", fmt.Sprintf("Streamers: %v", count)) |
||
| 41 | } |
||
| 42 | } |
||
| 43 | } else { |
||
| 44 | ctx.ReplyEmbed("Twitch", ctx.Loc("admin_require")) |
||
| 45 | } |
||
| 46 | } |