-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
100 lines (83 loc) · 2.48 KB
/
main.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
package main
import (
"bytes"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
)
const (
TargetUserID = "TARGET_USER_ID"
)
func main() {
dg, err := discordgo.New("Bot " + os.Getenv("DISCORD_BOT_TOKEN"))
if err != nil {
fmt.Println("error creating Discord session,", err)
return
}
// We only care about voice state updates, so we can ignore other events.
dg.Identify.Intents = discordgo.IntentsGuildVoiceStates
dg.AddHandler(voiceStateUpdate)
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
// Cleanly close down the Discord session.
dg.Close()
}
func voiceStateUpdate(s *discordgo.Session, m *discordgo.VoiceStateUpdate) {
// Check if the user ID matches the specific user
if m.UserID == os.Getenv("DISCORD_USER_ID") {
// Check if the user has just joined a voice channel
if m.ChannelID != "" {
if m.SelfMute {
fmt.Println("muted")
notifyHA(true)
} else {
fmt.Println("no longer muted")
notifyHA(false)
}
} else {
fmt.Println("no longer muted because no longer in channel")
notifyHA(false)
}
}
}
func notifyHA(sensor_state bool) {
// slap that HA HTTP endpoint with a big ol' POST
// true should mean sensor on
// false should mean sensor off
url := os.Getenv("HA_BASE_URL") + "/api/states/binary_sensor." + os.Getenv("HA_SENSOR_NAME")
fmt.Println("url: " + url)
jsonStr := []byte(``) // i'm not proud of this. surely there is a better way, but i will not be doing it at this time
if sensor_state {
jsonStr = []byte(`{"state": "on", "attributes": {"friendly_name": "Discord Muted Sensor"}}`)
} else {
jsonStr = []byte(`{"state": "off", "attributes": {"friendly_name": "Discord Muted Sensor"}}`)
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
if err != nil {
panic(err)
}
req.Header.Set("Authorization", "Bearer "+os.Getenv("HA_AUTH_TOKEN"))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// handy debug statements
// fmt.Println("response Status:", resp.Status)
// fmt.Println("response Headers:", resp.Header)
// body, _ := io.ReadAll(resp.Body)
// fmt.Println("response Body:", string(body))
}