Refactor: migrate discordbot
All checks were successful
All checks were successful
This commit is contained in:
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,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user