-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
210 lines (163 loc) · 5.47 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
)
const prefix = ":alvin"
var weatherCodes = map[int16]string{
0: "Unknown",
1000: "Clear",
1100: "Mostly Clear",
1101: "Partly Cloudy",
1102: "Mostly Cloudy",
1103: "Slightly Cloudy",
1001: "Cloudy",
2000: "Fog",
2100: "Light Fog",
4000: "Drizzle",
4001: "Rain",
4200: "Light Rain",
4201: "Heavy Rain",
5000: "Snow",
5001: "Flurries",
5100: "Light Snow",
5101: "Heavy Snow",
6000: "Freezing Drizzle",
6001: "Freezing Rain",
6200: "Light Freezing Rain",
6201: "Heavy Freezing Rain",
7000: "Ice Pellets",
7101: "Heavy Ice Pellets",
7102: "Light Ice Pellets",
8000: "Thunderstorm",
}
type WeatherData struct {
Data struct {
Time string
Values struct {
CloudBase float32
CloudCeiling float32
CloudCover int16
DewPoint float32
FreezingRainIntensity float32
Humidity int16
PrecipitationProbability int16
PressureSurfaceLevel float32
RainIntensity float32
SleetIntensity float32
SnowIntensity float32
Temperature float32
TemperatureApparent float32
UVHealthConcern int16
UVIndex int16
Visibility float32
WeatherCode int16
WindDirection float32
WindGust float32
WindSpeed float32
}
}
Location interface{}
}
func main() {
sess, err := discordgo.New("Bot ")
if err != nil {
log.Fatal(err)
}
sess.AddHandler(func(session *discordgo.Session, message *discordgo.MessageCreate) {
if message.Author.ID == session.State.User.ID {
return
}
args := strings.Split(message.Content, " ")
if args[0] != prefix {
return
}
switch instruction := args[1]; instruction {
case "hello":
session.ChannelMessageSend(message.ChannelID, "Hello "+message.Author.Username+", this is Alvin Beta. My days are numbered, I walk so other future Alvins can run.")
case "weather":
if len(args) <= 2 {
session.ChannelMessageSend(message.ChannelID, "You need to give me something to lookup. For example try sending ':alvin weather New York'.")
} else if len(args) >= 3 {
weatherCommand := args[2:]
city := strings.ToLower(strings.Join([]string(weatherCommand), "%20"))
url := "https://api.tomorrow.io/v4/weather/realtime?location=" + city + "&units=imperial&apikey="
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Add("accept", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
var weather WeatherData
if err := json.Unmarshal([]byte(body), &weather); err != nil {
log.Fatal(err)
}
weatherMessage := fmt.Sprintf("In your area, the temperature outside is **%.0f F** and it feels like **%.0f F**.\nCurrently you are experiencing **%v**", weather.Data.Values.Temperature, weather.Data.Values.TemperatureApparent, weatherCodes[weather.Data.Values.WeatherCode])
if weather.Data.Values.WeatherCode <= 1001 {
weatherMessage = weatherMessage + " conditions."
}
session.ChannelMessageSend(message.ChannelID, weatherMessage)
}
}
// if args[1] == "hello" {
// session.ChannelMessageSend(message.ChannelID, "Hello "+message.Author.Username+", this is Alvin Beta. My days are numbered, I walk so other future Alvins can run.")
// }
// if args[1] == "weather" {
// if len(args) <= 2 {
// session.ChannelMessageSend(message.ChannelID, "You need to give me something to lookup. For example try sending ':alvin weather New York'.")
// } else if len(args) >= 3 {
// weatherCommand := args[2:]
// city := strings.ToLower(strings.Join([]string(weatherCommand), "%20"))
// fmt.Println(city)
// url := "https://api.tomorrow.io/v4/weather/realtime?location=" + city + "&units=imperial&apikey=4SfUp3RmyXL4DnP4WP6yMBMzQN952jbh"
// req, err := http.NewRequest("GET", url, nil)
// if err != nil {
// log.Fatal(err)
// }
// req.Header.Add("accept", "application/json")
// res, err := http.DefaultClient.Do(req)
// if err != nil {
// log.Fatal(err)
// }
// defer res.Body.Close()
// body, _ := io.ReadAll(res.Body)
// var weather WeatherData
// if err := json.Unmarshal([]byte(body), &weather); err != nil {
// log.Fatal(err)
// }
// weatherMessage := fmt.Sprintf("In your area, the temperature outside is **%.0f F** and it feels like **%.0f F**.\nCurrently you are experiencing **%v**", weather.Data.Values.Temperature, weather.Data.Values.TemperatureApparent, weatherCodes[weather.Data.Values.WeatherCode])
// if weather.Data.Values.WeatherCode <= 1001 {
// weatherMessage = weatherMessage + " conditions."
// }
// session.ChannelMessageSend(message.ChannelID, weatherMessage)
// }
// }
})
sess.AddHandler(welcome)
sess.Identify.Intents = discordgo.IntentsAllWithoutPrivileged
err = sess.Open()
if err != nil {
log.Fatal(err)
}
defer sess.Close()
fmt.Println("Alvin is live!")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
}
func welcome(session *discordgo.Session, message *discordgo.GuildMemberAdd) {
session.ChannelMessageSend(message.GuildID, "Welcome Traveler! My name is Alvin")
}