| Conditions | 10 |
| Total Lines | 65 |
| Code Lines | 55 |
| 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 main.commandHandler 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 main |
||
| 86 | func commandHandler(discord *discordgo.Session, message *discordgo.MessageCreate) { |
||
| 87 | messagesCounter++ |
||
| 88 | user := message.Author |
||
| 89 | if user.ID == botId || user.Bot { |
||
| 90 | return |
||
| 91 | } |
||
| 92 | args := strings.Split(message.Content, " ") |
||
| 93 | name := strings.ToLower(args[0]) |
||
| 94 | command, found := CmdHandler.Get(name) |
||
| 95 | if !found { |
||
| 96 | return |
||
| 97 | } |
||
| 98 | |||
| 99 | var permission = true |
||
| 100 | var msg string |
||
| 101 | // Checking permissions |
||
| 102 | perm, err := discord.State.UserChannelPermissions(botId, message.ChannelID) |
||
| 103 | if err != nil { |
||
| 104 | msg = fmt.Sprintf("Error whilst getting bot permissions %v\n", err) |
||
| 105 | permission = false |
||
| 106 | } else { |
||
| 107 | if perm&discordgo.PermissionSendMessages != discordgo.PermissionSendMessages || |
||
| 108 | perm&discordgo.PermissionAttachFiles != discordgo.PermissionAttachFiles { |
||
| 109 | msg = "Permissions denied" |
||
| 110 | permission = false |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | channel, err := discord.State.Channel(message.ChannelID) |
||
| 115 | if err != nil { |
||
| 116 | fmt.Println("Error getting channel,", err) |
||
| 117 | return |
||
| 118 | } |
||
| 119 | guild, err := discord.State.Guild(channel.GuildID) |
||
| 120 | if err != nil { |
||
| 121 | fmt.Println("Error getting guild,", err) |
||
| 122 | return |
||
| 123 | } |
||
| 124 | |||
| 125 | if permission { |
||
| 126 | ctx := bot.NewContext( |
||
| 127 | botId, |
||
| 128 | discord, |
||
| 129 | guild, |
||
| 130 | channel, |
||
| 131 | user, |
||
| 132 | message, |
||
| 133 | conf, |
||
| 134 | CmdHandler, |
||
| 135 | Sessions, |
||
| 136 | youtube, |
||
| 137 | botMsg, |
||
| 138 | dataType, |
||
| 139 | dbWorker, |
||
| 140 | guilds, |
||
| 141 | botCron) |
||
| 142 | ctx.Args = args[1:] |
||
| 143 | c := *command |
||
| 144 | c(*ctx) |
||
| 145 | } else { |
||
| 146 | dbWorker.Log("Message", guild.ID, msg) |
||
| 147 | query := []byte(fmt.Sprintf("logs,server=%v module=\"%v\"", guild.ID, "message")) |
||
| 148 | addr := fmt.Sprintf("%v/write?db=%v", conf.Metrics.Address, conf.Metrics.Database) |
||
| 149 | r := bytes.NewReader(query) |
||
| 150 | _, _ = http.Post(addr, "", r) |
||
| 151 | } |
||
| 234 | } |