Exemplo n.º 1
0
// remind
func set(ctx *bot.Context) {
	// s == <target> <reminder> in|at|on <time>
	s := strings.Fields(ctx.Text())
	if len(s) < 4 {
		ctx.ReplyN("You asked me to remind %s.", ctx.Text())
		return
	}
	at, ok, reminder, timestr := time.Now(), false, "", ""
	for i := 1; i+1 < len(s); i++ {
		lc := strings.ToLower(s[i])
		if lc == "in" || lc == "at" || lc == "on" {
			timestr = strings.Join(s[i+1:], " ")
		} else if i+2 == len(s) {
			// Hack to test the last word for e.g. "tomorrow"
			i++
			timestr = strings.ToLower(s[i])
		} else {
			continue
		}
		// TODO(fluffle): surface better errors from datetime.Parse
		at, ok = datetime.Parse(timestr)
		if ok {
			reminder = strings.Join(s[1:i], " ")
			break
		}
	}
	if reminder == "" {
		ctx.ReplyN("You asked me to remind %s.", ctx.Text())
		return
	}
	if !ok {
		ctx.ReplyN("Couldn't parse time string '%s'", timestr)
		return
	}
	now := time.Now()
	start := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
	if at.Before(now) && at.After(start) {
		at = at.Add(24 * time.Hour)
	}
	if at.Before(now) {
		ctx.ReplyN("Time '%s' is in the past.", timestr)
		return
	}
	n, c := ctx.Storable()
	// TODO(fluffle): Use state tracking! And do this better.
	t := bot.Nick(s[0])
	if t.Lower() == strings.ToLower(ctx.Nick) ||
		t.Lower() == "me" {
		t = n
	}
	r := reminders.NewReminder(reminder, at, t, n, c)
	if err := rc.Insert(r); err != nil {
		ctx.ReplyN("Error saving reminder: %v", err)
		return
	}
	// Any previously-generated list of reminders is now obsolete.
	delete(listed, ctx.Nick)
	ctx.ReplyN("%s", r.Acknowledge())
	Remind(r, ctx)
}
Exemplo n.º 2
0
// remind
func set(line *base.Line) {
	// s == <target> <reminder> in|at|on <time>
	s := strings.Fields(line.Args[1])
	if len(s) < 4 {
		bot.ReplyN(line, "Invalid remind syntax. Sucka.")
		return
	}
	i := len(s) - 1
	for i > 0 {
		lc := strings.ToLower(s[i])
		if lc == "in" || lc == "at" || lc == "on" {
			break
		}
		i--
	}
	if i < 1 {
		bot.ReplyN(line, "Invalid remind syntax. Sucka.")
		return
	}
	reminder := strings.Join(s[1:i], " ")
	timestr := strings.ToLower(strings.Join(s[i+1:], " "))
	// TODO(fluffle): surface better errors from datetime.Parse
	at, ok := datetime.Parse(timestr)
	if !ok {
		bot.ReplyN(line, "Couldn't parse time string '%s'", timestr)
		return
	}
	now := time.Now()
	start := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
	if at.Before(now) && at.After(start) {
		// Perform some basic hacky corrections before giving up
		if strings.Contains(timestr, "am") || strings.Contains(timestr, "pm") {
			at = at.Add(24 * time.Hour)
		} else {
			at = at.Add(12 * time.Hour)
		}
	}
	if at.Before(now) {
		bot.ReplyN(line, "Time '%s' is in the past.", timestr)
		return
	}
	n, c := line.Storable()
	// TODO(fluffle): Use state tracking! And do this better.
	t := base.Nick(s[0])
	if t.Lower() == strings.ToLower(line.Nick) ||
		t.Lower() == "me" {
		t = n
	}
	r := reminders.NewReminder(reminder, at, t, n, c)
	if err := rc.Insert(r); err != nil {
		bot.ReplyN(line, "Error saving reminder: %v", err)
		return
	}
	// Any previously-generated list of reminders is now obsolete.
	delete(listed, line.Nick)
	bot.ReplyN(line, "%s", r.Acknowledge())
	Remind(r)
}