save first version
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package discord
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/leolionad58/ticketbot/internal/config"
|
||||
"github.com/leolionad58/ticketbot/internal/db"
|
||||
)
|
||||
|
||||
type Bot struct {
|
||||
Session *discordgo.Session
|
||||
Config *config.Provider
|
||||
Tickets *db.TicketRepo
|
||||
Claims *db.ClaimMessageRepo
|
||||
GuildID string
|
||||
}
|
||||
|
||||
func New(token string, cfg *config.Provider, tickets *db.TicketRepo, claims *db.ClaimMessageRepo, guildID string) (*Bot, error) {
|
||||
s, err := discordgo.New("Bot " + token)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create session: %w", err)
|
||||
}
|
||||
s.Identify.Intents = discordgo.IntentsGuilds |
|
||||
discordgo.IntentsGuildMembers |
|
||||
discordgo.IntentsGuildMessages |
|
||||
discordgo.IntentsMessageContent
|
||||
|
||||
return &Bot{
|
||||
Session: s,
|
||||
Config: cfg,
|
||||
Tickets: tickets,
|
||||
Claims: claims,
|
||||
GuildID: guildID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (b *Bot) Open() error {
|
||||
return b.Session.Open()
|
||||
}
|
||||
|
||||
func (b *Bot) Close() {
|
||||
b.Session.Close()
|
||||
}
|
||||
|
||||
func (b *Bot) RegisterCommands(appID string) error {
|
||||
cmds := ApplicationCommands()
|
||||
for _, cmd := range cmds {
|
||||
_, err := b.Session.ApplicationCommandCreate(appID, b.GuildID, cmd)
|
||||
if err != nil {
|
||||
return fmt.Errorf("register %s: %w", cmd.Name, err)
|
||||
}
|
||||
slog.Info("registered command", "name", cmd.Name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user