~kris/9p

glenda

ref: 4866136749b796584ec5f2c40543c736db5640fe glenda/x/mux/remind.go -rw-r--r-- 2.9 KiB
48661367 — Kris Yotam chore: sync local state after restore (push updates, no pull) a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package mux

import (
	"github.com/bwmarrin/discordgo"
	"time"
sc	"strconv"
)


// Channel for posting reminders -- inits in main
var RemChan chan Reminder

// Stores a reminder to be posted to a given user
type Reminder struct {
	NotifyAfter	time.Time
	Reason		string
	User			discordgo.User
	ChannelID	string
}

// Reminder daemon process that gets started in main and listens on RemChan
func Reminders() {
	// TODO -- Should be a heap and more robust (fix config first)

	// Handle reminders
	for {
		select {
		case r := <- RemChan:
			// Handle new reminder
			Config.Reminders = append(Config.Reminders, r)
			//write()
			
		default:
			// Check for any due reminders
			for i, r := range Config.Reminders {
				if time.Now().After(r.NotifyAfter) {
					// If we have passed the time of desired notification
					Session.ChannelMessageSend(r.ChannelID, r.User.Mention() + " -- " + r.Reason)
					
					// Delete the reminder
					// God this is ugly
					switch len(Config.Reminders) {
					case 1:
						// Make empty safely
						Config.Reminders = []Reminder{}
					case 2:
						// Binary switch
						switch i {
						case 0:
							Config.Reminders = []Reminder{Config.Reminders[1]}
						case 1:
							Config.Reminders = []Reminder{Config.Reminders[0]}
						}
					default:
						i = min(i, len(Config.Reminders) - 1)
						switch i {
						case len(Config.Reminders) - 1:
							// Last
							Config.Reminders = Config.Reminders[:i]
						case 0:
							// First
							Config.Reminders = Config.Reminders[i+1:]
						default:
							// Not first nor last
							Config.Reminders = append(Config.Reminders[:i], Config.Reminders[i+1:]...)
						}
					}

					//write()
				}
			}
		
			time.Sleep(50 * time.Millisecond)
		}
	}
}

// Reminds a user about a given reason after a specified time interval has passed
func (m *Mux) RemindMe(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
	resp := ""
	
	// remindme 20m dothing
	if len(ctx.Fields) >= 3 {
		var rem Reminder
		periodLong := ctx.Fields[1]
		reasonLong := ctx.Fields[2 : len(ctx.Fields)]
		rem.User = *dm.Author
		rem.ChannelID = dm.ChannelID
		//rem.Session = *ds
		
		reason := ""
		for _, v := range reasonLong {
			reason += v
			reason += " "
		}
		rem.Reason = reason
		
		interval := periodLong[len(periodLong)-1]
		period, err := sc.Atoi(periodLong[:len(periodLong)-1])
		if err != nil {
			resp += "Invalid period value."
			goto SEND
		}
		
		var dur time.Duration
		switch interval {
		case 's':
			dur = time.Second
		case 'm':
			dur = time.Minute
		case 'h':
			dur = time.Hour
		case 'd':
			dur = time.Hour * time.Duration(24)
		default:
			resp += "Invalid interval specifier."
			goto SEND
		}

		rem.NotifyAfter = time.Now().Add(time.Duration(period) * dur)
		
		RemChan <- rem
		
		resp += "Ok."
		
	} else {
		resp += "Please specify a time operator in the form [int][type] and a description (20h do thing)."
	}
	
	SEND:
	resp += "\n"

	ds.ChannelMessageSend(dm.ChannelID, resp)

	return
}