| Conditions | 9 |
| Total Lines | 53 |
| Code Lines | 44 |
| 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:
| 1 | package main |
||
| 77 | func commandHandler(discord *discordgo.Session, message *discordgo.MessageCreate) { |
||
| 78 | user := message.Author |
||
| 79 | if user.ID == botId || user.Bot { |
||
| 80 | return |
||
| 81 | } |
||
| 82 | args := strings.Split(message.Content, " ") |
||
| 83 | name := strings.ToLower(args[0]) |
||
| 84 | command, found := CmdHandler.Get(name) |
||
| 85 | if !found { |
||
| 86 | return |
||
| 87 | } |
||
| 88 | channel, err := discord.State.Channel(message.ChannelID) |
||
| 89 | if err != nil { |
||
| 90 | fmt.Println("Error getting channel,", err) |
||
| 91 | return |
||
| 92 | } |
||
| 93 | guild, err := discord.State.Guild(channel.GuildID) |
||
| 94 | if err != nil { |
||
| 95 | fmt.Println("Error getting guild,", err) |
||
| 96 | return |
||
| 97 | } |
||
| 98 | // Checking permissions |
||
| 99 | perm, err := discord.State.UserChannelPermissions(botId, message.ChannelID) |
||
| 100 | if err != nil { |
||
| 101 | fmt.Printf("Error whilst getting bot permissions in guild \"%v\", %v\n", guild.ID ,err) |
||
| 102 | return |
||
| 103 | } |
||
| 104 | |||
| 105 | if perm&discordgo.PermissionSendMessages != discordgo.PermissionSendMessages || |
||
| 106 | perm&discordgo.PermissionAttachFiles != discordgo.PermissionAttachFiles{ |
||
| 107 | fmt.Printf("Permissions denied on guild \"%v\"\n", guild.ID) |
||
| 108 | return |
||
| 109 | } |
||
| 110 | |||
| 111 | ctx := bot.NewContext( |
||
| 112 | botId, |
||
| 113 | discord, |
||
| 114 | guild, |
||
| 115 | channel, |
||
| 116 | user, |
||
| 117 | message, |
||
| 118 | conf, |
||
| 119 | CmdHandler, |
||
| 120 | Sessions, |
||
| 121 | youtube, |
||
| 122 | botMsg, |
||
| 123 | dataType, |
||
| 124 | dbWorker, |
||
| 125 | guilds, |
||
| 126 | botCron) |
||
| 127 | ctx.Args = args[1:] |
||
| 128 | c := *command |
||
| 129 | c(*ctx) |
||
| 130 | } |
||
| 149 |