Skip to content

Commit

Permalink
Merge pull request #13 from MichaelFraser99/dev
Browse files Browse the repository at this point in the history
updated the readme to match the new structure
  • Loading branch information
MichaelFraser99 authored Jun 4, 2023
2 parents a3bfaae + 92ed139 commit a73bcc5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 26 deletions.
53 changes: 29 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,35 @@
# Discord Bot
This module provides the internal to run a discord bot on AWS lambda. The bot is written in go and uses the discord interactions API to receive and respond to commands.
This module provides the internals to run a discord bot on AWS lambda. The bot is written in go and uses the discord interactions API to receive and respond to commands.

## Requirements
- go 1.20+
- terraform 1.4.x+
- an AWS account

## Getting Started
todo

## Interactions Endpoint
This bot leverages the Interaction Endpoint feature of discord bots

https://discord.com/developers/docs/getting-started#adding-interaction-endpoint-url

The endpoint for this bot is your API Gateway endpoint + `/interactions`. For example, if your API Gateway endpoint is `https://example.com` then your interactions endpoint is `https://example.com/interactions`

## Configuring the bot
This project contains a basic configuration struct which is passed into the handler during the lambda init phase. This struct contains the following fields:
```go
type BotConfig struct {
PublicKey string
ApplicationCommandHandlers map[string]func(ctx context.Context, applicationCommand ApplicationCommand) (InteractionResponse, error)
}
```
## Getting Started
1. Run ```go get github.com/MichaelFraser99/serverless-discord-bot```
2. Create a main.go file with the following contents:

## Adding custom commands
Configuring your discord bot with your own commands is very simple. Below is an example 'main.go' file which registers a single command called 'poke'
```go
package main

import (
"context"
"github.com/MichaelFraser99/discord-bot/internal"
"github.com/MichaelFraser99/serverless-discord-bot/handler"
"github.com/MichaelFraser99/serverless-discord-bot/model"
"github.com/aws/aws-lambda-go/lambda"
"github.com/rs/zerolog/log"
"os"
)

var config internal.BotConfig
var config model.BotConfig

func main() {
publicKey, found := os.LookupEnv("PUBLIC_KEY")
Expand All @@ -47,13 +38,13 @@ func main() {
return
}

config = internal.BotConfig{
config = model.BotConfig{
PublicKey: publicKey,
ApplicationCommandHandlers: map[string]func(ctx context.Context, applicationCommand internal.ApplicationCommand) (internal.InteractionResponse, error){
"poke": func(ctx context.Context, applicationCommand internal.ApplicationCommand) (internal.InteractionResponse, error) {
return internal.InteractionResponse{
ApplicationCommandHandlers: map[string]func(ctx context.Context, applicationCommand model.ApplicationCommand) (*model.InteractionResponse, error){
"poke": func(ctx context.Context, applicationCommand model.ApplicationCommand) (*model.InteractionResponse, error) {
return &model.InteractionResponse{
Type: 4,
Data: internal.InteractionResponseData{
Data: model.InteractionResponseData{
Content: "Hello, world!",
TTS: false,
},
Expand All @@ -62,14 +53,28 @@ func main() {
},
}

lambda.Start(internal.NewHandler(config))
lambda.Start(handler.New(config))
}

```
The above code snippet registers a command `poke` and provides a function which will be executed on command run.

Therefor, when a user runs the command `/poke` the bot will respond with "Hello, world!".

## Configuring the bot
This module exposes a basic configuration struct which is passed into the handler during the lambda init phase. This struct contains the following fields:
```go
type BotConfig struct {
PublicKey string
ApplicationCommandHandlers map[string]func(ctx context.Context, applicationCommand ApplicationCommand) (InteractionResponse, error)
}
```

## Deploying the bot
I'd recommend leveraging terraform to deploy the bot
A supporting terraform provider for configuring discord application commands can be found here: https://registry.terraform.io/providers/MichaelFraser99/discord-application/latest
A supporting terraform provider for configuring discord application commands can be found here: https://registry.terraform.io/providers/MichaelFraser99/discord-application/latest

This project packages a `./getting-started` directory which contains all you need to get started with deploying your bot

This directory contains an example swagger file to populate an API Gateway endpoint with the required routes to support the discord interactions API. This swagger file can be found at `./getting-started/api/swagger.yaml`

Finally, the `./getting-started/infrastructure` directory contains a terraform module which can be used to deploy the bot
9 changes: 8 additions & 1 deletion getting-started/infrastructure/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,11 @@ resource "aws_iam_policy" "lambda_logging" {
resource "aws_iam_role_policy_attachment" "lambda_logs" {
role = aws_iam_role.bot_lambda_role.name
policy_arn = aws_iam_policy.lambda_logging.arn
}
}

resource "discord-application_command" "poke" {
application_id = var.application_id
name = "poke"
description = "a poke to the application"
type = 1
}
10 changes: 10 additions & 0 deletions getting-started/infrastructure/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@ terraform {
source = "hashicorp/aws"
version = "~> 5.0"
}

discord-application = {
source = "MichaelFraser99/discord-application"
version = "0.1.2"
}
}
}

# Configure the AWS Provider
provider "aws" {
region = "eu-west-2"
}

# Configure the Discord Application Provider
provider "discord-application" {
token = "*your token here*"
}
5 changes: 5 additions & 0 deletions getting-started/infrastructure/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ variable "stage_name" {
variable "public_key" {
type = string
description = "The discord bots public API key"
}

variable "application_id" {
type = string
description = "The discord bots application id"
}
3 changes: 2 additions & 1 deletion getting-started/infrastructure/vars.tfvars
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
name = ""
public_key = ""
public_key = ""
application_id = ""

0 comments on commit a73bcc5

Please sign in to comment.