-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathdiscord.go
97 lines (77 loc) · 2.93 KB
/
discord.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
92
93
94
95
96
97
package discord
import (
"context"
"fmt"
"github.com/bwmarrin/discordgo"
)
//go:generate mockery --name=discordSession --output=. --case=underscore --inpackage
type discordSession interface {
ChannelMessageSend(channelID string, content string, options ...discordgo.RequestOption) (*discordgo.Message, error)
}
// Compile-time check to ensure that discordgo.Session implements the discordSession interface.
var _ discordSession = new(discordgo.Session)
// Discord struct holds necessary data to communicate with the Discord API.
type Discord struct {
client discordSession
channelIDs []string
}
// New returns a new instance of a Discord notification service.
func New() *Discord {
return &Discord{
client: &discordgo.Session{},
channelIDs: []string{},
}
}
// authenticate will try and authenticate to discord.
func (d *Discord) authenticate(token string) error {
client, err := discordgo.New(token)
if err != nil {
return err
}
client.Identify.Intents = discordgo.IntentsGuildMessageTyping
d.client = client
return nil
}
// AuthenticateWithBotToken authenticates you as a bot to Discord via the given access token.
// For more info, see here: https://pkg.go.dev/github.com/bwmarrin/[email protected]#New
func (d *Discord) AuthenticateWithBotToken(token string) error {
token = parseBotToken(token)
return d.authenticate(token)
}
// AuthenticateWithOAuth2Token authenticates you to Discord via the given OAUTH2 token.
// For more info, see here: https://pkg.go.dev/github.com/bwmarrin/[email protected]#New
func (d *Discord) AuthenticateWithOAuth2Token(token string) error {
token = parseOAuthToken(token)
return d.authenticate(token)
}
// parseBotToken parses a regular token to a bot token that is understandable for discord.
// For more info, see here: https://pkg.go.dev/github.com/bwmarrin/[email protected]#New
func parseBotToken(token string) string {
return "Bot " + token
}
// parseBotToken parses a regular token to a OAUTH2 token that is understandable for discord.
// For more info, see here: https://pkg.go.dev/github.com/bwmarrin/[email protected]#New
func parseOAuthToken(token string) string {
return "Bearer " + token
}
// AddReceivers takes Discord channel IDs and adds them to the internal channel ID list. The Send method will send
// a given message to all those channels.
func (d *Discord) AddReceivers(channelIDs ...string) {
d.channelIDs = append(d.channelIDs, channelIDs...)
}
// Send takes a message subject and a message body and sends them to all previously set chats.
func (d Discord) Send(ctx context.Context, subject, message string) error {
fullMessage := subject + "\n" + message // Treating subject as message title
for _, channelID := range d.channelIDs {
select {
case <-ctx.Done():
return ctx.Err()
default:
_, err := d.client.ChannelMessageSend(channelID, fullMessage)
if err != nil {
return fmt.Errorf("send message to Discord channel %q: %w", channelID, err)
}
}
}
return nil
}