This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
notify.go
132 lines (104 loc) · 2.35 KB
/
notify.go
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
package main
import (
"html"
"strings"
"github.com/diamondburned/discordgo"
"github.com/diamondburned/tview/v2"
"github.com/gen2brain/beeep"
)
const pingHighlightNode = " [#ED2939](!)[-]"
func mentionHandler(m *discordgo.MessageCreate) {
// Crash-prevention
if d.State.Settings == nil {
return
}
var (
submessage = "said in a heated channel"
name = m.Author.Username
pinged bool
)
if m.Author.ID != d.State.User.ID {
if heatedChannelsExists(m.ChannelID) {
goto Notify
}
}
if !messagePingable(m.Message, m.GuildID) {
return
}
pinged = true
Notify:
if c, err := d.State.Channel(m.ChannelID); err == nil {
switch {
case !pinged:
submessage = "said in a heated channel"
case len(c.Recipients) > 0:
submessage = "messaged"
default:
submessage = "mentioned you"
}
if c.Name != "" {
submessage += " in #" + c.Name
m, err := d.State.Member(c.GuildID, m.Author.ID)
if err == nil {
if m.Nick != "" {
name = m.Nick
}
}
} else {
if len(c.Recipients) > 1 {
var names = make([]string, len(c.Recipients))
for i, p := range c.Recipients {
names[i] = p.Username
}
submessage += " in " + HumanizeStrings(names)
}
}
}
// Skip if user is busy
if d.State.Settings.Status != discordgo.StatusDoNotDisturb || !pinged {
// we ignore errors for users without dbus/notify-send
beeep.Notify(
name+" "+submessage,
html.EscapeString(m.ContentWithMentionsReplaced()),
"",
)
// if it's a heat signal
if !pinged {
return
}
}
// Walk the tree for the sake of a (1)
if Channel != nil && m.ChannelID == Channel.ID {
return
}
root := guildView.GetRoot()
if root == nil {
return
}
root.Walk(func(node, parent *tview.TreeNode) bool {
if parent == nil {
return true
}
reference, ok := node.GetReference().(*discordgo.Channel)
if !ok {
return true
}
if reference.ID != m.ChannelID {
return false
}
pingNode := tview.NewTreeNode(
"[#ED2939]" + tview.Escape(name) + "[-] mentioned you",
)
pingNode.SetSelectable(false)
pingNode.SetIndent(cfg.Prop.SidebarIndent - 1)
node.AddChild(pingNode)
node.Expand()
if _, ok := parent.GetReference().(*discordgo.Guild); ok {
if !strings.HasSuffix(parent.GetText(), pingHighlightNode) {
parent.SetText(parent.GetText() + pingHighlightNode)
}
}
return false
})
app.Draw()
}