2021-01-25 01:14:21 +02:00
|
|
|
package discord
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2021-01-25 06:05:28 +02:00
|
|
|
// Discord struct holds necessary data to communicate with the Discord API.
|
2021-01-25 01:14:21 +02:00
|
|
|
type Discord struct {
|
|
|
|
client *discordgo.Session
|
|
|
|
channelIDs []string
|
|
|
|
}
|
|
|
|
|
2021-01-25 07:08:57 +02:00
|
|
|
// 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(credentials ...string) error {
|
|
|
|
client, err := discordgo.New(credentials)
|
2021-01-25 01:14:21 +02:00
|
|
|
if err != nil {
|
2021-01-25 07:08:57 +02:00
|
|
|
return err
|
2021-01-25 01:14:21 +02:00
|
|
|
}
|
|
|
|
|
2021-01-26 10:22:55 +02:00
|
|
|
client.Identify.Intents = discordgo.IntentsGuildMessageTyping
|
|
|
|
|
2021-01-25 07:08:57 +02:00
|
|
|
d.client = client
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthenticateWithCredentials authenticates you to Discord via your email and password. Note that this
|
|
|
|
// is highly discouraged by Discord. Please use an authentication token.
|
|
|
|
// For more info, see here: https://pkg.go.dev/github.com/bwmarrin/discordgo@v0.22.1#New
|
|
|
|
func (d *Discord) AuthenticateWithCredentials(email, password string) error {
|
|
|
|
return d.authenticate(email, password)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthenticateWithCredentialsFull authenticates you to Discord via your email, password and access token.
|
|
|
|
// This is what discord recommends.
|
|
|
|
// For more info, see here: https://pkg.go.dev/github.com/bwmarrin/discordgo@v0.22.1#New
|
|
|
|
func (d *Discord) AuthenticateWithCredentialsFull(email, password, token string, isOAuthToken bool) error {
|
|
|
|
if isOAuthToken {
|
|
|
|
token = parseOAuthToken(token)
|
|
|
|
} else {
|
|
|
|
token = parseBotToken(token)
|
2021-01-25 01:14:21 +02:00
|
|
|
}
|
|
|
|
|
2021-01-25 07:08:57 +02:00
|
|
|
return d.authenticate(email, password, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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/discordgo@v0.22.1#New
|
|
|
|
func (d *Discord) AuthenticateWithBotToken(token string) error {
|
|
|
|
token = parseBotToken(token)
|
|
|
|
|
|
|
|
return d.authenticate(token)
|
|
|
|
}
|
|
|
|
|
2021-01-26 10:22:36 +02:00
|
|
|
// AuthenticateWithOAuth2Token authenticates you to Discord via the given OAUTH2 token.
|
2021-01-25 07:08:57 +02:00
|
|
|
// For more info, see here: https://pkg.go.dev/github.com/bwmarrin/discordgo@v0.22.1#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/discordgo@v0.22.1#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/discordgo@v0.22.1#New
|
|
|
|
func parseOAuthToken(token string) string {
|
|
|
|
return "Bearer " + token
|
2021-01-25 01:14:21 +02:00
|
|
|
}
|
|
|
|
|
2021-01-29 00:06:20 +02:00
|
|
|
// AddReceivers takes Discord channel IDs and adds them to the internal channel ID list. The Send method will send
|
2021-01-25 06:05:28 +02:00
|
|
|
// a given message to all those channels.
|
2021-01-25 01:14:21 +02:00
|
|
|
func (d *Discord) AddReceivers(channelIDs ...string) {
|
|
|
|
d.channelIDs = append(d.channelIDs, channelIDs...)
|
|
|
|
}
|
|
|
|
|
2021-01-25 06:05:28 +02:00
|
|
|
// Send takes a message subject and a message body and sends them to all previously set chats.
|
2021-01-25 01:14:21 +02:00
|
|
|
func (d Discord) Send(subject, message string) error {
|
|
|
|
fullMessage := subject + "\n" + message // Treating subject as message title
|
|
|
|
|
|
|
|
for _, channelID := range d.channelIDs {
|
|
|
|
_, err := d.client.ChannelMessageSend(channelID, fullMessage)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to send message to Discord channel '%s'", channelID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|