Esempio n. 1
0
func (p *topStreamersPlugin) messageFunc(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) {
	if !service.IsMe(message) {
		if bruxism.MatchesCommand(service, "topstreamers", message) {
			n := time.Now()
			if !n.After(p.lastUpdate.Add(1 * time.Minute)) {
				if p.lastMessage != "" {
					service.SendMessage(message.Channel(), fmt.Sprintf("%s *Last updated %s.*", p.lastMessage, humanize.Time(p.lastUpdate)))
				}
				return
			}

			service.Typing(message.Channel())

			p.lastUpdate = n

			m, err := p.topStreamers(5)
			if err != nil {
				service.SendMessage(message.Channel(), "There was an error while requesting the top streamers, please try again later.")
				return
			}

			service.SendMessage(message.Channel(), m)
			p.lastMessage = m
		}
	}
}
Esempio n. 2
0
// NumberTriviaCommand is a command for getting number trivial.
func NumberTriviaCommand(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message, command string, parts []string) {
	notfound := true
	num := rand.Intn(1000)
	if len(parts) == 1 {
		if i, err := strconv.Atoi(parts[0]); err == nil {
			num = i
			notfound = false
		}
	}

	service.Typing(message.Channel())

	str, err := numberTrivia(bot, num, notfound)
	if err != nil {
		service.SendMessage(message.Channel(), "There was an error requesting trivia, sorry!")
		return
	}

	service.SendMessage(message.Channel(), str)
}
Esempio n. 3
0
func (p *streamerPlugin) messageFunc(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) {
	if !service.IsMe(message) {
		if bruxism.MatchesCommand(service, "streamer", message) {
			query, parts := bruxism.ParseCommand(service, message)

			if len(parts) == 0 {
				return
			}

			r, _ := p.requests[query]
			if r == nil {
				r = &streamerPluginRequest{}
				p.requests[query] = r
			}

			n := time.Now()
			if !n.After(r.lastUpdate.Add(60 * time.Minute)) {
				if r.lastMessage != "" {
					service.SendMessage(message.Channel(), fmt.Sprintf("%s *Last updated %s.*", r.lastMessage, humanize.Time(r.lastUpdate)))
				}
				return
			}

			service.Typing(message.Channel())

			r.lastUpdate = n

			m, err := p.streamer(query, service.Name() == bruxism.DiscordServiceName)
			if err != nil {
				service.SendMessage(message.Channel(), "There was an error while requesting the streamer, please try again later.")
				return
			}

			service.SendMessage(message.Channel(), m)
			r.lastMessage = m
		}
	}
}
Esempio n. 4
0
// 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
	}
}
Esempio n. 5
0
func (p *chartPlugin) messageFunc(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) {
	if service.IsMe(message) {
		return
	}

	if bruxism.MatchesCommand(service, "chart", message) {
		query, parts := bruxism.ParseCommand(service, message)
		if len(parts) == 0 {
			service.SendMessage(message.Channel(), fmt.Sprintf("Invalid chart eg: %s", p.randomChart(service)))
			return
		}

		start, end := 0.5, 0.5

		switch parts[0] {
		case "up":
			start, end = 0, 1
		case "down":
			start, end = 1, 0
		case "flat":
		case "straight":
		default:
			service.SendMessage(message.Channel(), fmt.Sprintf("Invalid chart direction. eg: %s", p.randomChart(service)))
			return
		}

		axes := strings.Split(query[len(parts[0]):], ",")
		if len(axes) != 2 {
			service.SendMessage(message.Channel(), fmt.Sprintf("Invalid chart axis labels eg: %s", p.randomChart(service)))
			return
		}

		pl, err := plot.New()
		if err != nil {
			service.SendMessage(message.Channel(), fmt.Sprintf("Error making chart, sorry! eg: %s", p.randomChart(service)))
			return
		}

		service.Typing(message.Channel())

		pl.Y.Label.Text = axes[0]
		pl.X.Label.Text = axes[1]

		num := 5 + rand.Intn(15)

		start *= float64(num)
		end *= float64(num)

		pts := make(plotter.XYs, num)
		for i := range pts {
			pts[i].X = float64(i) + rand.Float64()*0.5 - 0.2
			pts[i].Y = start + float64(end-start)/float64(num-1)*float64(i) + rand.Float64()*0.5 - 0.25
		}

		pl.X.Tick.Label.Color = color.Transparent
		pl.Y.Tick.Label.Color = color.Transparent

		pl.X.Min = -0.5
		pl.X.Max = float64(num) + 0.5

		pl.Y.Min = -0.5
		pl.Y.Max = float64(num) + 0.5

		lpLine, lpPoints, err := plotter.NewLinePoints(pts)
		if err != nil {
			service.SendMessage(message.Channel(), fmt.Sprintf("Sorry %s, there was a problem creating your chart.", message.UserName()))
		}
		lpLine.Color = plotutil.Color(rand.Int())
		lpLine.Width = vg.Points(1 + 0.5*rand.Float64())
		lpLine.Dashes = plotutil.Dashes(rand.Int())
		lpPoints.Shape = plotutil.Shape(rand.Int())
		lpPoints.Color = lpLine.Color

		pl.Add(lpLine, lpPoints)

		w, err := pl.WriterTo(320, 240, "png")
		if err != nil {
			service.SendMessage(message.Channel(), fmt.Sprintf("Sorry %s, there was a problem creating your chart.", message.UserName()))
			return
		}

		b := &bytes.Buffer{}
		w.WriteTo(b)

		go func() {
			if service.Name() == bruxism.DiscordServiceName {
				discord := service.(*bruxism.Discord)
				p, err := discord.UserChannelPermissions(message.UserID(), message.Channel())
				if err == nil && p&discordgo.PermissionAttachFiles != 0 {
					service.SendFile(message.Channel(), "chart.png", b)
					return
				}
			}

			url, err := bot.UploadToImgur(b, "chart.png")
			if err != nil {
				service.SendMessage(message.Channel(), fmt.Sprintf("Sorry %s, there was a problem uploading the chart to imgur.", message.UserName()))
				log.Println("Error uploading chart: ", err)
				return
			}

			if service.Name() == bruxism.DiscordServiceName {
				service.SendMessage(message.Channel(), fmt.Sprintf("Here's your chart <@%s>: %s", message.UserID(), url))
			} else {
				service.SendMessage(message.Channel(), fmt.Sprintf("Here's your chart %s: %s", message.UserName(), url))
			}
		}()
	}
}