diff --git a/docker-compose.yml b/docker-compose.yml index 67bd069..ebcebdb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,6 +5,6 @@ services: build: . container_name: ip-monitor restart: unless-stopped - environment: - - DISCORD_BOT_TOKEN=${DISCORD_BOT_TOKEN} - - DISCORD_CHANNEL_ID=${DISCORD_CHANNEL_ID} + env_file: + - .env + diff --git a/go.mod b/go.mod index d851b03..ea3fe19 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,6 @@ require ( require ( github.com/gorilla/websocket v1.5.3 // indirect - golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect - golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect + golang.org/x/crypto v0.27.0 // indirect + golang.org/x/sys v0.25.0 // indirect ) diff --git a/main.go b/main.go index f7eefe6..8bf1630 100644 --- a/main.go +++ b/main.go @@ -2,11 +2,12 @@ package main import ( "fmt" - "io/ioutil" + "io" "log" "net/http" "os" "os/signal" + "strings" "syscall" "time" @@ -49,7 +50,12 @@ func main() { if err != nil { log.Fatalf("Error opening connection to Discord: %v", err) } - defer dg.Close() + defer func(dg *discordgo.Session) { + err := dg.Close() + if err != nil { + log.Fatalf("Error closing connection to Discord: %v", err) + } + }(dg) log.Println("Bot is now running. Press CTRL+C to exit.") @@ -90,9 +96,14 @@ func getExternalIP() (string, error) { if err != nil { return "", err } - defer resp.Body.Close() + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + log.Printf("Error closing connection to IP Checker: %v", err) + } + }(resp.Body) - ip, err := ioutil.ReadAll(resp.Body) + ip, err := io.ReadAll(resp.Body) if err != nil { return "", err } @@ -109,24 +120,34 @@ func updateDiscordMessage(dg *discordgo.Session, channelID, currentIP string) er var pinnedMessageID string if len(pinnedMessages) > 0 { - // Assume the first pinned message is the one to update - pinnedMessageID = pinnedMessages[0].ID - _, err = dg.ChannelMessageEdit(channelID, pinnedMessageID, fmt.Sprintf("Current IP Address: `%s`", currentIP)) - if err != nil { - return fmt.Errorf("error editing pinned message: %w", err) - } - } else { - // Send a new message and pin it - msg, err := dg.ChannelMessageSend(channelID, fmt.Sprintf("Current IP Address: `%s`", currentIP)) - if err != nil { - return fmt.Errorf("error sending message: %w", err) - } + for _, message := range pinnedMessages { + // Assume the first pinned message is the one to update + if strings.Contains(message.Content, "Current IP Address:") { + pinnedMessageID = message.ID + _, err = dg.ChannelMessageEdit(channelID, pinnedMessageID, fmt.Sprintf("Current IP Address: `%s`", currentIP)) + if err != nil { + return fmt.Errorf("error editing pinned message: %w", err) + } + // Send a broadcast message + _, err = dg.ChannelMessageSend(channelID, fmt.Sprintf("IP Address has changed to `%s`", currentIP)) + if err != nil { + return fmt.Errorf("error sending broadcast message: %w", err) + } - err = dg.ChannelMessagePin(channelID, msg.ID) - if err != nil { - return fmt.Errorf("error pinning message: %w", err) + return nil + } } } + // Send a new message and pin it + msg, err := dg.ChannelMessageSend(channelID, fmt.Sprintf("Current IP Address: `%s`", currentIP)) + if err != nil { + return fmt.Errorf("error sending message: %w", err) + } + + err = dg.ChannelMessagePin(channelID, msg.ID) + if err != nil { + return fmt.Errorf("error pinning message: %w", err) + } // Send a broadcast message _, err = dg.ChannelMessageSend(channelID, fmt.Sprintf("IP Address has changed to `%s`", currentIP))