-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
89 lines (79 loc) · 1.92 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"context"
"flag"
"fmt"
"os"
"github.com/discord-gophers/dgobot/commands"
"github.com/bwmarrin/discordgo"
"github.com/peterbourgon/ff/v3"
"github.com/peterbourgon/ff/v3/ffcli"
)
var (
session *discordgo.Session
GuildID string
AppID string
)
func main() {
fs := flag.NewFlagSet("dgobot", flag.ExitOnError)
token := fs.String("token", "", "Discord Authentication Token")
fs.StringVar(&GuildID, "guild-id", "", "Discord Guild ID")
cmd := ffcli.Command{
Name: "manage",
ShortUsage: `go run manage.go -token "<token>" -guild-id "<guildID> add|remove"`,
FlagSet: fs,
Options: []ff.Option{ff.WithEnvVarPrefix("DG")},
Subcommands: []*ffcli.Command{
{
Name: "add",
ShortUsage: "Add/upsert/overwrite commands",
Exec: add,
},
{
Name: "remove",
ShortUsage: "Remove/delete commands",
Exec: remove,
},
},
Exec: func(ctx context.Context, args []string) error {
return fmt.Errorf("error: you did not provide command add | remove")
},
}
err := cmd.Parse(os.Args[1:])
if err != nil {
panic(err)
}
session, err = discordgo.New("Bot " + *token)
if err != nil {
panic(err)
}
user, err := session.User("@me")
if err != nil {
panic(err)
}
AppID = user.ID
if err := cmd.Run(context.Background()); err != nil {
panic(err)
}
}
func add(context.Context, []string) error {
commands.InitURLib("", "")
cmds := make([]*discordgo.ApplicationCommand, 0, len(commands.Commands))
for _, cmd := range commands.Commands {
cmds = append(cmds, cmd.ApplicationCommand)
}
_, err := session.ApplicationCommandBulkOverwrite(AppID, GuildID, cmds)
return err
}
func remove(context.Context, []string) error {
cmds, err := session.ApplicationCommands(AppID, GuildID)
if err != nil {
return err
}
for _, cmd := range cmds {
if err := session.ApplicationCommandDelete(AppID, GuildID, cmd.ID); err != nil {
return err
}
}
return nil
}