|
| 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 | +} |
0 commit comments