bingobot/internal/discord/discord.go
Ava Affine 430c0afaa6 Confessions module
This commit adds a confessions feature that allows users to mark a
"confessional" channel and also to post anonymously to it. The changes
that this comprises of are as follows:

- New discord "slash" commands for both marking a confessional and
  posting to it
- a bunch of stuff in the discord module to register and deregister
  "slash" commands
- New event type to track marked confessionals
- confession module that processes new confession channel links
  and also posts confessions to corresponding confessionals

Not included in this commit:
- a way to cleanup obsolete or reconfigured confession channel links
- access control for the confessional slash commands

Signed-off-by: Ava Affine <ava@sunnypup.io>
2025-01-08 13:40:11 -08:00

67 lines
1.2 KiB
Go

package discord
import (
"errors"
"fmt"
"github.com/bwmarrin/discordgo"
"gitlab.com/whom/bingobot/internal/logging"
)
type Session struct {
connected bool
s *discordgo.Session
}
var (
session *Session
ErrNotConnected = errors.New("session not connected")
)
func Connect(token string) error {
// throw away err because discordgo.New() actually can't return an error.
// https://github.com/bwmarrin/discordgo/blob/da9e191069d09e1b467145f5758d9b11cb9cca0d/discord.go#L32
s, _ := discordgo.New("Bot " + token)
session = &Session{
s: s,
}
addHandlers()
err := session.s.Open()
if err != nil {
return fmt.Errorf("failed to open discord session: %s", err)
}
registerCommands(session.s)
return nil
}
func Close() {
deregisterCommands(session.s)
err := session.s.Close()
if err != nil {
// don't bubble up this error because we can't do anything about it and
// the bot is exiting anyway.
logging.Error("could not close discord session gracefully", "type", "error", "error", err)
}
session.connected = false
}
func Connected() bool {
return session != nil && session.connected
}
func User(uid string) (*discordgo.User, error) {
if !Connected() {
return nil, ErrNotConnected
}
return session.s.User(uid)
}