func makeScriptFromMessages(service bruxism.Service, message bruxism.Message, messages []bruxism.Message) *comicgen.Script { speakers := make(map[string]int) avatars := make(map[int]string) script := []*comicgen.Message{} for _, message := range messages { speaker, ok := speakers[message.UserName()] if !ok { speaker = len(speakers) speakers[message.UserName()] = speaker avatars[speaker] = message.UserAvatar() } script = append(script, &comicgen.Message{ Speaker: speaker, Text: message.Message(), Author: message.UserName(), }) } return &comicgen.Script{ Messages: script, Author: fmt.Sprintf(service.UserName()), Avatars: avatars, Type: comicgen.ComicTypeChat, } }
// InviteHelp will return the help text for the invite command. func InviteHelp(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) (string, string) { switch service.Name() { case bruxism.DiscordServiceName: discord := service.(*bruxism.Discord) if discord.ApplicationClientID != "" { return "", fmt.Sprintf("Returns a URL to add %s to your server.", service.UserName()) } return "<discordinvite>", "Joins the provided Discord server." case bruxism.YouTubeServiceName: return "<videoid>", "Joins the provided YouTube live stream." } return "<channel>", "Joins the provided channel." }
func messageFunc(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) { if service.IsMe(message) || !bruxism.MatchesCommand(service, "myson", message) || service.Name() != bruxism.DiscordServiceName { return } discord := service.(*bruxism.Discord) discord.Session.ChannelMessageSendEmbed(message.Channel(), &discordgo.MessageEmbed{ Color: discord.UserColor(service.UserID(), message.Channel()), Description: "Don't ever talk to me or my son ever again.", Author: &discordgo.MessageEmbedAuthor{ Name: discord.NicknameForID(service.UserID(), service.UserName(), message.Channel()), IconURL: discordgo.EndpointUserAvatar(service.UserID(), discord.Session.State.User.Avatar), }, }) }
// InviteCommand is a command for accepting an invite to a channel. func InviteCommand(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message, command string, parts []string) { if service.Name() == bruxism.DiscordServiceName { discord := service.(*bruxism.Discord) if discord.ApplicationClientID != "" { service.SendMessage(message.Channel(), fmt.Sprintf("Please visit https://discordapp.com/oauth2/authorize?client_id=%s&scope=bot to add %s to your server.", discord.ApplicationClientID, service.UserName())) return } } if len(parts) == 1 { join := parts[0] if service.Name() == bruxism.DiscordServiceName { join = discordInviteID(join) } if err := service.Join(join); err != nil { if service.Name() == bruxism.DiscordServiceName && err == bruxism.ErrAlreadyJoined { service.PrivateMessage(message.UserID(), "I have already joined that server.") return } log.Println("Error joining %s %v", service.Name(), err) } else if service.Name() == bruxism.DiscordServiceName { service.PrivateMessage(message.UserID(), "I have joined that server.") } } }
// Help returns a list of help strings that are printed when the user requests them. func (p *playingPlugin) helpFunc(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message, detailed bool) []string { if detailed { return nil } if !service.IsBotOwner(message) { return nil } return bruxism.CommandHelp(service, "playing", "<game>, <url>", fmt.Sprintf("Set which game %s is playing.", service.UserName())) }
// Message handler. func (p *comicPlugin) Message(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) { if service.IsMe(message) { return } p.Lock() defer p.Unlock() log, ok := p.log[message.Channel()] if !ok { log = []bruxism.Message{} } if bruxism.MatchesCommand(service, "customcomic", message) || bruxism.MatchesCommand(service, "customcomicsimple", message) { ty := comicgen.ComicTypeChat if bruxism.MatchesCommand(service, "customcomicsimple", message) { ty = comicgen.ComicTypeSimple } service.Typing(message.Channel()) str, _ := bruxism.ParseCommand(service, message) messages := []*comicgen.Message{} splits := strings.Split(str, "|") for _, line := range splits { line := strings.Trim(line, " ") text := "" speaker := 0 author := "" if strings.Index(line, ":") != -1 { lineSplit := strings.Split(line, ":") author = strings.ToLower(strings.Trim(lineSplit[0], " ")) var err error speaker, err = strconv.Atoi(author) if err != nil { speaker = -1 } text = strings.Trim(lineSplit[1], " ") } else { text = line } messages = append(messages, &comicgen.Message{ Speaker: speaker, Text: text, Author: author, }) } if len(messages) == 0 { service.SendMessage(message.Channel(), fmt.Sprintf("Sorry %s, you didn't add any text.", message.UserName())) return } p.makeComic(bot, service, message, &comicgen.Script{ Messages: messages, Author: fmt.Sprintf(service.UserName()), Type: ty, }) } else if bruxism.MatchesCommand(service, "comic", message) { if len(log) == 0 { service.SendMessage(message.Channel(), fmt.Sprintf("Sorry %s, I don't have enough messages to make a comic yet.", message.UserName())) return } service.Typing(message.Channel()) lines := 0 linesString, parts := bruxism.ParseCommand(service, message) if len(parts) > 0 { lines, _ = strconv.Atoi(linesString) } if lines <= 0 { lines = 1 + int(math.Floor((math.Pow(2*rand.Float64()-1, 3)/2+0.5)*float64(5))) } if lines > len(log) { lines = len(log) } p.makeComic(bot, service, message, makeScriptFromMessages(service, message, log[len(log)-lines:])) } else { // Don't append commands. if bruxism.MatchesCommand(service, "", message) { return } switch message.Type() { case bruxism.MessageTypeCreate: if len(log) < 10 { log = append(log, message) } else { log = append(log[1:], message) } case bruxism.MessageTypeUpdate: for i, m := range log { if m.MessageID() == message.MessageID() { log[i] = message break } } case bruxism.MessageTypeDelete: for i, m := range log { if m.MessageID() == message.MessageID() { log = append(log[:i], log[i+1:]...) break } } } p.log[message.Channel()] = log } }