-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiscord.go
108 lines (91 loc) · 2.47 KB
/
discord.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
package main
import (
"encoding/json"
"io/ioutil"
"os"
"github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus"
)
// Have discordgo handle any incoming events pushed to us by the Discord API.
func discordEventReceiver() (*discordgo.Session, error) {
dg, err := discordgo.New(config.Discord.Token)
if err != nil {
log.Error("Error creating Discord session: ", err)
return nil, err
}
dg.AddHandler(muting)
err = dg.Open()
if err != nil {
log.Error("Error opening Discord session: ", err)
return nil, err
}
log.Info("Discord event receiver active.")
return dg, nil
}
// If Discord let's us know that we've been muted or unmuted
// then write the mute state to a file and set discordUnmuted to
// the appropriate value.
//
// Also, you have to be in a channel in order for Discord to emit
// a VoiceStateUpdate event.
func muting(s *discordgo.Session, v *discordgo.VoiceStateUpdate) {
if v.UserID == s.State.User.ID {
lkm := newLkm()
if v.Deaf || v.Mute || v.SelfDeaf || v.SelfMute {
log.Info("Muted on Discord.")
discordUnmuted = false
lkm.write(true)
} else {
log.Info("Unmuted on Discord.")
discordUnmuted = true
lkm.write(false)
}
}
return
}
// Discord's API is entirely push based. You cannot query for information.
// Therefore, there is no way to tell if you're muted or unmuted before
// a VoiceStateUpdate event is emitted. I.e, if you muted yourself and then quit
// Discord then there would be no way for OnAir to know that you're muted.
//
// Let's keep track of that muted state in a file.
//
// There is no locking or anything. Pretty simple.
type lastKnownMute struct {
Muted bool `json:"muted"`
filePath string
}
func newLkm() *lastKnownMute {
return &lastKnownMute{
filePath: stateFilePath,
}
}
func (l *lastKnownMute) read() bool {
dat, err := ioutil.ReadFile(l.filePath)
if err != nil {
log.WithFields(log.Fields{
"filePath": l.filePath,
}).Error("Could not read file: ", err)
}
json.Unmarshal(dat, l)
return l.Muted
}
func (l *lastKnownMute) write(state bool) {
l.Muted = state
m, err := json.Marshal(l)
if err != nil {
log.Error("Could not marshal last known muted state JSON: ", err)
}
err = os.Truncate(l.filePath, 0)
if err != nil {
log.WithFields(log.Fields{
"filePath": l.filePath,
}).Error("Could not truncate file: ", err)
}
err = ioutil.WriteFile(l.filePath, m, 0644)
if err != nil {
log.WithFields(log.Fields{
"filePath": l.filePath,
}).Error("Could not write file: ", err)
}
}