コード例 #1
0
ファイル: commands.go プロジェクト: lemoncola/DisGoBot
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"))
	}
}
コード例 #2
0
ファイル: liking.go プロジェクト: lemoncola/DisGoBot
func seen(extraArgs string, splitArgs []string, c *CommandContext) {
	if len(splitArgs) < 1 {
		c.AddReply("Not enough arguments.")
		return
	}
	log.Debug(strings.Join(splitArgs, ","))
	username := splitArgs[0]
	if !usernameRegex.MatchString(username) {
		c.AddReply(fmt.Sprintf(
			"'%s' is not a valid username.", username))
		return
	}

	var response discourse.ResponseUserSerializer

	err := c.Bot.DGetJsonTyped(fmt.Sprintf("/users/%s.json", username), &response)
	if err != nil {
		if _, ok := err.(discourse.ErrorNotFound); ok {
			c.AddReply(fmt.Sprintf(
				"The user '%s' does not exist.", username))
		} else {
			c.AddReply(fmt.Sprintf(
				"Error fetching data: " + err.Error()))
		}
		return
	}

	response.User.ParseTimes()

	duration := time.Since(response.User.LastSeenAtTime)
	duration = duration % time.Second
	c.AddReply(fmt.Sprintf(
		"@%s was last seen on the date %s, which was %s ago.",
		response.User.Username, response.User.LastSeenAtTime.Format(time.Stamp), duration))
}