cmd.TwitchCommand   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 16
nop 1
dl 0
loc 18
rs 8
c 0
b 0
f 0
1
package cmd
2
3
import (
4
	"fmt"
5
	"github.com/FlameInTheDark/dtbot/bot"
6
	"strings"
7
)
8
9
// TwitchCommand manipulates twitch announcer
10
func TwitchCommand(ctx bot.Context) {
11
	if ctx.IsServerAdmin() {
12
		if len(ctx.Args) == 0 {
13
			return
14
		}
15
		switch ctx.Args[0] {
16
		case "add":
17
			twitchAdd(&ctx)
18
		case "remove":
19
			twitchRemove(&ctx)
20
		case "list":
21
			twitchList(&ctx)
22
		case "count":
23
			twitchCount(&ctx)
24
		}
25
	} else {
26
		ctx.ReplyEmbed("Twitch", ctx.Loc("admin_require"))
27
		ctx.MetricsCommand("twitch", "error")
28
	}
29
}
30
31
func twitchAdd(ctx *bot.Context) {
32
	ctx.MetricsCommand("twitch", "add")
33
	if len(ctx.Args) > 2 {
34
		username, err := ctx.Twitch.AddStreamer(ctx.Guild.ID, ctx.Message.ChannelID, ctx.Args[1], strings.Join(ctx.Args[2:], " "))
35
		if err != nil {
36
			ctx.ReplyEmbed("Twitch", ctx.Loc("twitch_add_error"))
37
		} else {
38
			ctx.ReplyEmbed("Twitch", fmt.Sprintf(ctx.Loc("twitch_added"), username))
0 ignored issues
show
introduced by
can't check non-constant format in call to Sprintf
Loading history...
39
		}
40
	} else if len(ctx.Args) > 1 {
41
		username, err := ctx.Twitch.AddStreamer(ctx.Guild.ID, ctx.Message.ChannelID, ctx.Args[1], "")
42
		if err != nil {
43
			ctx.ReplyEmbed("Twitch", ctx.Loc("twitch_add_error"))
44
		} else {
45
			ctx.ReplyEmbed("Twitch", fmt.Sprintf(ctx.Loc("twitch_added"), username))
0 ignored issues
show
introduced by
can't check non-constant format in call to Sprintf
Loading history...
46
		}
47
	}
48
}
49
50
func twitchRemove(ctx *bot.Context) {
51
	ctx.MetricsCommand("twitch", "remove")
52
	if len(ctx.Args) > 1 {
53
		err := ctx.Twitch.RemoveStreamer(ctx.Args[1], ctx.Guild.ID)
54
		if err != nil {
55
			ctx.ReplyEmbed("Twitch", ctx.Loc("twitch_remove_error"))
56
		} else {
57
			ctx.ReplyEmbed("Twitch", ctx.Loc("twitch_removed"))
58
		}
59
	}
60
}
61
62
func twitchList(ctx *bot.Context) {
63
	ctx.MetricsCommand("twitch", "list")
64
	if g, ok := ctx.Twitch.Guilds[ctx.Guild.ID]; ok {
65
		if len(g.Streams) > 0 {
66
			list := ""
67
			var counter int
68
			if g.Streams != nil {
69
				for _, s := range g.Streams {
70
					list += fmt.Sprintf("%v. %v\n", counter, s.Login)
71
					counter++
72
				}
73
				ctx.ReplyEmbed("Twitch", fmt.Sprintf(ctx.Loc("twitch_list"), list))
0 ignored issues
show
introduced by
can't check non-constant format in call to Sprintf
Loading history...
74
			} else {
75
				ctx.ReplyEmbed("Twitch", ctx.Loc("twitch_list_empty"))
76
			}
77
78
		} else {
79
			ctx.ReplyEmbed("Twitch", ctx.Loc("twitch_list_empty"))
80
		}
81
	} else {
82
		ctx.ReplyEmbed("Twitch", ctx.Loc("twitch_list_empty"))
83
	}
84
}
85
86
func twitchCount(ctx *bot.Context) {
87
	ctx.MetricsCommand("twitch", "count")
88
	if ctx.IsAdmin() {
89
		count := 0
90
		for _, g := range ctx.Twitch.Guilds {
91
			count += len(g.Streams)
92
		}
93
		ctx.ReplyEmbed("Twitch", fmt.Sprintf("Streamers: %v", count))
94
	}
95
}
96