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