Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Initial commit
  • Loading branch information
shellbear committed May 3, 2020
0 parents commit c990dfd
Show file tree
Hide file tree
Showing 182 changed files with 262,316 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.git
.gitignore
Dockerfile
.DS_Store
README.md
LICENSE
web-watcher
db.sqlite
vendor
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

db.sqlite
web-watcher
.idea/*
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM golang:latest

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

COPY . .

RUN go build -o main .

CMD ["./main"]

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Shellbear

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
78 changes: 78 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Web-watcher

A small discord bot which aims to monitor and send notifications on website changes.

## Installation

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/).
You can follow this [tutorial](https://github.com/reactiflux/discord-irc/wiki/Creating-a-discord-bot-&-getting-a-token) to achieve this step.

With go CLI:
```bash
go get github.com/ShellBear/web-watcher
```

## Usage

```bash
export DISCORD_TOKEN=YOUR_DISCORD_TOKEN
web-watcher

# or

web-watcher --token YOUR_DISCORD_TOKEN
```

```bash
web-watcher --help

Web-watcher discord Bot.

Options:
-delay int
Watch delay in minutes (default 60)
-token string
Discord token
```

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.

```bash
# Set watch interval to 10 minutes
web-watcher --token YOUR_DISCORD_TOKEN --delay 10
```

## Commands

#### !watch [URL]

Add an URL to the watchlist.

#### !unwatch [URL]

Remove an URL from the watchlist.

#### !watchlist

Get the complete watchlist.

## Deploy

With docker:
```bash
docker build -t web-watcher .
```

And test:
```bash
docker run -e DISCORD_TOKEN=YOUR_DISCORD_TOKEN web-watcher
```

## Built With

- [Gorm](https://github.com/jinzhu/gorm) - The fantastic ORM library for Golang
- [DiscordGo](https://github.com/bwmarrin/discordgo) - Go bindings for Discord

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE.md) file for details
113 changes: 113 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

import (
"fmt"
"github.com/bwmarrin/discordgo"
"log"
"net/url"
"strings"
"time"
)

type commandProto func(*discordgo.Session, *discordgo.MessageCreate, []string) (*discordgo.Message, error)

var commands = map[string]commandProto{
"!watch": watch,
"!unwatch": unwatch,
"!watchlist": watchList,
}

func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
args := strings.Split(strings.TrimSpace(m.Content), " ")

if m.Author.ID == s.State.User.ID || len(args) == 0 {
return
}

if val, ok := commands[args[0]]; ok {
if _, err := val(s, m, args); err != nil {
log.Fatalln(err)
}
}
}

func watch(s *discordgo.Session, m *discordgo.MessageCreate, args []string) (*discordgo.Message, error) {
if len(args) != 2 {
return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+" Usage: watch URL")
}

url, err := url.ParseRequestURI(args[1])
if err != nil {
return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+" provided URL is invalid")
}

website := Website{
Url: url.String(),
GuildID: m.GuildID,
}

if db.First(&website, website).RecordNotFound() {
website.ChannelID = m.ChannelID

db.Create(&website)
launchTask(&website)

return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+" successfully registered URL")
}

return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+" this URL has already been registered")
}

func unwatch(s *discordgo.Session, m *discordgo.MessageCreate, args []string) (*discordgo.Message, error) {
if len(args) != 2 {
return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+" Usage: unwatch URL")
}

url, err := url.ParseRequestURI(args[1])
if err != nil {
return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+" provided URL is invalid")
}

var website Website

if !db.First(&website, Website{Url: url.String(), GuildID: m.GuildID}).RecordNotFound() {
taskName := website.Url + website.ChannelID
if val, ok := tasks[taskName]; ok {
val()
delete(tasks, taskName)
}

db.Delete(&website)
return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+" successfully deleted URL")
}

return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+" URL doesn't exist")
}

func watchList(s *discordgo.Session, m *discordgo.MessageCreate, args []string) (*discordgo.Message, error) {
var websites []Website

db.Find(&websites, Website{GuildID: m.GuildID})

if len(websites) == 0 {
return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+"There is no registered URL. Add one with `watch` command")
}

var urls []string

for i, website := range websites {
urls = append(urls, fmt.Sprintf("%d - %s", i+1, website.Url))
}

return s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+"\n"+strings.Join(urls, "\n"))
}

func ready(discord *discordgo.Session, ready *discordgo.Ready) {
if err := discord.UpdateStatus(0, "Looking at other people's website"); err != nil {
log.Fatalln("Error attempting to set my status,", err)
}

servers := discord.State.Guilds
log.Printf("Web-watcher has started on %d servers\n", len(servers))
log.Printf("Inspecting websites every %d minutes", int((time.Duration(*watchDelay) * time.Minute).Minutes()))
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module web-watcher

go 1.13

require (
github.com/bwmarrin/discordgo v0.19.0
github.com/jinzhu/gorm v1.9.10
)
Loading

0 comments on commit c990dfd

Please sign in to comment.