Skip to content

Commit

Permalink
first pass at a bot
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrea committed Oct 21, 2021
1 parent bfe6265 commit f370ed8
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
65 changes: 65 additions & 0 deletions cloudfunctions/bot/bot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package interactions

import (
"bytes"
"crypto/ed25519"
"encoding/json"
"net/http"
"os"

"github.com/bsdlp/discord-interactions-go/interactions"
log "github.com/sirupsen/logrus"
)

var (
discordPubkey, discordPubkeyPresent = os.LookupEnv("DISCORD_PUBLIC_KEY")
)

func init() {
if !discordPubkeyPresent {
log.Fatal("Discord public key is unset")
}
}

func Iteractions(w http.ResponseWriter, r *http.Request) {
verified := interactions.Verify(r, ed25519.PublicKey(discordPubkey))
if !verified {
http.Error(w, "signature mismatch", http.StatusUnauthorized)
return
}

defer r.Body.Close()
var data interactions.Data
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
// handle error
}

// respond to ping
if data.Type == interactions.Ping {
_, err := w.Write([]byte(`{"type":1}`))
if err != nil {
// handle error
}
return
}

// handle command
response := &interactions.InteractionResponse{
Type: interactions.ChannelMessage,
Data: &interactions.InteractionApplicationCommandCallbackData{
Content: "got your message kid",
},
}

var responsePayload bytes.Buffer
err = json.NewEncoder(&responsePayload).Encode(response)
if err != nil {
// handle error
}

_, err = http.Post(data.ResponseURL(), "application/json", &responsePayload)
if err != nil {
// handle err
}
}
7 changes: 7 additions & 0 deletions cloudfunctions/bot/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/iloveicedgreentea/valheim-gke-server/bot

go 1.14
require (
github.com/bsdlp/discord-interactions-go/interactions
)

49 changes: 49 additions & 0 deletions terraform/cloudfunctions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,53 @@ resource "google_cloudfunctions_function" "scaleup" {

trigger_http = true

}

########################
# Bot Function
########################

# Source code
data "archive_file" "bot" {
type = "zip"
source_dir = "${abspath("../")}/cloudfunctions/bot"
output_path = "/tmp/bot.zip"
}

# Archive file
resource "google_storage_bucket_object" "bot" {
name = "bot-${data.archive_file.scaleup.output_md5}.zip"
bucket = google_storage_bucket.bucket.name
source = data.archive_file.bot.output_path

metadata = {}

}
resource "google_cloudfunctions_function" "bot" {
depends_on = [
google_project_service.apis
]

name = "interactions"
description = "interactions"
runtime = "go116"

available_memory_mb = 128
source_archive_bucket = google_storage_bucket.bucket.name
source_archive_object = google_storage_bucket_object.bot.name
entry_point = "Interactions"

labels = {
function = "Interactions"
}

service_account_email = google_service_account.bot.email

environment_variables = {
DISCORD_PUBLIC_KEY = var.public_key

}

trigger_http = true

}
4 changes: 4 additions & 0 deletions terraform/iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ resource "google_service_account" "scaledown" {
account_id = "valheim-scaledown"
display_name = "SA to scaledown"
}
resource "google_service_account" "bot" {
account_id = "valheim-bot"
display_name = "SA for bot"
}
resource "google_project_iam_member" "scaleup" {
role = "roles/container.admin"
member = "serviceAccount:${google_service_account.scaleup.email}"
Expand Down
5 changes: 5 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,9 @@ variable "scaledown_function_schedule" {
type = string

default = "0 * * * *"
}

variable "public_key" {
description = "Discord bot public key"
type = string
}

0 comments on commit f370ed8

Please sign in to comment.