Completed
Push — master ( e7c2dd...9298ac )
by Viktor
02:25
created

cmd.BotCommand   F

Complexity

Conditions 48

Size

Total Lines 158
Code Lines 129

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 48
eloc 129
nop 1
dl 0
loc 158
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

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
2
3
import (
4
	"fmt"
5
	"github.com/bwmarrin/discordgo"
6
	"github.com/globalsign/mgo/bson"
7
	"strconv"
8
	"strings"
9
10
	"github.com/FlameInTheDark/dtbot/bot"
11
)
12
13
func showLogs(ctx *bot.Context, count int) {
14
	logs := ctx.DB.LogGet(count)
15
	var logString = []string{""}
16
	for _, log := range logs {
17
		logString = append(logString, fmt.Sprintf("[%v] %v: %v\n", log.Date, log.Module, log.Text))
18
	}
19
	ctx.ReplyEmbedPM("Logs", strings.Join(logString, ""))
20
}
21
22
// TODO: I should make it more tasty and remove all "switch/case"!
0 ignored issues
show
introduced by
comment on exported function BotCommand should be of the form "BotCommand ..."
Loading history...
23
// BotCommand special bot commands handler
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
					}
183
184
				}
185
			}
186
		}
187
	}
188
}
189
190
func guildsListID(guilds []*discordgo.Guild, current, pages int) string {
191
	var list string
192
	for _, g := range guilds {
193
		var gName string
194
		if len(g.Name) > 20 {
195
			gName = fmt.Sprintf("%v...", g.Name[:20])
196
		} else {
197
			gName = g.Name
198
		}
199
		list += fmt.Sprintf("[%v] - %v\n", g.ID, gName)
200
	}
201
	list += fmt.Sprintf("Page: %v | %v", current, pages)
202
	return list
203
}
204
205
func guildsListName(guilds []*discordgo.Guild, current, pages int) string {
206
	var list string
207
	for i, g := range guilds {
208
		var gName string
209
		if len(g.Name) > 20 {
210
			gName = fmt.Sprintf("%v...", g.Name[:20])
211
		} else {
212
			gName = g.Name
213
		}
214
		list += fmt.Sprintf("[%v] - %v | U: %v\n", i, gName, len(g.Members))
215
	}
216
	list += fmt.Sprintf("Page: %v | %v", current, pages)
217
	return list
218
}
219