-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.go
134 lines (124 loc) · 3.29 KB
/
bot.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
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"flag"
"fmt"
"net/url"
"strconv"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/coolbrow/dankbot/images"
"github.com/coolbrow/dankbot/reddit"
"github.com/coolbrow/dankbot/status"
"github.com/coolbrow/dankbot/textapis"
"google.golang.org/appengine"
)
const usage string = "\n" +
"*DankBot Commands:*\n\n" +
"`!help`\n" +
" Shows this text.\n\n" +
"`!reddit [subreddit]`\n" +
" Returns random top result of given subreddit.\n\n" +
"`!reddit [subreddit] [query]`\n" +
" Searches given subreddit for query and returns random top result.\n\n" +
"`!rip [name]`\n" +
" RIP pic\n\n" +
"`!retro [your], [text], [here]`\n" +
" Generates retro text\n\n" +
"`!sombra`\n" +
" ~~Party~~ Fiesta time\n\n" +
"`!swanson`\n" +
" Ron Swanson quote\n\n" +
"`!catfact`\n" +
" Cat fact\n\n" +
"`!roll NdM`\n" +
" Rolls M-sided dice N times; posts the sum"
func main() {
token := flag.String("token", "", "Client token")
flag.Parse()
if *token == "" {
fmt.Println("Must provide client token. Run with -h for more info")
return
}
discord, err := discordgo.New("Bot " + *token)
if err != nil {
fmt.Println("Error authenticating: ", err)
return
}
discord.AddHandler(newMessage)
err = discord.Open()
if err != nil {
fmt.Println("Error opening Discord session: ", err)
return
}
statusUpdate := func(s string) {
discord.UpdateStatus(0, s)
}
statusUpdate(status.RandomStatus())
status.TickerStatus(time.Hour*6, statusUpdate)
fmt.Println("DankBot is now running.")
appengine.Main()
}
func newMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
args := strings.Split(m.Content, " ")
switch args[0] {
case "!sombra":
sendMessage(s, m.ChannelID, images.Sombra())
case "!reddit":
var msg string
if len(args) >= 3 {
msg = reddit.RandomSearch(args[1], strings.Join(args[1:], " "))
} else if len(args) == 2 {
msg = reddit.Random(args[1])
} else {
msg = "*Must provide a subreddit.*"
}
if msg == "" {
msg = "*No results found.*"
}
sendMessage(s, m.ChannelID, msg)
case "!rip":
name := url.QueryEscape(strings.Join(args[1:], " "))
sendMessage(s, m.ChannelID, images.GenerateRIP(name))
case "!retro":
lines := strings.Split(strings.Join(args[1:], " "), ",")
var text1, text2, text3 string
switch {
case len(lines) >= 3:
text1 = lines[0]
text2 = lines[1]
text3 = strings.Join(lines[2:], " ")
case len(lines) == 2:
text1 = lines[0]
text2 = lines[1]
case len(lines) == 1:
text2 = lines[0]
}
sendMessage(s, m.ChannelID, images.GenerateRetro(text1, text2, text3))
case "!help":
sendMessage(s, m.ChannelID, usage)
case "!catfact":
sendMessage(s, m.ChannelID, textapis.CatFact())
case "!swanson":
sendMessage(s, m.ChannelID, textapis.Swanson())
case "!roll":
elems := strings.Split(args[1], "d")
reps, err := strconv.Atoi(elems[0])
if err != nil {
fmt.Println("Error parsing dice count: ", err)
return
}
side, err := strconv.Atoi(elems[1])
if err != nil {
fmt.Println("Error parsing dice sides: ", err)
return
}
sendMessage(s, m.ChannelID, textapis.Dice(reps, side))
}
}
func sendMessage(s *discordgo.Session, channelID string, content string) {
_, err := s.ChannelMessageSend(channelID, content)
if err != nil {
fmt.Println("Error sending message: ", err)
}
}