Refactor: migrate discordbot
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

This commit is contained in:
2025-12-13 02:03:01 +08:00
parent 0fc8f1f08c
commit 47f09b733a
23 changed files with 613 additions and 66 deletions

68
bot/commands/web.go Normal file
View 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,
},
})
}
}