Files
backend/bot/commands/web.go
Yi-Ting Shih 47f09b733a
All checks were successful
Go test / run-go-vet (push) Successful in 20s
Go test / check-swagger-up-to-date (push) Successful in 9s
Go test / run-go-test (push) Successful in 47s
Go test / cleanup-go-test (push) Successful in 4s
Go test / release-image (push) Successful in 3m35s
Refactor: migrate discordbot
2025-12-13 04:58:12 +08:00

69 lines
1.8 KiB
Go

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,
},
})
}
}