Skip to content

Commit c990dfd

Browse files
committed
Initial commit
Initial commit
0 parents  commit c990dfd

File tree

182 files changed

+262316
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

182 files changed

+262316
-0
lines changed

.dockerignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.git
2+
.gitignore
3+
Dockerfile
4+
.DS_Store
5+
README.md
6+
LICENSE
7+
web-watcher
8+
db.sqlite
9+
vendor

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, build with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
db.sqlite
15+
web-watcher
16+
.idea/*

Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM golang:latest
2+
3+
WORKDIR /app
4+
5+
COPY go.mod go.sum ./
6+
7+
RUN go mod download
8+
9+
COPY . .
10+
11+
RUN go build -o main .
12+
13+
CMD ["./main"]
14+

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Shellbear
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Web-watcher
2+
3+
A small discord bot which aims to monitor and send notifications on website changes.
4+
5+
## Installation
6+
7+
If you want to self host this bot, you have to first create a new Discord application and bot from the [developer portal](https://discordapp.com/developers/applications/).
8+
You can follow this [tutorial](https://github.com/reactiflux/discord-irc/wiki/Creating-a-discord-bot-&-getting-a-token) to achieve this step.
9+
10+
With go CLI:
11+
```bash
12+
go get github.com/ShellBear/web-watcher
13+
```
14+
15+
## Usage
16+
17+
```bash
18+
export DISCORD_TOKEN=YOUR_DISCORD_TOKEN
19+
web-watcher
20+
21+
# or
22+
23+
web-watcher --token YOUR_DISCORD_TOKEN
24+
```
25+
26+
```bash
27+
web-watcher --help
28+
29+
Web-watcher discord Bot.
30+
31+
Options:
32+
-delay int
33+
Watch delay in minutes (default 60)
34+
-token string
35+
Discord token
36+
```
37+
38+
By default the watch interval for every website is 1 Hour but you can easily change this with the `delay` parameter followed by the time interval in minutes.
39+
40+
```bash
41+
# Set watch interval to 10 minutes
42+
web-watcher --token YOUR_DISCORD_TOKEN --delay 10
43+
```
44+
45+
## Commands
46+
47+
#### !watch [URL]
48+
49+
Add an URL to the watchlist.
50+
51+
#### !unwatch [URL]
52+
53+
Remove an URL from the watchlist.
54+
55+
#### !watchlist
56+
57+
Get the complete watchlist.
58+
59+
## Deploy
60+
61+
With docker:
62+
```bash
63+
docker build -t web-watcher .
64+
```
65+
66+
And test:
67+
```bash
68+
docker run -e DISCORD_TOKEN=YOUR_DISCORD_TOKEN web-watcher
69+
```
70+
71+
## Built With
72+
73+
- [Gorm](https://github.com/jinzhu/gorm) - The fantastic ORM library for Golang
74+
- [DiscordGo](https://github.com/bwmarrin/discordgo) - Go bindings for Discord
75+
76+
## License
77+
78+
This project is licensed under the MIT License - see the [LICENSE](LICENSE.md) file for details

commands.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/bwmarrin/discordgo"
6+
"log"
7+
"net/url"
8+
"strings"
9+
"time"
10+
)
11+
12+
type commandProto func(*discordgo.Session, *discordgo.MessageCreate, []string) (*discordgo.Message, error)
13+
14+
var commands = map[string]commandProto{
15+
"!watch": watch,
16+
"!unwatch": unwatch,
17+
"!watchlist": watchList,
18+
}
19+
20+
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
21+
args := strings.Split(strings.TrimSpace(m.Content), " ")
22+
23+
if m.Author.ID == s.State.User.ID || len(args) == 0 {
24+
return
25+
}
26+
27+
if val, ok := commands[args[0]]; ok {
28+
if _, err := val(s, m, args); err != nil {
29+
log.Fatalln(err)
30+
}
31+
}
32+
}
33+
34+
func watch(s *discordgo.Session, m *discordgo.MessageCreate, args []string) (*discordgo.Message, error) {
35+
if len(args) != 2 {
36+
return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+" Usage: watch URL")
37+
}
38+
39+
url, err := url.ParseRequestURI(args[1])
40+
if err != nil {
41+
return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+" provided URL is invalid")
42+
}
43+
44+
website := Website{
45+
Url: url.String(),
46+
GuildID: m.GuildID,
47+
}
48+
49+
if db.First(&website, website).RecordNotFound() {
50+
website.ChannelID = m.ChannelID
51+
52+
db.Create(&website)
53+
launchTask(&website)
54+
55+
return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+" successfully registered URL")
56+
}
57+
58+
return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+" this URL has already been registered")
59+
}
60+
61+
func unwatch(s *discordgo.Session, m *discordgo.MessageCreate, args []string) (*discordgo.Message, error) {
62+
if len(args) != 2 {
63+
return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+" Usage: unwatch URL")
64+
}
65+
66+
url, err := url.ParseRequestURI(args[1])
67+
if err != nil {
68+
return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+" provided URL is invalid")
69+
}
70+
71+
var website Website
72+
73+
if !db.First(&website, Website{Url: url.String(), GuildID: m.GuildID}).RecordNotFound() {
74+
taskName := website.Url + website.ChannelID
75+
if val, ok := tasks[taskName]; ok {
76+
val()
77+
delete(tasks, taskName)
78+
}
79+
80+
db.Delete(&website)
81+
return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+" successfully deleted URL")
82+
}
83+
84+
return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+" URL doesn't exist")
85+
}
86+
87+
func watchList(s *discordgo.Session, m *discordgo.MessageCreate, args []string) (*discordgo.Message, error) {
88+
var websites []Website
89+
90+
db.Find(&websites, Website{GuildID: m.GuildID})
91+
92+
if len(websites) == 0 {
93+
return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+"There is no registered URL. Add one with `watch` command")
94+
}
95+
96+
var urls []string
97+
98+
for i, website := range websites {
99+
urls = append(urls, fmt.Sprintf("%d - %s", i+1, website.Url))
100+
}
101+
102+
return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+"\n"+strings.Join(urls, "\n"))
103+
}
104+
105+
func ready(discord *discordgo.Session, ready *discordgo.Ready) {
106+
if err := discord.UpdateStatus(0, "Looking at other people's website"); err != nil {
107+
log.Fatalln("Error attempting to set my status,", err)
108+
}
109+
110+
servers := discord.State.Guilds
111+
log.Printf("Web-watcher has started on %d servers\n", len(servers))
112+
log.Printf("Inspecting websites every %d minutes", int((time.Duration(*watchDelay) * time.Minute).Minutes()))
113+
}

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module web-watcher
2+
3+
go 1.13
4+
5+
require (
6+
github.com/bwmarrin/discordgo v0.19.0
7+
github.com/jinzhu/gorm v1.9.10
8+
)

0 commit comments

Comments
 (0)