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