Skip to content

Commit

Permalink
Add discord functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Jun 5, 2024
1 parent 05166fb commit f2b9093
Show file tree
Hide file tree
Showing 11 changed files with 812 additions and 600 deletions.
83 changes: 17 additions & 66 deletions app.go
Original file line number Diff line number Diff line change
@@ -1,88 +1,23 @@
package main

import (
"fmt"
"log"
"net/http"
"net/url"
"os"

"github.com/bwmarrin/discordgo"
"github.com/gorilla/mux"
"github.com/zond/diplicity/discord/api"
"github.com/zond/diplicity/discord/handlers"
"github.com/zond/diplicity/routes"
"github.com/zond/godip/variants"
"google.golang.org/appengine/v2"

. "github.com/zond/goaeoas"
)

var (
APPLICATION_ID = "1246942452791644281"
commands = []discordgo.ApplicationCommand{
handlers.CreateOrderCommand,
}
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"create-order": handlers.CreateOrderCommandHandler,
}
)

func main() {

discord, err := discordgo.New("Bot " + os.Getenv("DISCORD_BOT_TOKEN"))
if err != nil {
log.Fatal(err)
}

// Note these are dummy handlers for now. Will create a separate package
// for discord bot handlers which will do actually useful stuff.
discord.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
if m.Content == "ping" {
s.ChannelMessageSend(m.ChannelID, "pong")
}
if m.Content == "list variants" {
// iterate over variants and create a list of variant names
variantNames := ""
for _, variant := range variants.Variants {
variantNames += variant.Name + "\n"
}
s.ChannelMessageSend(m.ChannelID, variantNames)
}
})

discord.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.Type {
case discordgo.InteractionApplicationCommand:
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
}
})

cmdIDs := make(map[string]string, len(commands))

for _, cmd := range commands {
rcmd, err := discord.ApplicationCommandCreate(APPLICATION_ID, "", &cmd)
if err != nil {
log.Fatalf("Cannot create slash command %q: %v", cmd.Name, err)
}

cmdIDs[rcmd.ID] = rcmd.Name
}

discord.Identify.Intents = discordgo.IntentsAllWithoutPrivileged

err = discord.Open()
if err != nil {
log.Fatal(err)
}
defer discord.Close()

fmt.Println("Discord bot is now running!")

jsonFormURL, err := url.Parse("/js/jsonform.js")
if err != nil {
panic(err)
Expand All @@ -101,5 +36,21 @@ func main() {
router := mux.NewRouter()
routes.Setup(router)
http.Handle("/", router)

apiImpl := api.CreateApi()
session, err := discordgo.New("Bot " + os.Getenv("DISCORD_BOT_TOKEN"))
if err != nil {
log.Fatalf("Cannot create Discord session: %v", err)
}
handlers.RegisterHandlers(session, apiImpl)
log.Println("Discord initialization complete! Starting session...")

err = session.Open()
if err != nil {
log.Fatal(err)
}

defer session.Close()

appengine.Main()
}
165 changes: 165 additions & 0 deletions discord/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package api

// Api is a facade between the discord package and the backend

import (
"log"

"github.com/zond/diplicity/game"
)

type Province struct {
Name string
Key string
UnitType string
}

type OrderType struct {
Name string
Key string
}

type Api struct {
}

func (a *Api) SourceProvinces(userId, channelId string) ([]Province, error) {
return []Province{
{
Name: "Berlin",
Key: "berlin",
UnitType: "Army",
},
{
Name: "Kiel",
Key: "kiel",
UnitType: "Fleet",
},
{
Name: "Munich",
Key: "munich",
UnitType: "Army",
},
}, nil
}

func (a *Api) OrderTypes(userId, channelId, source string) ([]OrderType, error) {
if source == "" {
return []OrderType{}, nil
}
return []OrderType{
{
Name: "Hold",
Key: "hold",
},
{
Name: "Move",
Key: "move",
},
{
Name: "Support",
Key: "support",
},
{
Name: "Convoy",
Key: "convoy",
},
}, nil
}

func (a *Api) DestinationProvinces(userId, channelID, source, orderType string) ([]Province, error) {
if source == "" || orderType == "" {
return []Province{}, nil
}
return []Province{
{
Name: "Berlin",
Key: "berlin",
},
{
Name: "Kiel",
Key: "kiel",
},
{
Name: "Munich",
Key: "munich",
},
}, nil
}

func (a *Api) AuxProvinces(userId, channelId, source, orderType string) ([]Province, error) {
if source == "" || orderType == "" {
return []Province{}, nil
}
return []Province{
{
Name: "Berlin",
Key: "berlin",
},
{
Name: "Kiel",
Key: "kiel",
},
{
Name: "Munich",
Key: "munich",
},
}, nil
}

func (a *Api) AuxDestinationProvinces(userId, channelId, source, orderType, auxUnit string) ([]Province, error) {
if source == "" || orderType == "" || auxUnit == "" {
return []Province{}, nil
}
return []Province{
{
Name: "Berlin",
Key: "berlin",
},
{
Name: "Kiel",
Key: "kiel",
},
{
Name: "Munich",
Key: "munich",
},
}, nil
}

func (a *Api) CreateOrder(userId, channelId string) (*game.Order, error) {
gameId := "gameId" // TODO get game from channelId and get gameId from game
phaseOrdinal := "phaseOrdinal" // TODO get game from channelId and get phaseOrdinal from game
vars := map[string]string{
"game_id": gameId,
"phase_ordinal": phaseOrdinal,
}
request, err := CreateAuthenticatedRequest(userId, vars, "")
if err != nil {
return nil, err
}

return game.CreateOrder(nil, request)
}

func (a *Api) CreateGame(userId, channelId string) (*game.Game, error) {
log.Printf("api.CreateGame invoked\n")

newGame := NewGameDefaultValues
newGame.Desc = channelId // All new games are created with the channel ID as the name

log.Printf("Creating game with values: %+v\n", newGame)

vars := map[string]string{}

request, err := CreateAuthenticatedRequest(userId, vars, NewGameDefaultValues)
if err != nil {
return nil, err
}

log.Printf("Calling game.CreateGame with request: %+v\n", request)
return game.CreateGame(nil, request)
}

func CreateApi() *Api {
return &Api{}
}
Loading

0 comments on commit f2b9093

Please sign in to comment.