diff options
| author | Nathan Perry <avaglir@gmail.com> | 2017-07-27 21:14:04 -0400 |
|---|---|---|
| committer | Nathan Perry <avaglir@gmail.com> | 2017-07-27 21:14:04 -0400 |
| commit | c8f28679d10b3004fb7f04f54e625284c93e0c70 (patch) | |
| tree | 36834e113aa2376989b5d78c383a0ded0136ed8c | |
| parent | c6e5779f22b18b971f82ddd2c5de81015ac9740c (diff) | |
plenty of updates for today
| -rw-r--r-- | commands.go | 38 | ||||
| -rw-r--r-- | config.example.json | 4 | ||||
| -rw-r--r-- | extramemes.go | 72 | ||||
| -rw-r--r-- | messagectx.go | 6 | ||||
| -rw-r--r-- | thulani.go | 135 | ||||
| -rw-r--r-- | util.go | 58 |
6 files changed, 249 insertions, 64 deletions
diff --git a/commands.go b/commands.go index d3f5684..525c915 100644 --- a/commands.go +++ b/commands.go @@ -1,7 +1,41 @@ package thulani -import "github.com/bwmarrin/discordgo" +var cmdMap = map[string]func(*messageCtx){ + "help": printHelp, + "skip": commandNotImplemented, + "pause": commandNotImplemented, + "resume": commandNotImplemented, + "sudoku": commandNotImplemented, + "die": commandNotImplemented, + "list": commandNotImplemented, + "queue": commandNotImplemented, +} + +func printHelp(c *messageCtx) { + c.sendMessage(help, c.Tts) +} + +func commandNotImplemented(c *messageCtx) { + log.Errorf("%q not implemented", c.Command) + c.sendMessage("not implemented", c.Tts) +} + +func skip(c *messageCtx) { + log.Error("skip not implemented") +} + +func resume(c *messageCtx) { + log.Error("skip not implemented") +} -func printHelp(s *discordgo.Session, create *discordgo.MessageCreate) { +func pause(c *messageCtx) { + log.Error("skip not implemented") +} + +func stop(c *messageCtx) { + log.Error("skip not implemented") +} +func list(c *messageCtx) { + log.Error("skip not implemented") } diff --git a/config.example.json b/config.example.json index d3de506..2b923b7 100644 --- a/config.example.json +++ b/config.example.json @@ -5,5 +5,7 @@ "op_role": "bot-op", "server": "example server", "voice_channel": "General", - "token": 12391823918 + "token": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "client_id": "abcd", + "client_secret": "efgh" } diff --git a/extramemes.go b/extramemes.go new file mode 100644 index 0000000..4bac256 --- /dev/null +++ b/extramemes.go @@ -0,0 +1,72 @@ +package thulani + +import ( + "math/rand" + "strings" + "time" +) + +func init() { + rand.Seed(time.Now().UnixNano()) +} + +var extraMemes = []func(*messageCtx) MemeStatus{ + respondToFuckYou, + respondToMeme, +} + +var hateMatch = []string{ + "suck", + "fuck", + "trash", + "garbage", + "stupid", + "shit", + "dick", + "bitch", + "hate", +} + +var responses = []string{ + "WELL FUCK YOU TOO YOU PIECE OF SHIT", + "**i'll fucking burst ye**", + "memememexexxxxxxxxxxxwerp", + "thulando madondo", + "you are a memerman", +} + +type MemeStatus int + +const ( + Continue MemeStatus = iota + Interrupt +) + +func respondToFuckYou(ctx *messageCtx) (result MemeStatus) { + result = Continue + content := strings.ToLower(ctx.Message.Content) + + if !strings.Contains(content, config.Trigger) { + return + } + + for _, v := range hateMatch { + if strings.Contains(content, strings.ToLower(v)) { + response := responses[rand.Intn(len(responses))] + + ctx.sendMessage(response, true) + return + } + } + + return +} + +func respondToMeme(ctx *messageCtx) MemeStatus { + if !(ctx.Matched && ctx.Command == "meme") { + return Continue + } + + ctx.sendMessage("i am not yet capable of memeing.", false) + return Interrupt +} diff --git a/messagectx.go b/messagectx.go index ee38f5f..75d6ce6 100644 --- a/messagectx.go +++ b/messagectx.go @@ -25,6 +25,8 @@ func newCtx(s *discordgo.Session, m *discordgo.MessageCreate) (*messageCtx, erro matches := regex.FindStringSubmatch(m.Content) command := "" + log.Debugf("matches: %v", matches) + if len(matches) != 0 { command = strings.Split(matches[1], " ")[0] } @@ -66,7 +68,7 @@ func newCtx(s *discordgo.Session, m *discordgo.MessageCreate) (*messageCtx, erro MessageCreate: m, Command: command, - Matched: len(matches) == 0, + Matched: len(matches) != 0, Channel: channel, Guild: guild, @@ -75,7 +77,7 @@ func newCtx(s *discordgo.Session, m *discordgo.MessageCreate) (*messageCtx, erro } func (ctx *messageCtx) sendMessage(str string, tts bool) { - if !ctx.Tts { + if !tts { ctx.ChannelMessageSend(ctx.ChannelID, str) return } @@ -1,7 +1,6 @@ package thulani import ( - "fmt" "net/url" "os" "os/signal" @@ -17,32 +16,16 @@ var regex *regexp.Regexp func Run(conf *Config) { config = conf - regex = regexp.MustCompile("(?i)^[!/]" + conf.Trigger + " (.*)") + 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() - joined := false - - for !joined { - for _, v := range dg.State.Guilds { - if v.Name == conf.Server { - joined = true - break - } - } - - if !joined { - fmt.Println("Please input the token for whatever the fuck.") - var response string - fmt.Scanln(&response) - } - } - sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) <-sc @@ -52,6 +35,52 @@ func Run(conf *Config) { 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) { @@ -64,6 +93,16 @@ func onMessage(s *discordgo.Session, m *discordgo.MessageCreate) { 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 { @@ -71,7 +110,7 @@ func onMessage(s *discordgo.Session, m *discordgo.MessageCreate) { return } - if ctx.Channel.GuildID != config.Server { + if ctx.Guild.Name != config.Server { log.Infof("Wrong guild. Ignoring.") return } @@ -88,42 +127,34 @@ func onMessage(s *discordgo.Session, m *discordgo.MessageCreate) { return false } - //fmap := map[string]func(){ - // "sup": func() {}, - //}[ctx.Command] - - switch ctx.Command { - case "skip": - break - - case "die": - break + fn, ok := cmdMap[strings.ToLower(ctx.Command)] + if ok { + fn(ctx) + return + } - default: - 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 - } + // 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 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 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) - } - break + if m.Author.Username == "boomshticky" { + ctx.sendMessage("fuck you conway", true) + } else { + ctx.sendMessage("NO IMGUR", m.Tts) } - - // TODO: play audio } + } @@ -4,6 +4,13 @@ import ( "encoding/json" "os" + "net/url" + + "strconv" + + "sync" + + "github.com/bwmarrin/discordgo" "github.com/op/go-logging" ) @@ -12,13 +19,13 @@ const help = `wew lad. you should know these commands already. Usage: ` + "`!thulani [command]`" + ` commands: -**help**:\t\t\t\tprint this help message -**[url]**:\t\t\t a url with media that thulani can play. queued up to play after everything that's already waiting. -**list, queue**:\tlist items in the queue, as well as the currently-playing item. -**pause**:\t\t\tpause sound. -**resume**:\t\t resume sound. -**die**:\t\t\t\t empty the queue and stop playing. -**skip**:\t\t\t skip the current item. +**help**: print this help message +**[url]**: a url with media that thulani can play. queued up to play after everything that's already waiting. +**list, queue**: list items in the queue, as well as the currently-playing item. +**pause**: pause sound. +**resume**: resume sound. +**die**: empty the queue and stop playing. +**skip**: skip the current item. ` var log = logging.MustGetLogger("thulani") @@ -31,6 +38,8 @@ type Config struct { Server string `json:"server"` VoiceChannel string `json:"voice_channel"` Token string `json:"token"` + ClientID string `json:"client_id"` + ClientSecret string `json:"client_secret"` } func handle(err error) { @@ -49,3 +58,38 @@ func LoadConfig(filename string) (*Config, error) { err = json.NewDecoder(file).Decode(&conf) return &conf, err } + +var _oauthUrl string +var oauthOnce sync.Once + +const requestedPerms = discordgo.PermissionEmbedLinks | + discordgo.PermissionReadMessages | + discordgo.PermissionAddReactions | + discordgo.PermissionSendMessages | + discordgo.PermissionSendTTSMessages | + discordgo.PermissionMentionEveryone | + discordgo.PermissionUseExternalEmojis | + discordgo.PermissionVoiceConnect | + discordgo.PermissionVoiceSpeak | + discordgo.PermissionChangeNickname | + discordgo.PermissionVoiceUseVAD | + discordgo.PermissionAttachFiles + +func oauthUrl() string { + oauthOnce.Do(func() { + oUrl, err := url.Parse("https://discordapp.com/api/oauth2/authorize") + if err != nil { + panic(err) + } + + q := oUrl.Query() + q.Add("scope", "bot") + q.Add("permissions", strconv.Itoa(requestedPerms)) + q.Add("client_id", config.ClientID) + oUrl.RawQuery = q.Encode() + + _oauthUrl = oUrl.String() + }) + + return _oauthUrl +} |
