Example #1
0
func RunCommandBatch(commandLines [][]string, post discourse.S_Post, bot *discourse.DiscourseSite) {
	log.Info("Processing commands in post", post.Topic_id, post.Post_number, commandLines)

	var context = CommandContext{
		User:        CredentialsFromPost(post),
		Post:        post,
		Bot:         bot,
		replyBuffer: make([]string, 0),
	}

	for _, command := range commandLines {
		log.Debug(command[1], "X", command[2])
		if HasCommand(command[1]) {
			RunCommand(command[1], command[2], &context)
		} else {
			log.Warn("No such command", command[1])
		}
	}

	if context.redis != nil {
		context.redis.Close()
	}
	if len(context.replyBuffer) > 0 {
		bot.Reply(post.Topic_id, post.Post_number, strings.Join(context.replyBuffer, "\n\n"))
	}
}
Example #2
0
func (rl DRateLimiter) performed() {
	if len(rl.slots) > 0 {
		newest := rl.slots[0]
		oldest := rl.slots[len(rl.slots)-1]
		if newest.Add(time.Duration(float64(rl.eventExpiry) * 2.04)).Before(time.Now()) {
			// Ratelimit structure in Redis has expired. (2% margin)
			// Dump the timestamps
			rl.slots = make([]time.Time, 0, cap(rl.slots))
		} else if oldest.Add(rl.eventExpiry).Before(time.Now()) {
			// Oldest has expired, we have a use
		} else {
			// Have to wait.
			sleepTime := time.Now().Sub(oldest.Add(rl.eventExpiry))
			log.Warn("Ratelimit triggered! Waiting for", sleepTime)
			time.Sleep(sleepTime)
		}
	}

	// Put now on the end
	rl.slots = append(rl.slots, time.Now())
	if len(rl.slots) == cap(rl.slots)-1 {
		// Trim if full
		rl.slots = rl.slots[1:]
	}
}
Example #3
0
func help(extraArgs string, splitArgs []string, context *CommandContext) {
	log.Warn("Help command not implemented")
}