-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlambdas.go
118 lines (104 loc) · 3.07 KB
/
lambdas.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"context"
"fmt"
"hash/crc64"
"io"
"math/rand"
"net/http"
"net/url"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/go-redis/redis/v8"
)
func weatherLambda(ctx context.Context, rdb *redis.Client, topic *pubsubDiscordTopicAddr, m discordgo.Message) {
var (
weather string
err error
)
if strings.HasPrefix(m.Content, "!weather") {
var innerErr error
city := strings.TrimSpace(strings.TrimPrefix(m.Content, "!weather"))
weather, innerErr = getWeather(city)
if innerErr != nil {
logError(innerErr, "Error fetching weather data", city, "weatherLambda")
return
}
}
err = publishDiscordMessage(ctx, rdb, topic.getReplyTopic(), weather)
if err != nil {
logError(err, "Error publishing message", topic.getReplyTopic(), "weatherLambda")
return
}
}
func reactionsLambda(ctx context.Context, rdb *redis.Client, topic *pubsubDiscordTopicAddr, m discordgo.Message) {
var content string
switch m.Content {
case "ping":
content = "pong"
case "pong":
content = "ping"
case "!shrug":
content = "¯\\_(ツ)_/¯"
case "!lenny":
content = "( ͡° ͜ʖ ͡°)"
case "!tableflip":
content = "(╯°□°)╯︵ ┻━┻"
case "!tablefix":
content = "┬─┬ノ( º _ ºノ)"
case "!unflip":
content = "┬─┬ノ( º _ ºノ)"
case "hello", "hi", "hey", "howdy", "sup", "yo", "hiya", "anyong", "bonjour", "salut", "hallo", "moin":
content = sayHello(m.Author.Username)
case "!epeen":
content = epeen(m)
}
err := publishDiscordMessage(ctx, rdb, topic.getReplyTopic(), content)
if err != nil {
logError(err, "Error publishing message", topic.getReplyTopic(), "reactionsLambda")
}
}
func sayHello(username string) string {
greeting := []string{
"hi",
"hello",
"hey",
"howdy",
"sup",
"yo",
"hiya",
"anyong",
"bonjour",
"salut",
"hallo",
"moin",
}
return fmt.Sprintf("%s %s", greeting[rand.Intn(len(greeting))], username)
}
func epeen(m discordgo.Message) string {
peepeeSize := 20
peepeeCrc := crc64.Checksum([]byte(m.Author.Username+time.Now().Format("2006-01-02")), crc64.MakeTable(crc64.ECMA))
peepeeRnd := rand.New(rand.NewSource(int64(peepeeCrc)))
return "8" + strings.Repeat("=", peepeeRnd.Intn(peepeeSize)) + "D"
}
func getWeather(city string) (string, error) {
apiURL := fmt.Sprintf("https://wttr.in/%s?format=2", url.QueryEscape(city)) // format=2 is a preconfigured format for a single line of weather data. 3 and 4 include the city name with ugly + signs as delimiters
resp, err := http.Get(apiURL)
if err != nil {
logError(err, "Error fetching weather data", city, "getWeather")
return "", fmt.Errorf("failed to fetch weather data: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
logError(err, "Error reading response body", city, "getWeather")
return "", fmt.Errorf("failed to read response body: %v", err)
}
weather := string(body)
if resp.StatusCode != http.StatusOK {
logError(err, "Weather API returned an error", city, "getWeather")
return "", fmt.Errorf("weather API returned an error: %s", weather)
}
return weather, nil
}