/
bot.go
91 lines (73 loc) · 2.17 KB
/
bot.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
90
91
package bot
import (
"github.com/diamondburned/arikawa/v2/bot"
"github.com/diamondburned/arikawa/v2/discord"
"github.com/diamondburned/arikawa/v2/gateway"
"github.com/diamondburned/arikawa/v2/state"
"github.com/diamondburned/csufbot/internal/config"
"github.com/pkg/errors"
"golang.org/x/oauth2"
"github.com/diamondburned/csufbot/internal/bot/commands/admin"
)
var endpoint = oauth2.Endpoint{
AuthURL: "https://discordapp.com/api/oauth2/authorize",
TokenURL: "https://discordapp.com/api/oauth2/token",
AuthStyle: oauth2.AuthStyleInParams,
}
var oauthScopes = []string{
"identify", // for user ID
"guilds", // for list of guilds
"guilds.join", // for future use
}
type Discord struct {
*bot.Context
secret string
}
// Open opens a new Discord state connection.
func Open(cfg *config.Config) (*Discord, error) {
s, err := state.New("Bot " + cfg.Discord.Token)
if err != nil {
return nil, errors.Wrap(err, "failed to create state")
}
s.Gateway.Identifier.IdentifyData.Presence = &gateway.UpdateStatusData{
Activities: []discord.Activity{{
Name: "csuf!help | " + cfg.Site.FrontURL,
Type: discord.GameActivity,
}},
}
b, err := bot.New(s, &root{Config: cfg})
if err != nil {
return nil, errors.Wrap(err, "failed to create bot")
}
b.HasPrefix = bot.NewPrefix("csuf!")
b.AddIntents(b.DeriveIntents())
b.AddIntents(gateway.IntentGuilds)
b.AddIntents(gateway.IntentGuildMembers)
b.RegisterSubcommand(&admin.Admin{Config: cfg})
b.Name = "CSUFBot"
b.Description = "A Discord bot to help managing class servers."
// Bind a handler once forever.
b.Start()
if err := b.Open(); err != nil {
return nil, errors.Wrap(err, "failed to open Discord")
}
return &Discord{
Context: b,
secret: cfg.Discord.Secret,
}, nil
}
// OAuth returns a new OAuth config with the given redirection domain.
func (d *Discord) OAuth(frontURL string) oauth2.Config {
m, err := d.Me()
if err != nil {
return oauth2.Config{}
}
return oauth2.Config{
ClientID: m.ID.String(),
ClientSecret: d.secret,
Endpoint: endpoint,
RedirectURL: frontURL + "/oauth/redirect",
Scopes: oauthScopes,
}
}
func (d *Discord) Close() error { return d.CloseGracefully() }