implement bot

This commit is contained in:
semblanceofsense 2025-01-31 18:58:07 -07:00
parent 40408f54ec
commit 1beb950e13
5 changed files with 174 additions and 24 deletions

8
go.mod
View File

@ -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
)

12
go.sum
View File

@ -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=

121
internal/bot/bot.go Normal file
View File

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

View File

@ -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
}

View File

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