mirror of
https://github.com/Penguin-71630/meme-bot-frontend-dc.git
synced 2026-03-12 20:40:16 +08:00
Refactor: cleanup
- Introduce tracing - Introduce cobra / viper framework - Introduce resty client - Seperate files in api/ and bot/ - Trim unused functions
This commit is contained in:
129
bot/bot.go
129
bot/bot.go
@@ -1,85 +1,81 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/Penguin-71630/meme-bot-frontend-dc/api"
|
||||
"github.com/Penguin-71630/meme-bot-frontend-dc/config"
|
||||
"github.com/Penguin-71630/meme-bot-frontend-dc/tracing"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var 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,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "web",
|
||||
Description: "Get a login link to the web interface",
|
||||
},
|
||||
}
|
||||
|
||||
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) {
|
||||
func New() (*Bot, error) {
|
||||
// Create Discord session
|
||||
session, err := discordgo.New("Bot " + cfg.DiscordToken)
|
||||
session, err := discordgo.New("Bot " + viper.GetString("discord-bot-token"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating Discord session: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create API client
|
||||
apiClient := api.NewClient(cfg.APIBaseURL)
|
||||
|
||||
bot := &Bot{
|
||||
session: session,
|
||||
apiClient: apiClient,
|
||||
config: &Config{
|
||||
Token: cfg.DiscordToken,
|
||||
APIClient: apiClient,
|
||||
},
|
||||
apiClient: api.NewClient(),
|
||||
}
|
||||
|
||||
// Register handlers
|
||||
bot.registerHandlers()
|
||||
|
||||
// Set intents - only need guild messages for the ciallo listener
|
||||
session.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages | discordgo.IntentsMessageContent
|
||||
session.Identify.Intents = discordgo.IntentsGuildMessages |
|
||||
discordgo.IntentsDirectMessages |
|
||||
discordgo.IntentsMessageContent
|
||||
|
||||
return bot, nil
|
||||
}
|
||||
|
||||
func (b *Bot) registerSlashCommands(guildID string) 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,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "web",
|
||||
Description: "Get a login link to the web interface",
|
||||
},
|
||||
}
|
||||
|
||||
func (b *Bot) registerSlashCommands(ctx context.Context) error {
|
||||
for _, cmd := range commands {
|
||||
_, err := b.session.ApplicationCommandCreate(b.session.State.User.ID, guildID, cmd)
|
||||
_, err := b.session.ApplicationCommandCreate(
|
||||
b.session.State.User.ID, "", cmd)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create command %s: %w", cmd.Name, err)
|
||||
tracing.Logger.Ctx(ctx).
|
||||
Error("failed to create command",
|
||||
zap.String("command", cmd.Name),
|
||||
zap.Error(err))
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,8 +103,15 @@ func (b *Bot) registerHandlers() {
|
||||
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)
|
||||
func (b *Bot) onReady(
|
||||
s *discordgo.Session,
|
||||
event *discordgo.Ready,
|
||||
) {
|
||||
ctx := context.Background()
|
||||
tracing.Logger.Ctx(ctx).
|
||||
Info("logged in",
|
||||
zap.String("username", s.State.User.Username),
|
||||
zap.String("discriminator", s.State.User.Discriminator))
|
||||
|
||||
// For development: set your guild ID here for instant updates
|
||||
// For production: use "" for global commands
|
||||
@@ -120,18 +123,27 @@ func (b *Bot) onReady(s *discordgo.Session, event *discordgo.Ready) {
|
||||
}
|
||||
|
||||
// Register slash commands
|
||||
if err := b.registerSlashCommands(guildID); err != nil {
|
||||
log.Printf("Error registering slash commands: %v", err)
|
||||
if err := b.registerSlashCommands(ctx); err != nil {
|
||||
tracing.Logger.Ctx(ctx).
|
||||
Error("failed to register slash commands",
|
||||
zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
// Set bot status
|
||||
err := s.UpdateGameStatus(0, "/ping to check status")
|
||||
if err != nil {
|
||||
log.Printf("Error setting status: %v", err)
|
||||
tracing.Logger.Ctx(ctx).
|
||||
Error("failed to set status",
|
||||
zap.Error(err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) onInteractionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
func (b *Bot) onInteractionCreate(
|
||||
s *discordgo.Session,
|
||||
i *discordgo.InteractionCreate,
|
||||
) {
|
||||
if i.Type != discordgo.InteractionApplicationCommand {
|
||||
return
|
||||
}
|
||||
@@ -149,10 +161,7 @@ func (b *Bot) onInteractionCreate(s *discordgo.Session, i *discordgo.Interaction
|
||||
}
|
||||
|
||||
func (b *Bot) Start() error {
|
||||
if err := b.session.Open(); err != nil {
|
||||
return fmt.Errorf("error opening connection: %w", err)
|
||||
}
|
||||
return nil
|
||||
return b.session.Open()
|
||||
}
|
||||
|
||||
func (b *Bot) Stop() {
|
||||
|
||||
101
bot/commands.go
101
bot/commands.go
@@ -1,101 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
// Message listener for "ciallo"
|
||||
func (b *Bot) onMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
// Ignore messages from the bot itself
|
||||
if m.Author.ID == s.State.User.ID {
|
||||
return
|
||||
}
|
||||
|
||||
// Check if message is "ciallo" (case insensitive)
|
||||
if strings.ToLower(strings.TrimSpace(m.Content)) == "ciallo" {
|
||||
s.ChannelMessageSend(m.ChannelID, "Ciallo!")
|
||||
}
|
||||
}
|
||||
|
||||
// Slash command handlers
|
||||
func (b *Bot) handleSlashPing(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "pong",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (b *Bot) handleSlashGreet(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
var username string
|
||||
if i.Member != nil {
|
||||
username = i.Member.User.Username
|
||||
} else if i.User != nil {
|
||||
username = i.User.Username
|
||||
} else {
|
||||
username = "Unknown"
|
||||
}
|
||||
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: fmt.Sprintf("Ciallo, %s!", username),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (b *Bot) handleSlashEcho(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
options := i.ApplicationCommandData().Options
|
||||
if len(options) == 0 {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "No message provided!",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
message := options[0].StringValue()
|
||||
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: message,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (b *Bot) handleSlashWeb(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
var userID string
|
||||
if i.Member != nil {
|
||||
userID = i.Member.User.ID
|
||||
} else if i.User != nil {
|
||||
userID = i.User.ID
|
||||
}
|
||||
|
||||
// Call backend API
|
||||
loginURL, err := b.apiClient.GenerateLoginURL(userID)
|
||||
if err != nil {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "❌ Failed to generate login URL: " + err.Error(),
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: fmt.Sprintf("🔗 **Click here to access the web page:**\n%s\n\n", loginURL),
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
}
|
||||
28
bot/handleSlashEcho.go
Normal file
28
bot/handleSlashEcho.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package bot
|
||||
|
||||
import "github.com/bwmarrin/discordgo"
|
||||
|
||||
func (b *Bot) handleSlashEcho(
|
||||
s *discordgo.Session,
|
||||
i *discordgo.InteractionCreate,
|
||||
) {
|
||||
options := i.ApplicationCommandData().Options
|
||||
if len(options) == 0 {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "No message provided!",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
message := options[0].StringValue()
|
||||
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: message,
|
||||
},
|
||||
})
|
||||
}
|
||||
28
bot/handleSlashGreet.go
Normal file
28
bot/handleSlashGreet.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func (b *Bot) handleSlashGreet(
|
||||
s *discordgo.Session,
|
||||
i *discordgo.InteractionCreate,
|
||||
) {
|
||||
var username string
|
||||
if i.Member != nil {
|
||||
username = i.Member.User.Username
|
||||
} else if i.User != nil {
|
||||
username = i.User.Username
|
||||
} else {
|
||||
username = "Unknown"
|
||||
}
|
||||
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: fmt.Sprintf("Ciallo, %s!", username),
|
||||
},
|
||||
})
|
||||
}
|
||||
15
bot/handleSlashPing.go
Normal file
15
bot/handleSlashPing.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package bot
|
||||
|
||||
import "github.com/bwmarrin/discordgo"
|
||||
|
||||
func (b *Bot) handleSlashPing(
|
||||
s *discordgo.Session,
|
||||
i *discordgo.InteractionCreate,
|
||||
) {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "pong",
|
||||
},
|
||||
})
|
||||
}
|
||||
47
bot/handleSlashWeb.go
Normal file
47
bot/handleSlashWeb.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/Penguin-71630/meme-bot-frontend-dc/tracing"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func (b *Bot) handleSlashWeb(
|
||||
s *discordgo.Session,
|
||||
i *discordgo.InteractionCreate,
|
||||
) {
|
||||
var userID string
|
||||
if i.Member != nil {
|
||||
userID = i.Member.User.ID
|
||||
} else if i.User != nil {
|
||||
userID = i.User.ID
|
||||
}
|
||||
|
||||
// Call backend API
|
||||
loginURL, err := b.apiClient.PostGenLoginURL(userID)
|
||||
if err != nil {
|
||||
tracing.Logger.Ctx(context.Background()).
|
||||
Error("failed to generate login url",
|
||||
zap.Error(err))
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "❌ Failed to generate login URL",
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
content := "🔗 **Click here to access the web page:**\n"+
|
||||
loginURL + "\n\n"
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: content,
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
}
|
||||
23
bot/onMessageCreate.go
Normal file
23
bot/onMessageCreate.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
// Message listener for "ciallo"
|
||||
func (b *Bot) onMessageCreate(
|
||||
s *discordgo.Session,
|
||||
m *discordgo.MessageCreate,
|
||||
) {
|
||||
// Ignore messages from the bot itself
|
||||
if m.Author.ID == s.State.User.ID {
|
||||
return
|
||||
}
|
||||
|
||||
// Check if message is "ciallo" (case insensitive)
|
||||
if strings.ToLower(strings.TrimSpace(m.Content)) == "ciallo" {
|
||||
s.ChannelMessageSend(m.ChannelID, "Ciallo!")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user