-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
121 lines (112 loc) · 3.62 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
"net/url"
"regexp"
"strings"
"github.com/Krognol/go-wolfram"
"github.com/bwmarrin/discordgo"
"github.com/mz2212/discord-utility-bot/markov"
)
func messageCreate(client *discordgo.Session, message *discordgo.MessageCreate) {
splitMessage := strings.Split(message.Content, " ")
if message.Author.ID == client.State.User.ID {
return // Ignore messages from self
}
// Reddit Stuff
// '/r/' helper.
matcher := regexp.MustCompile(`^[(reddit\.com)]?/?r/([^\s]+)`)
if matcher.MatchString(message.Content) {
matches := matcher.FindStringSubmatch(message.Content)
//fmt.Println("Matches: ", matches) // Debugging.
client.ChannelMessageSend(message.ChannelID, fmt.Sprint("Link: https://reddit.com/r/", matches[1]))
return
}
// User/Sub quote generator.
if splitMessage[0] == "#!usergen" {
client.ChannelMessageSend(message.ChannelID, gen("/u/"+splitMessage[1]))
return
}
if splitMessage[0] == "#!subgen" {
client.ChannelMessageSend(message.ChannelID, gen("/r/"+splitMessage[1]+"/comments"))
return
}
// In chat documentation
if splitMessage[0] == "#!help" {
channel, err := client.UserChannelCreate(message.Author.ID)
if err != nil {
fmt.Println("[Discord] Failed to create DM: ", err)
return
}
client.ChannelMessageSend(channel.ID, help())
}
// Wolfram Stuff
if splitMessage[0] == "#!ask" {
if splitMessage[1] == "-image" {
// Question handler using the "Simple API"
query := strings.Join(splitMessage[2:], " ")
img, _, err := wolf.GetSimpleQuery(query, url.Values{})
if err != nil {
client.ChannelMessageSend(message.ChannelID, errorText())
fmt.Println("[Wolfram|Alpha] Error: ", err)
return
}
client.ChannelFileSend(message.ChannelID, "simple.gif", img)
return // I guess the reader returned is a gif, so I just upload it to discord
}
// Question handler using the "Short Answer API"
query := strings.Join(splitMessage[1:], " ")
answer, err := wolf.GetShortAnswerQuery(query, wolfram.Metric, 30)
if err != nil { // For some reason the area of a football field is still returned in feet
client.ChannelMessageSend(message.ChannelID, errorText())
fmt.Println("[Wolfram|Alpha] Error: ", err)
return
}
client.ChannelMessageSend(message.ChannelID, answer)
return
}
// MAL stuff
if splitMessage[0] == "#!anime" {
query := strings.Join(splitMessage[1:], " ")
result, _, err := malCli.Anime.Search(query)
if err != nil {
client.ChannelMessageSend(message.ChannelID, errorText())
return
}
client.ChannelMessageSend(message.ChannelID, animeFormat(result.Rows[0].Title,
result.Rows[0].StartDate,
result.Rows[0].EndDate,
result.Rows[0].Score,
result.Rows[0].Episodes))
return
}
if splitMessage[0] == "#!manga" {
query := strings.Join(splitMessage[1:], " ")
result, _, err := malCli.Manga.Search(query)
if err != nil {
client.ChannelMessageSend(message.ChannelID, errorText())
return
}
client.ChannelMessageSend(message.ChannelID, mangaFormat(result.Rows[0].Title,
result.Rows[0].StartDate,
result.Rows[0].EndDate,
result.Rows[0].Score,
result.Rows[0].Chapters,
result.Rows[0].Volumes))
return
}
}
// Helper Functions
func gen(loc string) string {
harvest, err := redd.Listing(loc, "")
if err != nil {
fmt.Println("[Reddit] Failed to get listing for ", loc, ": ", err)
return fmt.Sprint("Failed to get listing for ", loc, "\nEither that location doesn't exist, or I bugged out...")
}
gen := markov.New(2)
for _, comment := range harvest.Comments[:30] {
gen.Build(comment.Body)
}
locSplit := strings.Split(loc, "/")
return fmt.Sprint(gen.Generate(100), " - /", locSplit[1], "/", locSplit[2])
}