1
|
|
|
package cmd |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"fmt" |
5
|
|
|
"github.com/FlameInTheDark/dtbot/bot" |
6
|
|
|
"gopkg.in/robfig/cron.v2" |
7
|
|
|
"strconv" |
8
|
|
|
"strings" |
9
|
|
|
) |
10
|
|
|
|
11
|
|
|
// CronCommand manipulates cron functions |
12
|
|
|
func CronCommand(ctx bot.Context) { |
13
|
|
|
if ctx.IsServerAdmin() { |
14
|
|
|
// !cron add 0 0 7 * * * !w Chelyabinsk |
15
|
|
|
switch ctx.Arg(0) { |
16
|
|
|
case "add": |
17
|
|
|
ctx.MetricsCommand("cron", "add") |
18
|
|
|
if len(ctx.Args) > 7 { |
19
|
|
|
if ctx.Args[0] != "*" && ctx.Args[1] != "*" && ctx.Args[2] != "*" { |
20
|
|
|
if len(ctx.Args) > 7 { |
21
|
|
|
if !ctx.Data.CronIsFull(&ctx) { |
22
|
|
|
cmd := strings.Join(ctx.Args[1:], " ") |
23
|
|
|
cronTime := strings.Join(ctx.Args[1:7], " ") |
24
|
|
|
trigger := ctx.Args[7] |
25
|
|
|
ctx.Args = ctx.Args[8:] |
26
|
|
|
id, _ := ctx.Cron.AddFunc(cronTime, func() { |
27
|
|
|
switch trigger { |
28
|
|
|
case "!w": |
29
|
|
|
WeatherCommand(ctx) |
30
|
|
|
case "!c": |
31
|
|
|
CurrencyCommand(ctx) |
32
|
|
|
case "!p": |
33
|
|
|
PollCommand(ctx) |
34
|
|
|
case "!v": |
35
|
|
|
VoiceCommand(ctx) |
36
|
|
|
case "!y": |
37
|
|
|
YoutubeCommand(ctx) |
38
|
|
|
case "!play": |
39
|
|
|
YoutubeShortCommand(ctx) |
40
|
|
|
case "!b": |
41
|
|
|
BotCommand(ctx) |
42
|
|
|
case "!n": |
43
|
|
|
NewsCommand(ctx) |
44
|
|
|
} |
45
|
|
|
}) |
46
|
|
|
_ = ctx.Data.AddCronJob(&ctx, id, cmd) |
47
|
|
|
ctx.ReplyEmbedPM("Cron", fmt.Sprintf("Job added: [%v] [%v]", cmd, id)) |
48
|
|
|
} else { |
49
|
|
|
ctx.ReplyEmbedPM("Cron", "Schedule is full") |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
case "remove": |
55
|
|
|
ctx.MetricsCommand("cron", "remove") |
56
|
|
|
val, err := strconv.Atoi(ctx.Args[1]) |
57
|
|
|
if err != nil { |
58
|
|
|
ctx.ReplyEmbedPM("Cron", err.Error()) |
59
|
|
|
return |
60
|
|
|
} |
61
|
|
|
cErr := ctx.Data.CronRemove(&ctx, cron.EntryID(val)) |
62
|
|
|
if cErr != nil { |
63
|
|
|
ctx.ReplyEmbedPM("Cron", "Error adding job") |
64
|
|
|
fmt.Println("Error adding job: ", cErr.Error()) |
65
|
|
|
return |
66
|
|
|
} |
67
|
|
|
ctx.ReplyEmbedPM("Cron", "Job removed") |
68
|
|
|
case "list": |
69
|
|
|
ctx.MetricsCommand("cron", "list") |
70
|
|
|
s, err := ctx.Data.CronList(&ctx) |
71
|
|
|
if err != nil { |
72
|
|
|
ctx.ReplyEmbedPM("Cron", err.Error()) |
73
|
|
|
return |
74
|
|
|
} |
75
|
|
|
var reply = []string{"Jobs:"} |
76
|
|
|
for key, val := range s.CronJobs { |
77
|
|
|
reply = append(reply, fmt.Sprintf("[%v] - [%v]", key, val)) |
78
|
|
|
} |
79
|
|
|
ctx.ReplyEmbedPM("Cron", strings.Join(reply, "\n")) |
80
|
|
|
} |
81
|
|
|
} else { |
82
|
|
|
ctx.MetricsCommand("cron", "error") |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|