From cc18c89cd43a3e1cf5f4d5a79a21fc7a8ab8b359 Mon Sep 17 00:00:00 2001 From: furtidev Date: Fri, 26 May 2023 17:50:43 +0600 Subject: [PATCH] added \!weather --- main.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/main.go b/main.go index c44ac75..c2a5a23 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,8 @@ import ( "syscall" "strings" "regexp" + "io" + "net/http" "github.com/bwmarrin/discordgo" "github.com/spf13/viper" @@ -73,6 +75,29 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { s.ChannelMessageSend(m.ChannelID, fmt.Sprintf(":whale2: :dash: %s", c[1])) } return + } else if strings.HasPrefix(m.Content, "!weather") { + c := strings.Fields(m.Content) + if len(c) == 2 { + // contact wttr.in + url := fmt.Sprintf("https://wttr.in/%s?0QAT", c[1]) + resp, err := http.Get(url) + if err != nil { + fmt.Printf("ERROR while running $weather: %s", err) + return + } + body, err := io.ReadAll(resp.Body) + if err != nil { + fmt.Printf("ERROR while running $weather: %s", err) + return + } + body_string := string(body[:]) + if strings.Contains(body_string, "404 UNKNOWN") { + s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("That's not a city, silly!")) + return + } + s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("```%s```", body_string)) + } + return } // check for reddit/twitter links and replace them with open source frontends