| Conditions | 16 |
| Total Lines | 58 |
| Code Lines | 51 |
| 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.botSetConf 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 |
||
| 109 | func botSetConf(ctx *bot.Context) { |
||
| 110 | ctx.MetricsCommand("bot", "setconf") |
||
| 111 | if len(ctx.Args) > 2 { |
||
| 112 | target := strings.Split(ctx.Args[1], ".") |
||
| 113 | switch target[0] { |
||
| 114 | case "general": |
||
| 115 | switch target[1] { |
||
| 116 | case "language": |
||
| 117 | ctx.Guilds.Guilds[ctx.Guild.ID].Language = ctx.Args[2] |
||
| 118 | _ = ctx.DB.Guilds().Update(bson.M{"id": ctx.Guild.ID}, bson.M{"$set": bson.M{"language": ctx.Args[2]}}) |
||
| 119 | ctx.ReplyEmbedPM("Config", fmt.Sprintf("Language set to: %v", ctx.Args[2])) |
||
| 120 | case "timezone": |
||
| 121 | tz, err := strconv.Atoi(ctx.Args[1]) |
||
| 122 | if err != nil { |
||
| 123 | ctx.ReplyEmbedPM("Settings", "Not a number") |
||
| 124 | } |
||
| 125 | ctx.Guilds.Guilds[ctx.Guild.ID].Timezone = tz |
||
| 126 | _ = ctx.DB.Guilds().Update(bson.M{"id": ctx.Guild.ID}, bson.M{"$set": bson.M{"timezone": tz}}) |
||
| 127 | ctx.ReplyEmbedPM("Config", fmt.Sprintf("Timezone set to: %v", ctx.Args[2])) |
||
| 128 | case "nick": |
||
| 129 | _ = ctx.Discord.GuildMemberNickname(ctx.Guild.ID, "@me", ctx.Args[2]) |
||
| 130 | ctx.ReplyEmbedPM("Config", fmt.Sprintf("Nickname changed to %v", ctx.Args[2])) |
||
| 131 | } |
||
| 132 | case "weather": |
||
| 133 | switch target[1] { |
||
| 134 | case "city": |
||
| 135 | ctx.Guilds.Guilds[ctx.Guild.ID].WeatherCity = ctx.Args[2] |
||
| 136 | _ = ctx.DB.Guilds().Update(bson.M{"id": ctx.Guild.ID}, bson.M{"$set": bson.M{"weathercity": ctx.Args[2]}}) |
||
| 137 | ctx.ReplyEmbedPM("Config", fmt.Sprintf("Weather city set to: %v", ctx.Args[2])) |
||
| 138 | } |
||
| 139 | case "news": |
||
| 140 | switch target[1] { |
||
| 141 | case "country": |
||
| 142 | ctx.Guilds.Guilds[ctx.Guild.ID].NewsCounty = ctx.Args[2] |
||
| 143 | _ = ctx.DB.Guilds().Update(bson.M{"id": ctx.Guild.ID}, bson.M{"$set": bson.M{"weathercountry": ctx.Args[2]}}) |
||
| 144 | ctx.ReplyEmbedPM("Config", fmt.Sprintf("News country set to: %v", ctx.Args[2])) |
||
| 145 | } |
||
| 146 | case "embed": |
||
| 147 | switch target[1] { |
||
| 148 | case "color": |
||
| 149 | var color int64 |
||
| 150 | var err error |
||
| 151 | if strings.HasPrefix(ctx.Args[2], "#") { |
||
| 152 | color, err = strconv.ParseInt(ctx.Args[2][1:], 16, 32) |
||
| 153 | if err != nil { |
||
| 154 | ctx.Log("Config", ctx.Guild.ID, fmt.Sprintf("error setting parameter %v to value %v: %v", ctx.Args[1], target[2], err.Error())) |
||
| 155 | return |
||
| 156 | } |
||
| 157 | } else { |
||
| 158 | color, err = strconv.ParseInt(ctx.Args[2], 16, 32) |
||
| 159 | if err != nil { |
||
| 160 | ctx.Log("Config", ctx.Guild.ID, fmt.Sprintf("error setting parameter %v to value %v: %v", ctx.Args[1], ctx.Args[2], err.Error())) |
||
| 161 | return |
||
| 162 | } |
||
| 163 | } |
||
| 164 | ctx.Guilds.Guilds[ctx.Guild.ID].EmbedColor = int(color) |
||
| 165 | _ = ctx.DB.Guilds().Update(bson.M{"id": ctx.Guild.ID}, bson.M{"$set": bson.M{"embedcolor": int(color)}}) |
||
| 166 | ctx.ReplyEmbedPM("Config", fmt.Sprintf("Embed color set to: %v", ctx.Args[2])) |
||
| 167 | } |
||
| 274 | } |