Refactor: migrate discordbot
All checks were successful
All checks were successful
This commit is contained in:
168
bot/bot.go
Normal file
168
bot/bot.go
Normal file
@@ -0,0 +1,168 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"gitea.konchin.com/go2025/backend/tracing"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/go-resty/resty/v2"
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type CommandHandler = func(
|
||||
*discordgo.Session, *discordgo.InteractionCreate)
|
||||
|
||||
type Command interface {
|
||||
ApplicationCommand() *discordgo.ApplicationCommand
|
||||
Handler() CommandHandler
|
||||
}
|
||||
|
||||
type Bot struct {
|
||||
session *discordgo.Session
|
||||
commands map[string]Command
|
||||
aliases map[string]int64
|
||||
|
||||
Client *resty.Client
|
||||
}
|
||||
|
||||
func New() (*Bot, error) {
|
||||
// Create Discord session
|
||||
session, err := discordgo.New(
|
||||
"Bot " + viper.GetString("discord-bot-token"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client := resty.New()
|
||||
client.SetBaseURL(viper.GetString("api-endpoint"))
|
||||
client.SetAuthToken(viper.GetString("preshared-key"))
|
||||
|
||||
bot := &Bot{
|
||||
session: session,
|
||||
commands: make(map[string]Command),
|
||||
|
||||
Client: client,
|
||||
}
|
||||
|
||||
bot.registerHandlers()
|
||||
|
||||
go bot.fetchAliases()
|
||||
|
||||
// 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(ctx context.Context) error {
|
||||
for _, cmd := range b.commands {
|
||||
_, err := b.session.ApplicationCommandCreate(
|
||||
b.session.State.User.ID, "", cmd.ApplicationCommand())
|
||||
if err != nil {
|
||||
tracing.Logger.Ctx(ctx).
|
||||
Error("failed to create command",
|
||||
zap.String("command", cmd.ApplicationCommand().Name),
|
||||
zap.Error(err))
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bot) clearSlashCommands(guildID string) error {
|
||||
commands, err := b.session.ApplicationCommands(b.session.State.User.ID, guildID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, cmd := range commands {
|
||||
err := b.session.ApplicationCommandDelete(b.session.State.User.ID, guildID, cmd.ID)
|
||||
if err != nil {
|
||||
log.Printf("Failed to delete command %s: %v", 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) RegisterCommand(newCommand func(*Bot) Command) {
|
||||
command := newCommand(b)
|
||||
b.commands[command.ApplicationCommand().Name] = command
|
||||
}
|
||||
|
||||
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
|
||||
guildID := "1377176828833169468" // Replace with your Discord server ID for faster testing
|
||||
|
||||
// clear slash commands
|
||||
if err := b.clearSlashCommands(guildID); err != nil {
|
||||
log.Printf("Error clearing slash commands: %v", err)
|
||||
}
|
||||
|
||||
// Register slash commands
|
||||
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 {
|
||||
tracing.Logger.Ctx(ctx).
|
||||
Error("failed to set status",
|
||||
zap.Error(err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) onInteractionCreate(
|
||||
s *discordgo.Session,
|
||||
i *discordgo.InteractionCreate,
|
||||
) {
|
||||
if i.Type != discordgo.InteractionApplicationCommand {
|
||||
return
|
||||
}
|
||||
|
||||
command, ok := b.commands[i.ApplicationCommandData().Name]
|
||||
if ok {
|
||||
tracing.Logger.Ctx(context.Background()).
|
||||
Info("run command",
|
||||
zap.String("name", i.ApplicationCommandData().Name))
|
||||
command.Handler()(s, i)
|
||||
} else {
|
||||
tracing.Logger.Ctx(context.Background()).
|
||||
Error("command not exist",
|
||||
zap.String("name", i.ApplicationCommandData().Name))
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) Start() error {
|
||||
return b.session.Open()
|
||||
}
|
||||
|
||||
func (b *Bot) Stop() {
|
||||
if b.session != nil {
|
||||
b.session.Close()
|
||||
}
|
||||
}
|
||||
53
bot/commands/echo.go
Normal file
53
bot/commands/echo.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"gitea.konchin.com/go2025/backend/bot"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
type EchoCommand struct {
|
||||
bot *bot.Bot
|
||||
}
|
||||
|
||||
func NewEchoCommand(bot *bot.Bot) bot.Command {
|
||||
return &EchoCommand{bot: bot}
|
||||
}
|
||||
|
||||
func (self *EchoCommand) ApplicationCommand() *discordgo.ApplicationCommand {
|
||||
return &discordgo.ApplicationCommand{
|
||||
Name: "echo",
|
||||
Description: "Bot repeats what you say",
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Name: "message",
|
||||
Description: "Message to echo",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (self *EchoCommand) Handler() bot.CommandHandler {
|
||||
return func(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,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
43
bot/commands/greet.go
Normal file
43
bot/commands/greet.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitea.konchin.com/go2025/backend/bot"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
type GreetCommand struct {
|
||||
bot *bot.Bot
|
||||
}
|
||||
|
||||
func NewGreetCommand(bot *bot.Bot) bot.Command {
|
||||
return &GreetCommand{bot: bot}
|
||||
}
|
||||
|
||||
func (self *GreetCommand) ApplicationCommand() *discordgo.ApplicationCommand {
|
||||
return &discordgo.ApplicationCommand{
|
||||
Name: "greet",
|
||||
Description: "Get a friendly greeting",
|
||||
}
|
||||
}
|
||||
|
||||
func (self *GreetCommand) Handler() bot.CommandHandler {
|
||||
return func(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),
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
32
bot/commands/ping.go
Normal file
32
bot/commands/ping.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"gitea.konchin.com/go2025/backend/bot"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
type PingCommand struct {
|
||||
bot *bot.Bot
|
||||
}
|
||||
|
||||
func NewPingCommand(bot *bot.Bot) bot.Command {
|
||||
return &PingCommand{bot: bot}
|
||||
}
|
||||
|
||||
func (self *PingCommand) ApplicationCommand() *discordgo.ApplicationCommand {
|
||||
return &discordgo.ApplicationCommand{
|
||||
Name: "ping",
|
||||
Description: "Check if bot is responsive",
|
||||
}
|
||||
}
|
||||
|
||||
func (self *PingCommand) Handler() bot.CommandHandler {
|
||||
return func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "pong",
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
68
bot/commands/web.go
Normal file
68
bot/commands/web.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"gitea.konchin.com/go2025/backend/bot"
|
||||
"gitea.konchin.com/go2025/backend/handlers/auth"
|
||||
"gitea.konchin.com/go2025/backend/tracing"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type WebCommand struct {
|
||||
bot *bot.Bot
|
||||
}
|
||||
|
||||
func NewWebCommand(bot *bot.Bot) bot.Command {
|
||||
return &WebCommand{bot: bot}
|
||||
}
|
||||
|
||||
func (self *WebCommand) ApplicationCommand() *discordgo.ApplicationCommand {
|
||||
return &discordgo.ApplicationCommand{
|
||||
Name: "web",
|
||||
Description: "Get a login link to the web interface",
|
||||
}
|
||||
}
|
||||
|
||||
func (self *WebCommand) Handler() bot.CommandHandler {
|
||||
return func(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
|
||||
var res auth.PostGenLoginUrlOutput
|
||||
resp, err := self.bot.Client.R().
|
||||
SetBody(auth.PostGenLoginUrlInput{UserId: userID}).
|
||||
SetResult(&res).
|
||||
Post("/bot/auth/gen-login-url")
|
||||
if err != nil || resp.StatusCode() != http.StatusOK {
|
||||
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"+
|
||||
res.LoginUrl + "\n\n"
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: content,
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
69
bot/onMessageCreate.go
Normal file
69
bot/onMessageCreate.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.konchin.com/go2025/backend/handlers/api"
|
||||
"gitea.konchin.com/go2025/backend/tracing"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/zap"
|
||||
"golang.org/x/exp/rand"
|
||||
)
|
||||
|
||||
func (b *Bot) fetchAliases() {
|
||||
for {
|
||||
var res []api.GetAliasesOutputAlias
|
||||
resp, err := b.Client.R().
|
||||
SetResult(&res).
|
||||
Get("/bot/api/aliases")
|
||||
if err == nil && resp.StatusCode() == http.StatusOK {
|
||||
aliases := make(map[string]int64)
|
||||
for _, alias := range res {
|
||||
aliases[alias.Name] = alias.Id
|
||||
}
|
||||
b.aliases = aliases
|
||||
|
||||
tracing.Logger.Ctx(context.Background()).
|
||||
Info("nmsl",
|
||||
zap.String("aliases", fmt.Sprintf("%+v", aliases)))
|
||||
}
|
||||
|
||||
time.Sleep(10 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
key := strings.ToLower(strings.TrimSpace(m.Content))
|
||||
tracing.Logger.Ctx(context.Background()).
|
||||
Info("wtf",
|
||||
zap.String("key", key))
|
||||
|
||||
if id, ok := b.aliases[key]; ok {
|
||||
var res []api.GetImagesOutputImage
|
||||
resp, err := b.Client.R().
|
||||
SetResult(&res).
|
||||
SetQueryParam("aliases", strconv.FormatInt(id, 10)).
|
||||
Get("/bot/api/images")
|
||||
if err == nil && resp.StatusCode() == http.StatusOK {
|
||||
image := res[rand.Intn(len(res))]
|
||||
s.ChannelMessageSend(m.ChannelID,
|
||||
fmt.Sprintf("%s/img/%d.%s",
|
||||
viper.GetString("external-url"),
|
||||
image.Id, image.Extension))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user