Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Lawton committed Sep 26, 2024
1 parent c075884 commit 89e30a1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
59 changes: 40 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package main

import (
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -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.")

Expand Down Expand Up @@ -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
}
Expand All @@ -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))
Expand Down

0 comments on commit 89e30a1

Please sign in to comment.