-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.go
More file actions
121 lines (106 loc) · 2.72 KB
/
timer.go
File metadata and controls
121 lines (106 loc) · 2.72 KB
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
package main
import (
"fmt"
"time"
"github.com/bwmarrin/discordgo"
"github.com/dustin/go-humanize"
)
type Timer struct {
InternalID int
ID string
Message string
User string
Channel string
Created time.Time
Due time.Time
SnoozedDue time.Time
SnoozeCount int
Shown bool
}
func checkDueTimers(session *discordgo.Session) {
timers, err := getDueTimers()
if err != nil {
fmt.Println("Error getting due timers:", err)
return
}
for _, timer := range timers {
showDueTimer(session, timer)
err := markTimerAsShown(timer.ID)
if err != nil {
fmt.Println("Error marking timer as shown:", err)
}
}
}
func showDueTimer(session *discordgo.Session, timer *Timer) {
user, err := session.User(timer.User)
if err != nil {
fmt.Println("Error getting user:", err)
return
}
embed := createTimerEmbed(timer, user, TimerEmbedTypeDue)
_, err = session.ChannelMessageSendComplex(timer.Channel, &discordgo.MessageSend{
Embeds: []*discordgo.MessageEmbed{embed},
Content: user.Mention(),
})
if err != nil {
fmt.Println("Error sending message:", err)
}
}
type TimerEmbedType struct {
Title string
color int
includeDurationForDue bool
includeDurationForCreated bool
}
var (
TimerEmbedTypeCreation = TimerEmbedType{"Timer Created", 0x00ff00, true, false}
TimerEmbedTypeDeletion = TimerEmbedType{"Timer Deleted", 0xff0000, false, true}
TimerEmbedTypeSnooze = TimerEmbedType{"Timer Snoozed", 0x00ffff, true, true}
TimerEmbedTypeEdit = TimerEmbedType{"Timer Edited", 0xffff00, true, true}
TimerEmbedTypeDue = TimerEmbedType{"Timer Due", 0x0000ff, false, true}
)
func createTimerEmbed(timer *Timer, owner *discordgo.User, embedType TimerEmbedType) *discordgo.MessageEmbed {
due := formatTime(timer.SnoozedDue, embedType.includeDurationForDue)
created := formatTime(timer.Created, embedType.includeDurationForCreated)
return &discordgo.MessageEmbed{
Title: embedType.Title,
Description: timer.Message,
Color: embedType.color,
Fields: []*discordgo.MessageEmbedField{
{
Name: "Id",
Value: timer.ID,
Inline: true,
},
{
Name: "Owner",
Value: owner.Mention(),
Inline: true,
},
{
Name: "\u200b",
Value: "\u200b",
Inline: true,
},
{
Name: "Due",
Value: due,
Inline: true,
},
{
Name: "Created",
Value: created,
Inline: true,
},
},
}
}
func formatTime(timeToFormat time.Time, includeDuration bool) string {
base := timeToFormat.In(time.Local).
Format("02/01/2006, 15:04:05")
if includeDuration {
// fmt.Sprintf("<t:%d:R>", timer.Due.Unix())
base += fmt.Sprintf("\n(%s)", humanize.Time(timeToFormat))
}
return base
}