| Conditions | 48 |
| Total Lines | 158 |
| Code Lines | 129 |
| 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.BotCommand 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 |
||
| 24 | func BotCommand(ctx bot.Context) { |
||
| 25 | ctx.MetricsCommand("bot") |
||
| 26 | if ctx.GetRoles().ExistsName("bot.admin") { |
||
| 27 | if len(ctx.Args) == 0 { |
||
| 28 | return |
||
| 29 | } |
||
| 30 | switch ctx.Args[0] { |
||
| 31 | case "clear": |
||
| 32 | if len(ctx.Args) < 2 { |
||
| 33 | ctx.BotMsg.Clear(&ctx, 0) |
||
| 34 | return |
||
| 35 | } |
||
| 36 | from, err := strconv.Atoi(ctx.Args[1]) |
||
| 37 | if err != nil { |
||
| 38 | return |
||
| 39 | } |
||
| 40 | ctx.BotMsg.Clear(&ctx, from) |
||
| 41 | case "help": |
||
| 42 | ctx.ReplyEmbed(ctx.Loc("help"), ctx.Loc("help_reply")) |
||
| 43 | case "logs": |
||
| 44 | if ctx.IsAdmin() { |
||
| 45 | if len(ctx.Args) < 2 { |
||
| 46 | showLogs(&ctx, 10) |
||
| 47 | } else { |
||
| 48 | count, err := strconv.Atoi(ctx.Args[1]) |
||
| 49 | if err != nil { |
||
| 50 | fmt.Println(err) |
||
| 51 | return |
||
| 52 | } |
||
| 53 | showLogs(&ctx, count) |
||
| 54 | } |
||
| 55 | } |
||
| 56 | case "setconf": |
||
| 57 | if len(ctx.Args) > 2 { |
||
| 58 | target := strings.Split(ctx.Args[1], ".") |
||
| 59 | switch target[0] { |
||
| 60 | case "general": |
||
| 61 | switch target[1] { |
||
| 62 | case "language": |
||
| 63 | ctx.Guilds[ctx.Guild.ID].Language = ctx.Args[2] |
||
| 64 | ctx.DB.Guilds().Update(bson.M{"id": ctx.Guild.ID}, bson.M{"$set": bson.M{"language": ctx.Args[2]}}) |
||
| 65 | ctx.ReplyEmbedPM("Config", fmt.Sprintf("Language set to: %v", ctx.Args[2])) |
||
| 66 | case "timezone": |
||
| 67 | tz, err := strconv.Atoi(ctx.Args[1]) |
||
| 68 | if err != nil { |
||
| 69 | ctx.ReplyEmbedPM("Settings", "Not a number") |
||
| 70 | } |
||
| 71 | ctx.Guilds[ctx.Guild.ID].Timezone = tz |
||
| 72 | ctx.DB.Guilds().Update(bson.M{"id": ctx.Guild.ID}, bson.M{"$set": bson.M{"timezone": tz}}) |
||
| 73 | ctx.ReplyEmbedPM("Config", fmt.Sprintf("Timezone set to: %v", ctx.Args[2])) |
||
| 74 | case "nick": |
||
| 75 | ctx.Discord.GuildMemberNickname(ctx.Guild.ID, "@me", ctx.Args[2]) |
||
| 76 | ctx.ReplyEmbedPM("Config", fmt.Sprintf("Nickname changed to %v", ctx.Args[2])) |
||
| 77 | } |
||
| 78 | case "weather": |
||
| 79 | switch target[1] { |
||
| 80 | case "city": |
||
| 81 | ctx.Guilds[ctx.Guild.ID].WeatherCity = ctx.Args[2] |
||
| 82 | ctx.DB.Guilds().Update(bson.M{"id": ctx.Guild.ID}, bson.M{"$set": bson.M{"weathercity": ctx.Args[2]}}) |
||
| 83 | ctx.ReplyEmbedPM("Config", fmt.Sprintf("Weather city set to: %v", ctx.Args[2])) |
||
| 84 | } |
||
| 85 | case "news": |
||
| 86 | switch target[1] { |
||
| 87 | case "country": |
||
| 88 | ctx.Guilds[ctx.Guild.ID].NewsCounty = ctx.Args[2] |
||
| 89 | ctx.DB.Guilds().Update(bson.M{"id": ctx.Guild.ID}, bson.M{"$set": bson.M{"weathercountry": ctx.Args[2]}}) |
||
| 90 | ctx.ReplyEmbedPM("Config", fmt.Sprintf("News country set to: %v", ctx.Args[2])) |
||
| 91 | } |
||
| 92 | case "embed": |
||
| 93 | switch target[1] { |
||
| 94 | case "color": |
||
| 95 | var color int64 |
||
| 96 | var err error |
||
| 97 | if strings.HasPrefix(ctx.Args[2], "#") { |
||
| 98 | color, err = strconv.ParseInt(ctx.Args[2][1:], 16, 32) |
||
| 99 | if err != nil { |
||
| 100 | ctx.Log("Config", ctx.Guild.ID, fmt.Sprintf("error setting parameter %v to value %v: %v", ctx.Args[1], target[2], err.Error())) |
||
| 101 | return |
||
| 102 | } |
||
| 103 | } else { |
||
| 104 | color, err = strconv.ParseInt(ctx.Args[2], 16, 32) |
||
| 105 | if err != nil { |
||
| 106 | ctx.Log("Config", ctx.Guild.ID, fmt.Sprintf("error setting parameter %v to value %v: %v", ctx.Args[1], ctx.Args[2], err.Error())) |
||
| 107 | return |
||
| 108 | } |
||
| 109 | } |
||
| 110 | ctx.Guilds[ctx.Guild.ID].EmbedColor = int(color) |
||
| 111 | ctx.DB.Guilds().Update(bson.M{"id": ctx.Guild.ID}, bson.M{"$set": bson.M{"embedcolor": int(color)}}) |
||
| 112 | ctx.ReplyEmbedPM("Config", fmt.Sprintf("Embed color set to: %v", ctx.Args[2])) |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 | case "guild": |
||
| 117 | if len(ctx.Args) < 2 && !ctx.IsAdmin() { |
||
| 118 | return |
||
| 119 | } |
||
| 120 | switch ctx.Args[1] { |
||
| 121 | case "leave": |
||
| 122 | if len(ctx.Args) < 3 { |
||
| 123 | return |
||
| 124 | } |
||
| 125 | err := ctx.Discord.GuildLeave(ctx.Args[2]) |
||
| 126 | if err != nil { |
||
| 127 | ctx.Log("Guild", ctx.Guild.ID, fmt.Sprintf("error leaving from guild [%v]: %v", ctx.Args[2], err.Error())) |
||
| 128 | ctx.ReplyEmbedPM("Guild", fmt.Sprintf("Error leaving from guild [%v]: %v", ctx.Args[2], err.Error())) |
||
| 129 | return |
||
| 130 | } |
||
| 131 | ctx.ReplyEmbedPM("Guild", fmt.Sprintf("Leave from guild: %v", ctx.Args[2])) |
||
| 132 | case "list": |
||
| 133 | var selected string |
||
| 134 | if len(ctx.Args) > 2 && ctx.Args[2] == "id" { |
||
| 135 | if len(ctx.Args) > 3 { |
||
| 136 | selected = ctx.Args[3] |
||
| 137 | } else { |
||
| 138 | selected = "1" |
||
| 139 | } |
||
| 140 | } else { |
||
| 141 | if len(ctx.Args) > 2 { |
||
| 142 | selected = ctx.Args[2] |
||
| 143 | } else { |
||
| 144 | selected = "1" |
||
| 145 | } |
||
| 146 | } |
||
| 147 | guilds := ctx.Discord.State.Guilds |
||
| 148 | pages := int(len(guilds)/20) + 1 |
||
| 149 | if len(ctx.Args) > 3 { |
||
| 150 | index := 0 |
||
| 151 | page, err := strconv.Atoi(selected) |
||
| 152 | if err == nil { |
||
| 153 | index = page |
||
| 154 | if index > 0 { |
||
| 155 | index = index * 20 |
||
| 156 | } else { |
||
| 157 | index = 0 |
||
| 158 | } |
||
| 159 | if index > len(guilds) { |
||
| 160 | index = len(guilds) |
||
| 161 | } |
||
| 162 | } |
||
| 163 | var indexEnd = index + 20 |
||
| 164 | if indexEnd > len(guilds) { |
||
| 165 | indexEnd = len(guilds) |
||
| 166 | } |
||
| 167 | if len(ctx.Args) > 2 && ctx.Args[2] == "id" { |
||
| 168 | ctx.ReplyEmbed("Guilds", guildsListID(guilds[index:indexEnd], page, pages)) |
||
| 169 | } else { |
||
| 170 | ctx.ReplyEmbed("Guilds", guildsListName(guilds[index:indexEnd], page, pages)) |
||
| 171 | } |
||
| 172 | |||
| 173 | } else { |
||
| 174 | var indexEnd = 20 |
||
| 175 | if indexEnd > len(guilds) { |
||
| 176 | indexEnd = len(guilds) |
||
| 177 | } |
||
| 178 | if len(ctx.Args) > 2 && ctx.Args[2] == "id" { |
||
| 179 | ctx.ReplyEmbed("Guilds", guildsListID(guilds[:indexEnd], 1, 1)) |
||
| 180 | } else { |
||
| 181 | ctx.ReplyEmbed("Guilds", guildsListName(guilds[:indexEnd], 1, 1)) |
||
| 182 | } |
||
| 219 |