mirror of
https://github.com/Penguin-71630/meme-bot-frontend-dc.git
synced 2026-03-12 20:40:16 +08:00
133 lines
2.9 KiB
Go
133 lines
2.9 KiB
Go
package bot
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/Penguin-71630/meme-bot-frontend-dc/api"
|
|
"github.com/Penguin-71630/meme-bot-frontend-dc/config"
|
|
"github.com/bwmarrin/discordgo"
|
|
)
|
|
|
|
type Bot struct {
|
|
session *discordgo.Session
|
|
config *Config
|
|
apiClient *api.Client
|
|
}
|
|
|
|
type Config struct {
|
|
Token string
|
|
APIClient *api.Client
|
|
}
|
|
|
|
func New(cfg *config.Config) (*Bot, error) {
|
|
// Create Discord session
|
|
session, err := discordgo.New("Bot " + cfg.DiscordToken)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error creating Discord session: %w", err)
|
|
}
|
|
|
|
// Create API client
|
|
apiClient := api.NewClient(cfg.APIBaseURL)
|
|
|
|
bot := &Bot{
|
|
session: session,
|
|
apiClient: apiClient,
|
|
config: &Config{
|
|
Token: cfg.DiscordToken,
|
|
APIClient: apiClient,
|
|
},
|
|
}
|
|
|
|
// Register handlers
|
|
bot.registerHandlers()
|
|
|
|
// Set intents - only need guild messages for the ciallo listener
|
|
session.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages | discordgo.IntentsMessageContent
|
|
|
|
return bot, nil
|
|
}
|
|
|
|
func (b *Bot) registerSlashCommands() error {
|
|
commands := []*discordgo.ApplicationCommand{
|
|
{
|
|
Name: "ping",
|
|
Description: "Check if bot is responsive",
|
|
},
|
|
{
|
|
Name: "greet",
|
|
Description: "Get a friendly greeting",
|
|
},
|
|
{
|
|
Name: "echo",
|
|
Description: "Bot repeats what you say",
|
|
Options: []*discordgo.ApplicationCommandOption{
|
|
{
|
|
Type: discordgo.ApplicationCommandOptionString,
|
|
Name: "message",
|
|
Description: "Message to echo",
|
|
Required: true,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, cmd := range commands {
|
|
_, err := b.session.ApplicationCommandCreate(b.session.State.User.ID, "", cmd)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot create command %s: %w", cmd.Name, err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *Bot) registerHandlers() {
|
|
b.session.AddHandler(b.onReady)
|
|
b.session.AddHandler(b.onMessageCreate)
|
|
b.session.AddHandler(b.onInteractionCreate)
|
|
}
|
|
|
|
func (b *Bot) onReady(s *discordgo.Session, event *discordgo.Ready) {
|
|
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
|
|
|
|
// Register slash commands
|
|
if err := b.registerSlashCommands(); err != nil {
|
|
log.Printf("Error registering slash commands: %v", err)
|
|
}
|
|
|
|
// Set bot status
|
|
err := s.UpdateGameStatus(0, "/ping to check status")
|
|
if err != nil {
|
|
log.Printf("Error setting status: %v", err)
|
|
}
|
|
}
|
|
|
|
func (b *Bot) onInteractionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
if i.Type != discordgo.InteractionApplicationCommand {
|
|
return
|
|
}
|
|
|
|
switch i.ApplicationCommandData().Name {
|
|
case "ping":
|
|
b.handleSlashPing(s, i)
|
|
case "greet":
|
|
b.handleSlashGreet(s, i)
|
|
case "echo":
|
|
b.handleSlashEcho(s, i)
|
|
}
|
|
}
|
|
|
|
func (b *Bot) Start() error {
|
|
if err := b.session.Open(); err != nil {
|
|
return fmt.Errorf("error opening connection: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (b *Bot) Stop() {
|
|
if b.session != nil {
|
|
b.session.Close()
|
|
}
|
|
}
|