This repository has been archived by the owner on Aug 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (64 loc) · 1.61 KB
/
main.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
package main
import (
"time"
"github.com/aws/aws-lambda-go/lambda"
"github.com/bwmarrin/discordgo"
"gitlab.com/Cacophony/dhelpers"
"gitlab.com/Cacophony/dhelpers/cache"
"gitlab.com/Cacophony/dhelpers/components"
)
var (
serviceName = "lambda/pong"
)
func init() {
// init components
components.InitLogger(serviceName)
err := components.InitSentry()
dhelpers.CheckErr(err)
}
// Handler is the lambda entry point when event is triggered
// error handling is built in, just panic (dhelpers.CheckErr)
func Handler(event dhelpers.EventContainer) {
// pass on event to the correct method
switch event.Type {
case dhelpers.MessageCreateEventType:
for _, destination := range event.Destinations {
switch destination.Alias {
case "ping":
ping(event)
return
}
}
}
}
// ping is triggered when we receive a ping command
func ping(container dhelpers.EventContainer) {
// measure time
pingStart := time.Now()
defer func() {
cache.GetLogger().Infoln("ping took", time.Since(pingStart).String())
}()
// send ping response
_, err := container.SendComplex(container.MessageCreate.ChannelID, &discordgo.MessageSend{
Embed: &discordgo.MessageEmbed{
Title: "Pong!",
Fields: []*discordgo.MessageEmbedField{
{
Name: "Gateway => Lambda",
Value: pingStart.Sub(container.ReceivedAt).String(),
Inline: false,
},
{
Name: "Gateway Uptime",
Value: time.Since(container.GatewayStarted).String(),
Inline: false,
},
},
},
})
dhelpers.CheckErr(err)
}
func main() {
// register handler
lambda.StartHandler(dhelpers.NewLambdaHandler(serviceName, Handler))
}