Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Discord bot and initial commands #190

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Dockerfile → .docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ ENV PATH $PATH:/usr/local/gcloud/google-cloud-sdk/bin
WORKDIR /go/src/app

# Copy the current directory contents into the container at /go/src/app
COPY . .

# Remove Dockerfile to prevent issue with App Engine
RUN rm Dockerfile
COPY ../ .

# Expose port 8080 to the outside world
EXPOSE 8080
Expand Down
18 changes: 18 additions & 0 deletions .docker/docker-build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Description: Build docker image from Dockerfile

Write-Host "Building Docker image..."

# Confirm that the script is being executed from the correct directory
if (-not (Test-Path ".docker/docker-run.ps1")) {
Write-Host "You must run this command from the parent directory of this script."
exit
}

Write-Host "Confirmed that script is being executed from the correct directory."

$dockerCommand = "docker build --tag 'diplicity' ./.docker"

Write-Host "Running Docker command: $dockerCommand"

# Execute the Docker command
Invoke-Expression $dockerCommand
19 changes: 19 additions & 0 deletions .docker/docker-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# Description: Build docker image from Dockerfile
echo "Building Docker image..."

# Confirm that the script is being executed from the correct directory
if [ ! -f ".docker/docker-run.sh" ]; then
echo "You must run this command from the parent directory of this script."
exit 1
fi

echo "Confirmed that script is being executed from the correct directory."

dockerCommand="docker build --tag 'diplicity' ./.docker"

echo "Running Docker command: $dockerCommand"

# Execute the Docker command
eval $dockerCommand
10 changes: 10 additions & 0 deletions .docker/docker-create-network.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Description: Create a new docker network called my-net

Write-Host "Creating Docker network..."

$dockerCommand = "docker network create -d bridge my-net"

Write-Host "Running Docker command: $dockerCommand"

# Execute the Docker command
Invoke-Expression $dockerCommand
11 changes: 11 additions & 0 deletions .docker/docker-create-network.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

# Description: Create a new docker network called my-net
echo "Creating Docker network..."

dockerCommand="docker network create -d bridge my-net"

echo "Running Docker command: $dockerCommand"

# Execute the Docker command
eval $dockerCommand
47 changes: 47 additions & 0 deletions .docker/docker-run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Ensure that this command is being called from the parent directory
# of the Dockerfile. This is necessary for the volume mapping to work
# correctly.

Write-Host "Running Docker container..."

# Confirm that the script is being executed from the correct directory
if (-not (Test-Path ".docker/docker-run.ps1")) {
Write-Host "You must run this command from the parent directory of this script."
exit
}

Write-Host "Confirmed that script is being executed from the correct directory."

# Base docker run command
$dockerCommand = "docker run"

# Mount a volume from the host machine to the container. This means
# that the container will have access to the files in the host machine
# and that changes made in the host machine will be reflected in the
# container.
$dockerCommand += " -v .:/go/src/app:ro" # Add volume mapping

# Specify the network that the container should be connected to. This
# is necessary for the Discord bot to be able to work correctly.
$dockerCommand += " --network my-net" # Specify the network

# Specify the environment file that should be used by the container. This
# file contains secret environment variables that the application needs
# to run correctly, e.g. DISCORD_BOT_TOKEN.
$dockerCommand += " --env-file ./.env" # Specify the environment file

# Specify that the 8080 port should be exposed to the host machine. This
# is the port that the API application listens on.
$dockerCommand += " -p 8080:8080" # Map port 8080

# Specify that the 8000 port should be exposed to the host machine. This
# is the port that the admin application listens on.
$dockerCommand += " -p 8000:8000" # Map port 8000

# Specify the image that should be used to create the container. This
$dockerCommand += " diplicity"

Write-Host "Running Docker command: $dockerCommand"

# Execute the Docker command
Invoke-Expression $dockerCommand
49 changes: 49 additions & 0 deletions .docker/docker-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

# Ensure that this command is being called from the parent directory
# of the Dockerfile. This is necessary for the volume mapping to work
# correctly.

echo "Running Docker container..."

# Confirm that the script is being executed from the correct directory
if [ ! -f ".docker/docker-run.sh" ]; then
echo "You must run this command from the parent directory of this script."
exit 1
fi

echo "Confirmed that script is being executed from the correct directory."

# Base docker run command
dockerCommand="docker run"

# Mount a volume from the host machine to the container. This means
# that the container will have access to the files in the host machine
# and that changes made in the host machine will be reflected in the
# container.
dockerCommand+=" -v .:/go/src/app:ro" # Add volume mapping

# Specify the network that the container should be connected to. This
# is necessary for the Discord bot to be able to work correctly.
dockerCommand+=" --network my-net" # Specify the network

# Specify the environment file that should be used by the container. This
# file contains secret environment variables that the application needs
# to run correctly, e.g. DISCORD_BOT_TOKEN.
dockerCommand+=" --env-file ./.env" # Specify the environment file

# Specify that the 8080 port should be exposed to the host machine. This
# is the port that the API application listens on.
dockerCommand+=" -p 8080:8080" # Map port 8080

# Specify that the 8000 port should be exposed to the host machine. This
# is the port that the admin application listens on.
dockerCommand+=" -p 8000:8000" # Map port 8000

# Specify the image that should be used to create the container.
dockerCommand+=" diplicity"

echo "Running Docker command: $dockerCommand"

# Execute the Docker command
eval $dockerCommand
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ game/game.debug

# Diff tool files
*.orig

.env
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ To enable debugging the JSON output in a browser, adding the query parameter `ac

## Running locally using Docker (recommended)

- **Note** you need to create a `.env` file in the root directory of the repo an
add `DISCORD_BOT_TOKEN` value, e.g. `DISCORD_BOT_TOKEN=abc123` (no quote marks).
Create a Discord app and get a token if you want to test Discord locally.
- Download Docker
- Navigate to the root directory of this project
- Run `docker build --tag 'diplicity' .`
- Run `docker run -p 8080:8080 -p 8000:8000 diplicity`
- Use `ps1` files for Windows and `sh` files for UNIX
- Run `.\.docker\docker-build.ps1` **or** `.\.docker\docker-build.sh` (only required once)
- Run `.\.docker\docker-network.ps1` **or** `.\.docker\docker-network.sh` (only required once)
- Run `.\.docker\docker-run.ps1` **or** `.\.docker\docker-run.sh`
- The API is now available on your machine at `localhost:8080`
- The Admin server is now available on your machine at `localhost:8000`

Expand Down
22 changes: 22 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package main

import (
"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"
"google.golang.org/appengine/v2"

. "github.com/zond/goaeoas"
)

func main() {

jsonFormURL, err := url.Parse("/js/jsonform.js")
if err != nil {
panic(err)
Expand All @@ -30,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"))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zond I'll need to add a DISCORD_BOT_TOKEN value to GitHub secrets or something. Can you help me with that?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently all the secrets needed are stored in App Engine Datastore, see for example https://github.com/zond/diplicity/blob/master/auth/sendgrid.go#L50 where it fetches a Sendgrid key used to send emails via Sendgrid (which may not be functional anymore, people have reported that email deliveries don't work).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When running locally, I typically have just uploaded the secret using e.g. https://github.com/zond/diplicity/blob/master/game/handler.go#L664.

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()
}
Loading
Loading