Exemple #1
0
// snooze
func snooze(ctx *bot.Context) {
	r, ok := finished[strings.ToLower(ctx.Nick)]
	if !ok {
		ctx.ReplyN("No record of an expired reminder for you, sorry!")
		return
	}
	now := time.Now()
	at := now.Add(30 * time.Minute)
	if ctx.Text() != "" {
		at, ok = datetime.Parse(ctx.Text())
		if !ok {
			ctx.ReplyN("Couldn't parse time string '%s'.")
			return
		}
		if at.Before(now) {
			ctx.ReplyN("You can't snooze reminder into the past, fool.")
			return
		}
	}
	r.Created = now
	r.RemindAt = at
	if _, err := rc.UpsertId(r.Id, r); err != nil {
		ctx.ReplyN("Error saving reminder: %v", err)
		return
	}
	delete(listed, ctx.Nick)
	ctx.ReplyN("%s", r.Acknowledge())
	Remind(r, ctx)
}
Exemple #2
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)
}
Exemple #3
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)
}
Exemple #4
0
func date(ctx *bot.Context) {
	tstr, zone := ctx.Text(), ""
	if idx := strings.Index(tstr, "in "); idx != -1 {
		tstr, zone = tstr[:idx], strings.TrimSpace(tstr[idx+3:])
	}
	tm, ok := time.Now(), true
	if tstr != "" {
		if tm, ok = datetime.Parse(tstr); !ok {
			ctx.ReplyN("Couldn't parse time string '%s'.", tstr)
			return
		}
	}
	if loc := datetime.Zone(zone); zone != "" && loc != nil {
		tm = tm.In(loc)
		ctx.ReplyN("%s", tm.Format(datetime.TimeFormat))
	} else {
		ctx.ReplyN("%s", datetime.Format(tm))
	}
}