Conditions | 16 |
Total Lines | 83 |
Code Lines | 68 |
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.YoutubeShortCommand 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 |
||
215 | func YoutubeShortCommand(ctx bot.Context) { |
||
216 | ctx.MetricsCommand("youtube_command", "short") |
||
217 | sess := ctx.Sessions.GetByGuild(ctx.Guild.ID) |
||
218 | newargs := ctx.Args |
||
219 | if len(newargs) == 0 { |
||
220 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_args_missing")) |
||
221 | return |
||
222 | } |
||
223 | if sess == nil { |
||
224 | vc := ctx.GetVoiceChannel() |
||
225 | if vc == nil { |
||
226 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_must_be_in_voice")) |
||
227 | return |
||
228 | } |
||
229 | nsess, serr := ctx.Sessions.Join(ctx.Discord, ctx.Guild.ID, vc.ID, bot.JoinProperties{ |
||
230 | Muted: false, |
||
231 | Deafened: true, |
||
232 | }, ctx.Guilds.Guilds[ctx.Guild.ID].VoiceVolume) |
||
233 | sess = nsess |
||
234 | if serr != nil { |
||
235 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("session error: %v", serr.Error())) |
||
236 | //ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), ctx.Loc("player_error")) |
||
237 | return |
||
238 | } |
||
239 | ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("player")), fmt.Sprintf("%v <#%v>!", ctx.Loc("player_joined"), sess.ChannelID)) |
||
240 | } |
||
241 | msg := ctx.ReplyEmbed(fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_adding_song")) |
||
242 | for _, arg := range newargs { |
||
243 | t, inp, err := ctx.Youtube.Get(arg) |
||
244 | |||
245 | if err != nil { |
||
246 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("error getting input: %v", err.Error())) |
||
247 | return |
||
248 | } |
||
249 | |||
250 | switch t { |
||
251 | case bot.ERROR_TYPE: |
||
252 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("error type: %v", t)) |
||
253 | fmt.Println("error type", t) |
||
254 | return |
||
255 | case bot.VIDEO_TYPE: |
||
256 | { |
||
257 | video, err := ctx.Youtube.Video(*inp) |
||
258 | if err != nil { |
||
259 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("error getting video1: %v", err.Error())) |
||
260 | return |
||
261 | } |
||
262 | song := bot.NewSong(video.Media, video.Title, arg) |
||
263 | sess.Queue.Add(song) |
||
264 | ctx.EditEmbed(msg.ID, fmt.Sprintf("%v:", ctx.Loc("youtube")), fmt.Sprintf(ctx.Loc("youtube_added_format"), song.Title), true) |
||
265 | shortPlay(&ctx, sess, msg) |
||
266 | break |
||
267 | } |
||
268 | case bot.PLAYLIST_TYPE: |
||
269 | { |
||
270 | videos, err := ctx.Youtube.Playlist(*inp) |
||
271 | if err != nil { |
||
272 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("error getting playlist: %v", err.Error())) |
||
273 | return |
||
274 | } |
||
275 | var isPlaying bool |
||
276 | for _, v := range *videos { |
||
277 | id := v.Id |
||
278 | _, i, err := ctx.Youtube.Get(id) |
||
279 | if err != nil { |
||
280 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("error getting video2: %v", err.Error())) |
||
281 | continue |
||
282 | } |
||
283 | video, err := ctx.Youtube.Video(*i) |
||
284 | if err != nil { |
||
285 | ctx.Log("Youtube", ctx.Guild.ID, fmt.Sprintf("error getting video3: %v", err.Error())) |
||
286 | return |
||
287 | } |
||
288 | song := bot.NewSong(video.Media, video.Title, arg) |
||
289 | sess.Queue.Add(song) |
||
290 | ctx.EditEmbed(msg.ID, fmt.Sprintf("%v:", ctx.Loc("youtube")), fmt.Sprintf(ctx.Loc("youtube_added_format"), song.Title), true) |
||
291 | if !isPlaying { |
||
292 | shortPlay(&ctx, sess, msg) |
||
293 | isPlaying = true |
||
294 | } |
||
295 | } |
||
296 | ctx.EditEmbed(msg.ID, fmt.Sprintf("%v:", ctx.Loc("youtube")), ctx.Loc("youtube_added"), true) |
||
297 | break |
||
298 | } |
||
302 |