This Go application monitors your external IP address and updates a pinned message in a specified Discord channel whenever the IP changes. It also sends a broadcast message notifying about the IP change.
- Features
- Prerequisites
- Setup Instructions
- Environment Variables
- Stopping the Bot
- Updating the Bot
- Additional Considerations
- Troubleshooting
- Contributing
- License
- Periodic IP Monitoring: Checks your external IP address at a configurable interval.
- Discord Integration: Updates a pinned message in a specified Discord channel when the IP changes.
- Broadcast Notifications: Sends a message to notify channel members of IP changes.
- Environment Configuration: Uses a
.env
file for configuration, keeping sensitive data secure. - Dockerized: Easily build and run the application within a Docker container.
- Go: (Optional for local development) Install Go if you plan to build and run the application locally.
- Docker: Ensure Docker is installed on your system.
- Discord Account: You need a Discord account to create a bot and add it to your server.
- Discord Server: Access to a Discord server where you can add the bot.
- Discord Bot Token and Channel ID: You'll need these to configure the bot.
Clone the repository to your local machine:
git clone https://github.com/yourusername/ip-monitor.git
cd ip-monitor
- Go to the Discord Developer Portal: Discord Developer Portal
- Create a New Application:
- Click on "New Application".
- Enter a name for your application and click "Create".
- Add a Bot to Your Application:
- Navigate to the "Bot" tab on the left.
- Click "Add Bot" and confirm.
- Copy the Bot Token:
- Under the bot's username, click "Copy" to copy the bot's token.
- Important: Keep this token secure!
- Invite the Bot to Your Server:
- Go to the "OAuth2" tab and then "URL Generator".
- Under "Scopes", select "bot".
- Under "Bot Permissions", select the following permissions:
- Send Messages
- Manage Messages
- Read Message History
- Copy the generated URL and paste it into your browser to invite the bot to your server.
- Get the Channel ID:
- Enable Developer Mode in Discord settings (User Settings > Advanced > Developer Mode).
- Right-click the channel where you want the bot to post and select "Copy ID".
In the project root directory, create a .env
file to store your environment variables:
DISCORD_BOT_TOKEN=your_bot_token
DISCORD_CHANNEL_ID=your_channel_id
- Replace
your_bot_token
with the bot token you copied earlier. - Replace
your_channel_id
with the ID of the channel where the bot will operate.
Important: Do not share this file or commit it to version control.
If you plan to build and run the application locally (without Docker), install the required Go packages:
go mod download
To build and run the application locally without Docker:
go build -o ip-monitor .
./ip-monitor
Build the Docker image using the provided Dockerfile
:
docker build -t ip-monitor .
Run the Docker container, passing the .env
file:
docker run -d \
--name ip-monitor \
--env-file .env \
ip-monitor
- DISCORD_BOT_TOKEN: Your Discord bot token (required).
- DISCORD_CHANNEL_ID: The ID of the Discord channel where the bot will post messages (required).
To stop and remove the Docker container:
docker stop ip-monitor
docker rm ip-monitor
If you make changes to the code:
-
Rebuild the Docker Image:
docker build -t ip-monitor .
-
Restart the Container:
docker stop ip-monitor docker rm ip-monitor docker run -d \ --name ip-monitor \ --env-file .env \ ip-monitor
- Protect Your
.env
File: Ensure your.env
file is not committed to version control. It contains sensitive information. - Environment Variables in Production: For production environments, consider using a secrets manager or setting environment variables directly in your deployment environment.
- Bot Permissions: Ensure your bot has the correct permissions in the Discord channel:
- Send Messages
- Manage Messages (to pin messages)
- Read Message History
-
The
checkInterval
constant inmain.go
controls how often the application checks for IP changes. -
Default is set to check every 5 minutes:
const checkInterval = 5 * time.Minute
-
Adjust as needed, then rebuild the Docker image.
-
View Logs: To view the application logs, use:
docker logs -f ip-monitor
-
Enhanced Logging: Consider integrating a logging framework like
logrus
for more advanced logging capabilities.
- The application includes basic error handling.
- For production use, consider implementing retry mechanisms and more sophisticated error handling.
-
Bot Not Responding:
-
Verify the bot is online in your Discord server.
-
Ensure the bot has the necessary permissions.
-
Check the logs for errors:
docker logs ip-monitor
-
-
Environment Variables Not Set:
- Ensure the
DISCORD_BOT_TOKEN
andDISCORD_CHANNEL_ID
are correctly set in your.env
file. - Verify that the
.env
file is in the project root and is correctly formatted.
- Ensure the
-
Cannot Connect to Discord:
- Ensure your network allows outbound connections to Discord's API.
- Check for firewall or network restrictions.
Contributions are welcome! Please follow these steps:
-
Fork the Repository
-
Create a Feature Branch
git checkout -b feature/YourFeature
-
Commit Your Changes
git commit -m "Add your message here"
-
Push to the Branch
git push origin feature/YourFeature
-
Open a Pull Request
This project is licensed under the MIT License.
If you're interested in extending the functionality of the bot, consider the following enhancements:
- Why: Instead of sharing your raw IP address, use a domain name that updates automatically.
- How: Integrate with a dynamic DNS provider like DuckDNS or No-IP.
- Why: Keeping a log of IP changes can be useful for auditing.
- How: Append changes to a local file or store them in a database.
- Why: Notify users across different platforms.
- How: Integrate with Slack, Telegram, or send email notifications.
- How: Use an error tracking service like Sentry to capture and analyze exceptions.
- discordgo: A Go package that provides low-level bindings to the Discord chat client API.
- Author: Steven Lawton
- Email: [email protected]
- GitHub: stevenlawton