-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlmao.go
90 lines (74 loc) · 2.17 KB
/
lmao.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"context"
"errors"
"log"
"net/http"
"runtime/debug"
"strconv"
"time"
"github.com/aws/aws-lambda-go/lambda"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/sirupsen/logrus"
"github.com/uav-gaming/lmao/lmao"
)
// Constants
const BUILD_TIME_KEY string = "build_time"
// Globals
var (
// Variables populated by build script.
BuildTime string
// Variables populated by `init`s and other runtime functions.
BuildInfo map[string]interface{}
bot *lmao.LMAO
)
// Initialize build info
func init() {
BuildInfo = make(map[string]interface{})
// Get build info from binary.
info, ok := debug.ReadBuildInfo()
if !ok {
logrus.Fatal("Failed to read build info.")
}
for _, setting := range info.Settings {
BuildInfo[setting.Key] = setting.Value
}
// Parse and inject build time
build_epoch, err := strconv.ParseInt(BuildTime, 10, 64)
if err != nil {
log.Fatal("Failed to parse timestamp to int64: ", err)
}
BuildInfo[BUILD_TIME_KEY] = time.Unix(build_epoch, 0)
}
func HandleRequest(ctx context.Context, request lmao.Request) (lmao.Response, error) {
logrus.Infof("Received request %+v", request)
// Verify request signature.
if !bot.VerifyRequest(request) {
logrus.Warn("Request verification failed.")
return lmao.Response{
StatusCode: http.StatusUnauthorized,
}, nil
}
// Parse and handle request.
var event discord.InteractionEvent
if err := event.UnmarshalJSON([]byte(request.Body)); err != nil {
logrus.Error("Failed to unmarshal request body: ", request.Body)
return lmao.Response{}, errors.New("invalid request format")
}
response := bot.HandleInteraction(event)
return lmao.ToHttpResponse(response)
}
func main() {
// Setup logger.
logrus.SetFormatter(&logrus.JSONFormatter{
DisableTimestamp: true, // Logged by AWS.
})
logrus.SetReportCaller(true) // Log caller.
logrus.Info("Starting up with build info: ", BuildInfo)
var err error
bot, err = lmao.NewLMAO(lmao.GetenvMustStr("DISCORD_TOKEN"), lmao.GetenvMustEd25519PubKey("DISCORD_PUBLIC_KEY"), lmao.GetenvMustValidSnowflake[discord.AppID]("DISCORD_APPLICATION_ID"))
if err != nil {
logrus.Fatal("Failed to init bot: ", err)
}
lambda.Start(HandleRequest)
}