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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
package thulani
import (
"net/url"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/mammothbane/thulani-go/downloader"
)
var config *Config
var regex *regexp.Regexp
var manager *downloader.DownloadManager
func Run(conf *Config) {
//defer profile.Start(profile.ProfilePath("."), profile.BlockProfile).Stop()
config = conf
regex = regexp.MustCompile(`(?i)^[!/]` + conf.Trigger + " (.*)")
dg, err := discordgo.New("Bot " + conf.Token)
handle(err)
dg.AddHandler(onReady)
dg.AddHandler(onMessage)
dg.AddHandler(onGuildCreate)
dg.Open()
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
dg.Close()
}
func onReady(s *discordgo.Session, m *discordgo.Ready) {
log.Infof("Logged in as %v (%v)", m.User.Username, m.User.ID)
s.UpdateStatus(0, "literally nothing")
joined := false
for _, v := range m.Guilds {
if v.ID != config.GuildStr() {
joined = true
break
}
}
if !joined {
log.Warningf("Server in config not available! Click here to enable thulani on your server: %v", oauthUrl())
}
manager = downloader.NewManager(s, config.GuildStr(), config.VoiceChannelStr())
}
func onGuildCreate(s *discordgo.Session, m *discordgo.GuildCreate) {
member, err := s.GuildMember(m.Guild.ID, s.State.User.ID)
if err != nil {
log.Warningf("joined guild %v but was unable to get member id: %q", m.Name, err)
log.Notice("please reconnect to guild: %v", oauthUrl())
s.GuildLeave(m.Guild.ID)
return
}
log.Infof("joined guild %v", m.Name)
perms := 0
for _, role := range m.Roles {
for _, mRole := range member.Roles {
if role.ID == mRole {
perms |= role.Permissions
log.Infof("discovered role: %v (%v)", role.Name, role.ID)
}
}
}
if perms&requestedPerms != requestedPerms {
log.Errorf("server didn't grant us the desired permissions.")
s.GuildLeave(m.Guild.ID)
log.Warningf("Don't disable any permissions or thulani will be a little sponge man! Click here to die: %v", oauthUrl())
return
}
err = s.GuildMemberNickname(m.Guild.ID, "@me", "newlani")
if err != nil {
log.Warningf("unable to update nickname: %q", err)
}
}
func onMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
log.Debugf("got message %q", m.Content)
ctx, err := newCtx(s, m)
if err != nil {
log.Errorf("error constructing message context: %q", err)
return
}
for _, v := range extraMemes {
v(ctx)
}
if !ctx.Matched {
log.Infof("Message didn't match. Ignoring.")
return
}
if ctx.Channel.IsPrivate {
log.Infof("Ignoring private message")
return
}
if ctx.Guild.ID != config.GuildStr() {
log.Infof("Wrong guild. Ignoring.")
return
}
fn, ok := cmdMap[strings.ToLower(ctx.Command)]
if ok {
authorized := false
for _, role := range ctx.Guild.Roles {
for _, v := range ctx.Member.Roles {
if v != role.Name {
continue
}
if role.Name == config.OpRole {
authorized = true
}
}
}
if !authorized {
log.Infof("User %v not authorized.", m.Author.Username)
ctx.sendMessage("fuck you. you're not allowed to do that.", m.Tts)
return
}
fn(ctx)
return
}
// it's not a command we know; we're looking for a url
target, err := url.Parse(ctx.Command)
if err != nil {
log.Errorf("Url parse failed: %q", err)
ctx.sendMessage("format your commands right. fuck you.", m.Tts)
return
}
if target.Path == "" || (target.Path == "/watch" && len(target.Query()) == 0) {
log.Warningf("Bad url format: %q", ctx.Command)
ctx.sendMessage("that\nis\na\nbad\nurl", m.Tts)
return
}
if strings.Contains(target.Hostname(), "imgur") {
log.Infof("Ignoring imgur link.")
if m.Author.Username == "boomshticky" {
ctx.sendMessage("fuck you conway", true)
} else {
ctx.sendMessage("NO IMGUR", m.Tts)
}
}
if err := manager.Enqueue(target.String(), 0, 0); err != nil {
log.Errorf("unable to enqueue video: %q", err)
ctx.sendMessage("you fucked up the video.", ctx.Tts)
return
}
log.Infof("started playing from: %q", target.String())
}
|