| Conditions | 38 |
| Total Lines | 155 |
| Code Lines | 115 |
| 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.botGuild 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 |
||
| 174 | func botGuild(ctx *bot.Context) { |
||
| 175 | ctx.MetricsCommand("bot", "guild") |
||
| 176 | if len(ctx.Args) < 2 && !ctx.IsAdmin() { |
||
| 177 | return |
||
| 178 | } |
||
| 179 | switch ctx.Arg(1) { |
||
| 180 | case "leave": |
||
| 181 | if len(ctx.Args) < 3 { |
||
| 182 | return |
||
| 183 | } |
||
| 184 | err := ctx.Discord.GuildLeave(ctx.Args[2]) |
||
| 185 | if err != nil { |
||
| 186 | ctx.Log("Guild", ctx.Guild.ID, fmt.Sprintf("error leaving from guild [%v]: %v", ctx.Args[2], err.Error())) |
||
| 187 | ctx.ReplyEmbedPM("Guild", fmt.Sprintf("Error leaving from guild [%v]: %v", ctx.Args[2], err.Error())) |
||
| 188 | return |
||
| 189 | } |
||
| 190 | ctx.ReplyEmbedPM("Guild", fmt.Sprintf("Leave from guild: %v", ctx.Args[2])) |
||
| 191 | case "info": |
||
| 192 | var guild *discordgo.Guild |
||
| 193 | var err error |
||
| 194 | |||
| 195 | if len(ctx.Args) < 3 { |
||
| 196 | guild = ctx.Guild |
||
| 197 | } else { |
||
| 198 | guild, err = ctx.Discord.Guild(ctx.Args[2]) |
||
| 199 | if err != nil { |
||
| 200 | ctx.ReplyEmbed(ctx.Loc("guild_info"), ctx.Loc("guild_not_found")) |
||
| 201 | return |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | var ( |
||
| 206 | usersOnline int |
||
| 207 | usersOffline int |
||
| 208 | usersIdle int |
||
| 209 | usersDND int |
||
| 210 | usersBot int |
||
| 211 | channelsVoice int |
||
| 212 | channelsText int |
||
| 213 | guildEmojis int |
||
| 214 | guildRoles string |
||
| 215 | guildUsers int |
||
| 216 | guildOwner string |
||
| 217 | ) |
||
| 218 | |||
| 219 | for _, m := range guild.Members { |
||
| 220 | if m.User.ID == guild.OwnerID { |
||
| 221 | guildOwner = m.User.Username + "#" + m.User.Discriminator |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | for _, p := range guild.Presences { |
||
| 226 | switch p.Status { |
||
| 227 | case discordgo.StatusOnline: |
||
| 228 | usersOnline++ |
||
| 229 | case discordgo.StatusIdle: |
||
| 230 | usersIdle++ |
||
| 231 | case discordgo.StatusDoNotDisturb: |
||
| 232 | usersDND++ |
||
| 233 | } |
||
| 234 | } |
||
| 235 | guildUsers = len(guild.Members) |
||
| 236 | usersOffline = guild.MemberCount - (usersOnline + usersIdle + usersDND) |
||
| 237 | |||
| 238 | for _, m := range guild.Members { |
||
| 239 | if m.User.Bot { |
||
| 240 | usersBot++ |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | for _, c := range guild.Channels { |
||
| 245 | switch c.Type { |
||
| 246 | case discordgo.ChannelTypeGuildText: |
||
| 247 | channelsText++ |
||
| 248 | case discordgo.ChannelTypeGuildVoice: |
||
| 249 | channelsVoice++ |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | for _, _ = range guild.Emojis { |
||
| 254 | guildEmojis++ |
||
| 255 | } |
||
| 256 | |||
| 257 | for _, r := range guild.Roles { |
||
| 258 | if r.Name == "@everyone" { |
||
| 259 | continue |
||
| 260 | } |
||
| 261 | if len(guildRoles+r.Name) < 100 { |
||
| 262 | guildRoles += r.Name + "\n" |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | emb := bot.NewEmbed(ctx.Loc("guild_info")) |
||
| 267 | emb.Color(ctx.GetGuild().EmbedColor) |
||
| 268 | emb.Field(ctx.Loc("guild_name"), guild.Name, true) |
||
| 269 | emb.Field(ctx.Loc("guild_emoji"), fmt.Sprintf(ctx.Loc("guild_emoji_count"), guildEmojis), true) |
||
| 270 | emb.Field(ctx.Loc("guild_channels"), fmt.Sprintf(ctx.Loc("guild_channels_format"), channelsText, channelsVoice), true) |
||
| 271 | emb.Field(ctx.Loc("guild_id"), guild.ID, true) |
||
| 272 | emb.Field(ctx.Loc("guild_users"), fmt.Sprintf(ctx.Loc("guild_users_format"), guildUsers, usersOnline, usersOffline, usersIdle, usersDND, usersBot), true) |
||
| 273 | emb.Field(ctx.Loc("guild_roles"), guildRoles, true) |
||
| 274 | emb.Field(ctx.Loc("guild_owner"), fmt.Sprintf(ctx.Loc("guild_owner_format"), guildOwner, guild.OwnerID), true) |
||
| 275 | emb.Send(ctx) |
||
| 276 | case "list": |
||
| 277 | var selected string |
||
| 278 | var paged = false |
||
| 279 | if len(ctx.Args) > 2 && ctx.Args[2] == "id" { |
||
| 280 | if len(ctx.Args) > 3 { |
||
| 281 | selected = ctx.Args[3] |
||
| 282 | paged = true |
||
| 283 | } else { |
||
| 284 | selected = "1" |
||
| 285 | } |
||
| 286 | } else { |
||
| 287 | if len(ctx.Args) > 2 { |
||
| 288 | selected = ctx.Args[2] |
||
| 289 | paged = true |
||
| 290 | } else { |
||
| 291 | selected = "1" |
||
| 292 | } |
||
| 293 | } |
||
| 294 | // calculates count of pages |
||
| 295 | guilds := ctx.Discord.State.Guilds |
||
| 296 | pages := 1 + int(len(guilds)/20) |
||
| 297 | // paginate |
||
| 298 | var indexTo = 20 |
||
| 299 | if paged { |
||
| 300 | page, err := strconv.Atoi(selected) |
||
| 301 | if err == nil { |
||
| 302 | indexTo = page * 20 |
||
| 303 | indexFrom := indexTo - 20 |
||
| 304 | |||
| 305 | if indexFrom < 0 { |
||
| 306 | indexFrom = 0 |
||
| 307 | } |
||
| 308 | if indexTo > len(guilds) { |
||
| 309 | indexTo = len(guilds) - 1 |
||
| 310 | } |
||
| 311 | if len(ctx.Args) > 2 && ctx.Args[2] == "id" { |
||
| 312 | ctx.ReplyEmbed("Guilds", guildsListID(guilds[indexFrom:indexTo], page, pages)+fmt.Sprintf("\nFrom: %v\nTo: %v", indexFrom, indexTo)) |
||
| 313 | } else { |
||
| 314 | ctx.ReplyEmbed("Guilds", guildsListName(guilds[indexFrom:indexTo], page, pages)+fmt.Sprintf("\nFrom: %v\nTo: %v", indexFrom, indexTo)) |
||
| 315 | } |
||
| 316 | |||
| 317 | } else { |
||
| 318 | ctx.ReplyEmbed("Guilds", fmt.Sprintf("Selected: %v\nError: %v", selected, err.Error())) |
||
| 319 | } |
||
| 320 | |||
| 321 | } else { |
||
| 322 | if indexTo > len(guilds) { |
||
| 323 | indexTo = len(guilds) - 1 |
||
| 324 | } |
||
| 325 | if len(ctx.Args) > 2 && ctx.Args[2] == "id" { |
||
| 326 | ctx.ReplyEmbed("Guilds", guildsListID(guilds[:indexTo], 1, 1)+fmt.Sprintf("\nTo: %v", indexTo)) |
||
| 327 | } else { |
||
| 328 | ctx.ReplyEmbed("Guilds", guildsListName(guilds[:indexTo], 1, 1)+fmt.Sprintf("\nTo: %v", indexTo)) |
||
| 329 | } |
||
| 382 |