func (w *Watcher) updateTask(task *models.Task, hash string, body []byte) error {
	if _, err := w.Session.ChannelMessageSend(
		task.ChannelID,
		fmt.Sprintf("%s has been updated! Last update : %s", task.URL, task.UpdatedAt.Format(updateFormat)),
	); err != nil {
		return err
	}
	return w.DB.Model(task).Updates(&models.Task{
		Hash: hash,
		Body: body,
	}).Error
}
Update task in database and alert sender on Discord.
package watcher
import (
	"fmt"
	"net/http"
	"time"
	"github.com/bwmarrin/discordgo"
	"github.com/jinzhu/gorm"
	"golang.org/x/net/context"
	"github.com/shellbear/web-watcher/models"
)
type Watcher struct {
	DB *gorm.DB
	Session *discordgo.Session
	Client *http.Client
	Prefix string
	ChangeRatio float64
	WatchInterval time.Duration
	Tasks map[string]context.CancelFunc
}
func New(watchInterval time.Duration, changeRatio float64, discordToken string, prefix string) (*Watcher, error) {
	db, err := models.New()
	if err != nil {
		return nil, fmt.Errorf("failed to init database: %s", err)
	}
	session, err := discordgo.New("Bot " + discordToken)
	if err != nil {
		return nil, fmt.Errorf("failed to connect to discord: %s", err)
	}
	session.Client = &http.Client{Timeout: time.Second * 15}
	w := Watcher{
		DB:            db,
		Session:       session,
		Prefix:        prefix,
		WatchInterval: watchInterval,
		ChangeRatio:   changeRatio,
		Client:        session.Client,
		Tasks:         map[string]context.CancelFunc{},
	}
	session.AddHandlerOnce(w.onReady)
	session.AddHandler(w.onNewMessage)
	return &w, nil
}
func (w *Watcher) Run() error {
	var tasks []models.Task
	if err := w.DB.Find(&tasks).Error; err != nil {
		return fmt.Errorf("failed to fetch existing tasks: %s", err)
	}
	for i := range tasks {
		w.NewTask(&tasks[i])
	}
	return w.Session.Open()
}