From 1beb950e13efaeb4cb5dfd48cec9863252957fb0 Mon Sep 17 00:00:00 2001 From: semblanceofsense Date: Fri, 31 Jan 2025 18:58:07 -0700 Subject: [PATCH] implement bot --- go.mod | 8 ++- go.sum | 12 ++++ internal/bot/bot.go | 121 ++++++++++++++++++++++++++++++++++++ internal/getmaze/getmaze.go | 26 +++++++- mazesolver.go | 31 +++------ 5 files changed, 174 insertions(+), 24 deletions(-) create mode 100644 internal/bot/bot.go diff --git a/go.mod b/go.mod index a6fdedd..3b66c3b 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,10 @@ module mazesolver go 1.23.5 -require golang.org/x/image v0.23.0 // indirect +require ( + github.com/bwmarrin/discordgo v0.28.1 // indirect + github.com/gorilla/websocket v1.4.2 // indirect + golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect + golang.org/x/image v0.23.0 // indirect + golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect +) diff --git a/go.sum b/go.sum index 8ec2c9e..66ceb9a 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,14 @@ +github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4= +github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/image v0.23.0 h1:HseQ7c2OpPKTPVzNjG5fwJsOTCiiwS4QdsYi5XU6H68= golang.org/x/image v0.23.0/go.mod h1:wJJBTdLfCCf3tiHa1fNxpZmUI4mmoZvwMCPP0ddoNKY= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/internal/bot/bot.go b/internal/bot/bot.go new file mode 100644 index 0000000..43e14f5 --- /dev/null +++ b/internal/bot/bot.go @@ -0,0 +1,121 @@ +package bot + +import ( + "fmt" + "log" + getMaze "mazesolver/internal/getmaze" + "mazesolver/internal/outputmaze" + "mazesolver/internal/solvemaze" + "os" + "os/signal" + + "github.com/bwmarrin/discordgo" +) + +var ( + appId = "1335044960898252830" + guildId = "" +) + +func Run(BotToken string) { + discord, err := discordgo.New(("Bot " + BotToken)) + if err != nil { fmt.Println("Bot 1"); log.Fatal(err) } + + _, err = discord.ApplicationCommandBulkOverwrite(appId, guildId, []*discordgo.ApplicationCommand { + { + Name: "solve-maze", + Description: "solve one of tilley's mazes", + Options: []*discordgo.ApplicationCommandOption { + { + Type: discordgo.ApplicationCommandOptionString, + Name: "image-link", + Description: "link to maze image", + Required: true, + }, + }, + }, + }, + ) + if err != nil { fmt.Println("Bot 2"); log.Fatal(err) } + + discord.AddHandler(func ( + s *discordgo.Session, + i *discordgo.InteractionCreate, + ) { + if i.Type == discordgo.InteractionApplicationCommand { + data := i.ApplicationCommandData() + responseData := "" + if i.Interaction.Member.User.ID == s.State.User.ID { return; } + + switch data.Name { + case "solve-maze": + maze, err := getMaze.GetMaze(i.ApplicationCommandData().Options[0].Value.(string)) + if err != nil { + responseData = "You must provide an image! Select the maze, click \"open in browser\", and copy the link of the image" + } else { + if (len(maze) < 1) { + responseData = "You must provide an image! Select the maze, click \"open in browser\", and copy the link of the image" + } else { + points := solvemaze.FindPath(maze) + if (len(points) < 1) { + responseData = "You must provide an image! Select the maze, click \"open in browser\", and copy the link of the image" + } else { + outputmaze.EditMaze(points, "/tmp/maze.png", "/tmp/outputmaze.png") + }}}} + + if responseData != "" { + err = s.InteractionRespond( + i.Interaction, + &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Flags: 1 << 6, + Content: responseData, + }, + }, + ) + } + fileName := "/tmp/outputmaze.png" + f, err := os.Open(fileName) + defer f.Close() + err = s.InteractionRespond( + i.Interaction, + &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Files: []*discordgo.File{ + &discordgo.File{ + Name: fileName, + Reader: f, + }, + }, + }, + }, + ) + if err != nil { + fmt.Println(err) + } + } + }) + + discord.AddHandler( func( + s *discordgo.Session, + m *discordgo.MessageCreate, + ) { + listOfGuilds := discord.State.Guilds + for _, v := range listOfGuilds { + fmt.Println(v.Name) + } + }) + + err = discord.Open() + if err != nil { log.Fatal(err) } + + stop := make (chan os.Signal, 1) + signal.Notify(stop, os.Interrupt) + log.Println("Press Ctrl+C to Exit") + <-stop + + err = discord.Close() + if err != nil { log.Fatal(err) } +} diff --git a/internal/getmaze/getmaze.go b/internal/getmaze/getmaze.go index 4f5622d..25fea63 100644 --- a/internal/getmaze/getmaze.go +++ b/internal/getmaze/getmaze.go @@ -5,6 +5,8 @@ import ( "fmt" "image/color" "image/png" + "io" + "net/http" "os" ) @@ -27,13 +29,33 @@ type Maze [][]Point func GetMaze(imagepath string) (Maze, error) { returnMaze := *new(Maze) - imagereader, err := os.Open(imagepath) + res, err := http.Get(imagepath) + if err != nil { - return *new(Maze), err + return returnMaze, err + } + + data, err := io.ReadAll(res.Body) + + if err != nil { + return returnMaze, err + } + + defer res.Body.Close() + + err = os.WriteFile("/tmp/maze.png", data, 0755) + if err != nil { + return returnMaze, err + } + + imagereader, err := os.Open("/tmp/maze.png") + if err != nil { + return returnMaze, err } image, err := png.Decode(imagereader) if err != nil { + fmt.Println("Here") return *new(Maze), err } diff --git a/mazesolver.go b/mazesolver.go index 5bcefe6..c2b65b9 100644 --- a/mazesolver.go +++ b/mazesolver.go @@ -1,29 +1,18 @@ package main import ( - "fmt" - "log" - getMaze "mazesolver/internal/getmaze" - "mazesolver/internal/outputmaze" - "mazesolver/internal/solvemaze" + "flag" + "mazesolver/internal/bot" ) -func main() { - path := "/tmp/maze.png" - maze, err := getMaze.GetMaze(path) - if err != nil { - log.Fatal(err) - } +var BotToken string - p := solvemaze.FindPath(maze) - if err != nil { - log.Fatal(err) - } +func init() { + flag.StringVar(&BotToken, "bottoken", "", "discord bot token") - newpath := "/tmp/outputmaze.png" - _, err = outputmaze.EditMaze(p, path, newpath) - if err != nil { - log.Fatal(err) - } - fmt.Println(newpath) + flag.Parse() +} + +func main() { + bot.Run(BotToken) }