aboutsummaryrefslogtreecommitdiff
path: root/thulani.go
blob: d057768906aacf3e75743d2031b46bde5239cf54 (plain) (blame)
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
package thulani

import (
	"net/url"
	"os"
	"os/signal"
	"regexp"
	"strings"
	"syscall"

	"github.com/bwmarrin/discordgo"
)

var config *Config
var regex *regexp.Regexp

func Run(conf *Config) {
	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)

	_, err := s.UserUpdate("", "", "thulani", "https://cdn.discordapp.com/attachments/90548758458167296/338518035256311809/todd.png", "")
	if err != nil {
		log.Errorf("error updating user: %v", err)
	}

	s.UpdateStatus(0, "literally nothing")

	joined := false
	for _, v := range m.Guilds {
		if v.Name == config.Server {
			joined = true
			break
		}
	}

	if !joined {
		log.Warningf("Server in config not available! Click here to enable thulani on your server: %v", oauthUrl())
	}
}

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)
	}

	for _, role := range m.Roles {
		for _, mRole := range member.Roles {
			if role.ID == mRole {
				log.Infof("joined guild %v with role: %v (%v)", m.Name, role.Name, role.ID)

				if role.Permissions&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.Name != config.Server {
		log.Infof("Wrong guild. Ignoring.")
		return
	}

	_ = func() bool {
		for _, v := range ctx.Member.Roles {
			if v == config.OpRole {
				return true
			}
		}
		log.Infof("User %v not authorized.", m.Author.Username)

		ctx.sendMessage("fuck you. you're not allowed to do that.", m.Tts)
		return false
	}

	fn, ok := cmdMap[strings.ToLower(ctx.Command)]
	if ok {
		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)
		}
	}

}